Created
November 8, 2019 01:00
-
-
Save iomarmochtar/fa618b48d24aa1f39c239efea3cb4178 to your computer and use it in GitHub Desktop.
Move user's OU by sAMAccountName for samba4
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 | |
# Author: Imam Omar Mochtar ([email protected]) | |
# Move user's OU by sAMAccountName for samba4 | |
# arg1 = param name, arg2 = value, arg3 = default value | |
getParam(){ | |
if [ "$2"x == "x" ] && [ "$3"x == "x" ]; then | |
echo "$1 dimasukan diset" | |
exit 1 | |
fi | |
if [ "$2"x == "x" ]; then | |
echo $3 | |
else | |
echo $2 | |
fi | |
} | |
isFileExist(){ | |
if [ ! -f $1 ]; then | |
echo "$1 is not exists" | |
exit 1 | |
fi | |
} | |
ACT_LIST=`getParam "Account list file" $1` | |
OU_TO_MOVE=`getParam "Detination OU" $2` | |
LDB=`getParam "LDB" $3 "/var/lib/samba/private/sam.ldb"` | |
isFileExist $ACT_LIST | |
isFileExist $LDB | |
for act in `cat $ACT_LIST`; do | |
filter="sAMAccountName=$act" | |
echo "" | |
echo "Searching for account by filter $filter" | |
dn=`ldbsearch -H $LDB $filter DN | grep dn: | cut -d\: -f2 | sed -e 's/^[ \t]*//'` | |
if [ "$dn"x == "x" ]; then | |
echo "DN is not found for $act" | |
continue | |
fi | |
cn=`echo $dn | cut -d\, -f1` | |
ESDN="$dn" | |
ESTODN="$cn,$OU_TO_MOVE" | |
echo "Moving account $ESDN to OU $ESTODN" | |
#echo "ldbrename -H $LDB $ESDN $ESTODN" | |
ldbrename -H $LDB "$ESDN" "$ESTODN" > /dev/null && echo "Success" || echo "Failed" | |
done | |
echo "Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment