Created
August 24, 2018 19:33
-
-
Save delgadofarid/b45e5f315cf867fe9fe226c20c1d34a1 to your computer and use it in GitHub Desktop.
Getting LDAP data and Updating MySQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Current time function | |
NOW=$(date +"%Y-%m-%d_%H-%M-%S") | |
# Set output file name | |
FILE="/tmp/inactive_users.$NOW.csv" | |
#ldapsearch -x \ | |
# -H "CHANGEIT" \ # -> LDAP HOST & PORT | |
# -b "CHANGEIT" \ # -> OU USER LOCATION | |
# -D "CHANGEIT" \ # -> USER FOR BINDING | |
# -w CHANGEIT \ # -> PASSWORD FOR BIND USER | |
# CHANGEIT \ # -> DEFAULT FILTER | |
# sAMAccountName \ # -> return USERNAME | |
# -LLL \ # -> PERFOM A SUBTREE SEARCH USING THE DEFAULT SEARCH BASE | |
# | grep '^sAMAccountName:' | sed 's/sAMAccountName: //' > $FILE # -> FORMAT OUTPUT | |
# Create output file with LDAP active users | |
ldapsearch -x \ | |
-H "CHANGEIT" \ | |
-b "CHANGEIT" \ | |
-D "CHANGEIT" \ | |
-w CHANGEIT \ | |
CHANGEIT \ | |
sAMAccountName \ | |
-LLL \ | |
| grep '^sAMAccountName:' | sed 's/sAMAccountName: //' > $FILE | |
# Set public access to log file | |
chmod 777 $FILE | |
# Construct string of active users required for update query | |
users=$(cat $FILE | sed 's/^/"/' | sed 's/$/",/' | sed '$ s/.$//') | |
# Get users to be marked as INACTIVE and append them to $FILE | |
mysql \ | |
-u intranetwordpress \ | |
-pH5UxVNRD_1 intranet_wordpress \ | |
-e "SELECT A.user_login \ | |
FROM wp_users A \ | |
JOIN wp_usermeta B ON B.user_id = A.ID \ | |
WHERE A.user_login NOT IN ($users) \ | |
AND A.user_status = 0 \ | |
AND B.meta_key = 'mo_ldap_user_dn';" | sed -n '1!p' > $FILE | |
FILESIZE=$(stat -c%s $FILE) | |
if [ $FILESIZE -eq 0 ] | |
then | |
rm $FILE | |
else | |
sed -i '1 i\Users marked as INACTIVE' $FILE | |
fi | |
# Update users from DB to INACTIVE | |
mysql \ | |
-u intranetwordpress \ | |
-pH5UxVNRD_1 intranet_wordpress \ | |
-e "UPDATE wp_users as A \ | |
JOIN wp_usermeta as B ON B.user_id = A.ID \ | |
SET A.user_status = 1 \ | |
WHERE A.user_login NOT IN ($users) \ | |
AND A.user_status = 0 \ | |
AND B.meta_key = 'mo_ldap_user_dn';" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment