This is a simple shell script that can be used for addition and deletion of user accounts on UNIX boxes by system administrators,though I have primarily tested it on AIX boxes.The script doesn't have an option of an automatic sudoers file entry and deletion right now,it's for non sudo privilege users management.I will publish sudoers file enrty enabled script in next post. However for the script to work you need to have password less SSH configured from NIM server.
To run the script simply paste the script in your home directory on a NIM server and execute the script,the password less ssh would go on each server mentioned by you and create or delete the user.
Enter servernames seperated by space not in a line eg. servername1 servername2 servername3 is correct while servername1
servername2 this is incorrect.However the script can be easily modified to read servernames from a file instead of passing them as arguement when prompted for inputting them by user
#!/usr/bin/ksh
#function for getting user details
userinfo()
{
echo "Enter first Name: ";
read fname;
echo "Enter Last Name: ";
read lname;
echo "Enter Ticket Number: ";
read tno;
echo "Enter ShortId: "
read sid;
}
#function for displaying usrid window
createuser()
{
echo "Argument fname:" . $fname;
echo "Argument lname:" . $lname;
echo "Argument ticket number:" . $tno;
echo "Argument team name:" . $tname;
echo "Arguement ShortID: " . $sid;
for i in $servernames
do
ssh $i /usr/sbin/useradd -c \"$fname $lname $tno $tname\" -d \"/home/$sid\" -m -u \"$uid\" -g staff -G staff -s /usr/bin/ksh $sid;
if [ "$?" -ne "0" ];
then
echo "User Addition failed"
return 1
else
echo "User $sid created succesfully in $i" >>useraddedonserverlist
echo `date`>>useraddedonserverlist
echo "User $sid created succesfully in $i"
fi
done
for j in $servernames
do
ssh $j "echo $sid:change > /home/rm370/pwdfile ; cat /home/rm370/pwdfile | chpasswd";
done
echo "Password Set";
}
#####MAIN SCRIPT####
echo "##########USER CREATION AND DELETION SCRIPT ########################";
echo "############# ################";
echo "Please select from one of the options: ";
echo "1.General User Addition";
echo "2.Deletion of a User";
echo "3.Exit"
echo
echo "Please enter your choice: ";
read choice;
if [ $choice -eq 1 ];then
echo "entered option1";
userinfo
#echo $fname;
#echo $lname;
#echo $tno;
#echo $sid;
echo "Please enter the Team Name: "
read tname;
userinfo="$fname $lname $tno $tname"
echo $userinfo;
homedir="/home/$sid"
echo $homedir;
echo "Please enter the server names: ";
read servernames;
createuser
fi
if [ $choice -eq 2 ];then
echo "entered option 2";
echo "Enter short id of user: ";
read userdelid
echo "Enter Ticket Number for deletion request: "
read deltno
echo "Enter list of servernames on which id needs to be deleted:";
read serverids
for n in $serverids
do
ssh $n "/usr/sbin/rmuser -p $userdelid"
if [ "$?" -ne "0" ];
then
echo "User deletion failed"
return 1
else
echo "User $userdelid($deltno)was deleted succesfully in $n" >> userdelonserverlist
echo `date`>>userdelonserverlist
echo "User $userdelid was deleted succesfully in $n"
fi
done
fi
if [ $choice -eq 3 ];then
echo "entered option 3";
exit
fi
To run the script simply paste the script in your home directory on a NIM server and execute the script,the password less ssh would go on each server mentioned by you and create or delete the user.
Enter servernames seperated by space not in a line eg. servername1 servername2 servername3 is correct while servername1
servername2 this is incorrect.However the script can be easily modified to read servernames from a file instead of passing them as arguement when prompted for inputting them by user
#!/usr/bin/ksh
#function for getting user details
userinfo()
{
echo "Enter first Name: ";
read fname;
echo "Enter Last Name: ";
read lname;
echo "Enter Ticket Number: ";
read tno;
echo "Enter ShortId: "
read sid;
}
#function for displaying usrid window
createuser()
{
echo "Argument fname:" . $fname;
echo "Argument lname:" . $lname;
echo "Argument ticket number:" . $tno;
echo "Argument team name:" . $tname;
echo "Arguement ShortID: " . $sid;
for i in $servernames
do
ssh $i /usr/sbin/useradd -c \"$fname $lname $tno $tname\" -d \"/home/$sid\" -m -u \"$uid\" -g staff -G staff -s /usr/bin/ksh $sid;
if [ "$?" -ne "0" ];
then
echo "User Addition failed"
return 1
else
echo "User $sid created succesfully in $i" >>useraddedonserverlist
echo `date`>>useraddedonserverlist
echo "User $sid created succesfully in $i"
fi
done
for j in $servernames
do
ssh $j "echo $sid:change > /home/rm370/pwdfile ; cat /home/rm370/pwdfile | chpasswd";
done
echo "Password Set";
}
#####MAIN SCRIPT####
echo "##########USER CREATION AND DELETION SCRIPT ########################";
echo "############# ################";
echo "Please select from one of the options: ";
echo "1.General User Addition";
echo "2.Deletion of a User";
echo "3.Exit"
echo
echo "Please enter your choice: ";
read choice;
if [ $choice -eq 1 ];then
echo "entered option1";
userinfo
#echo $fname;
#echo $lname;
#echo $tno;
#echo $sid;
echo "Please enter the Team Name: "
read tname;
userinfo="$fname $lname $tno $tname"
echo $userinfo;
homedir="/home/$sid"
echo $homedir;
echo "Please enter the server names: ";
read servernames;
createuser
fi
if [ $choice -eq 2 ];then
echo "entered option 2";
echo "Enter short id of user: ";
read userdelid
echo "Enter Ticket Number for deletion request: "
read deltno
echo "Enter list of servernames on which id needs to be deleted:";
read serverids
for n in $serverids
do
ssh $n "/usr/sbin/rmuser -p $userdelid"
if [ "$?" -ne "0" ];
then
echo "User deletion failed"
return 1
else
echo "User $userdelid($deltno)was deleted succesfully in $n" >> userdelonserverlist
echo `date`>>userdelonserverlist
echo "User $userdelid was deleted succesfully in $n"
fi
done
fi
if [ $choice -eq 3 ];then
echo "entered option 3";
exit
fi
No comments:
Post a Comment