Sunday, September 22, 2013

[bash] Remove Users and their directories from LDAP server, that NOT in active users list

this is also from 2009:
Task: Remove Users and their directories from LDAP server, that NOT in active users list
 users1.txt – userlist from HR We will leave only theses users
 users2.txt – all LDAP users:
smbldap-userlist -u > users.txt
awk -F"|" '{ print $2 }' users.txt > users_1.txt

Next we will clean tab signs and spaces:
cat users_1.txt | sed 's/^[ t]*//;s/[ t]*$//' > users2.txt
Next we will create PHP (php_script.php) to compare two files (http://php.net/manual/en/function.array-diff.php:
<?php
$file_array1 = file( "users1.txt" );
//print_r ($file_array1);

$file_array2 = file( "users2.txt" );
//print_r ($file_array2);

$result = array_diff($file_array2, $file_array1);
print_r ($result);
?>
To run this PHP script use:
root@localhos:~# /usr/bin/php -f php_script.php > users3.txt
clean up:
awk -F"=>" '{ print $2 }' tmp.txt > users_to_delete.txt
Then we will delete non-active users:
#!/bin/bash
for i in `cat users_to_delete.txt`;
do echo "going to remove user $i";
/usr/sbin/smbldap-userdel -r $i;
echo "going to remove directory of $i";
rm -R /home/$i;
done;

No comments:

Post a Comment