So, the script I've been working on, since I was starting to learn Shell scripting is now complete.
This was coded in ksh, and I am very proud of it.
What this script does, is syncs up uid's across the network. So if you have 10 servers, with 10 usernames with different UID's - this will sync them up.
Special thanks to Aigles, who helped me with the chown part of the script!
This script requires that you set up ssh keys!
#############################################################
#This script will sync UID across AIX/Linux servers.
##############################################################
#----------------------- Variables -----------------------#
#id=`whoami`
id=
tmpvar="/tmp/{$USER}uid.tmp"
#----------------------- Functions -----------------------#
function check1 {
sudo grep -x $UID /etc/passwd | cut -d: -f3
}
function check2 {
sudo id -u $USER
}
#----------------------------------------------------------#
clear
echo "\n \n \n"
echo "This script will sync UID's across multiple servers."
echo "It will also find all files owned by the user and set the ownership to the new UID."
echo
USER=
while [ -z "$USER" ] ;
do
echo "What user has an incorrect UID on the servers? \c"
read USER
if [ -z "$USER" ] ; then
echo "You must enter a valid User id...!"
USER=
fi
done
echo
UID=
while [ -z "$UID" ] ;
do
echo "What is the correct UID for $USER ? \c"
read UID
if [ -z "$UID" ] ; then
echo "You must enter a UID...!"
UID=
fi
done
echo
LIST=
while [ -z "$list" ];
do
echo
echo "** Which servers do you want to sync UID's up on?"
echo "** List the servers, space delimited (e.g., server1 server2 server3) \n \t \c"
read list
if [ -z "$list" ]; then
echo "You have to enter at least one server!"
list=
fi
done
sleep 2
echo " "
echo " "
echo "Searching to see if the UID is being used on any user on $list."
for server0 in $list
do
ssh -q $id@$server0 "variable1=`check1`"
if [ "variable1 -ne $UID" ]; then
echo "No UID found"
else
echo "UID already in use!!"; exit
fi
done
sleep 2
echo " "
echo " "
echo "Searching to see if the UID is already being used by $USER"
for server00 in $list
do
ssh -q $id@$server00 "variable2=`check2`"
if [ "$UID -ne $variable2" ]; then
echo "UID's are not the same. You may proceed"
else "$USER already has an ID of $UID"; exit
fi
done
echo " "
echo " "
sleep 2
echo " "
echo "Searching for files owned by $USER, on the following servers: $list."
echo "Hold.."
echo "Any file found will be dumped into the temp file: /tmp/uid.tmp"
echo " "
for server in $list
do
ssh -q $id@$server "sudo find / -user $USER | grep -v proc | grep -v dev > /tmp/{$USER}uid.tmp"
done
sleep 2
# Change the UID
for server2 in $list
do
ssh -q $id@$server2 "sudo usermod -u $UID $USER"
echo "If this fails, the UID you're trying to change to is probably in use already"
done
sleep 2
# Change the ownership
chown_command=""
for tmp in $tempvar
do
chown_command="${chown_command}chown $UID $tmp;"
done
for server3 in $list
do
ssh -q $id@$server3 "${chown_command}"
done
sleep 2
#Delete the temp files. --- Commented out for Debugging! Manually delete tmp file!! ---
for server4 in $list
do
ssh -q $id@$server4 "sudo rm /tmp/{$USER}uid.tmp"
done
echo "All finished!"
exit 0