Monday, February 6, 2012

Change the Name of Blog

I want to write several topics. But always it is complex to write everything. Here I shared my Unix shell experience. I will do another blog for non technical subjects. This blog will keep for share my technical experience. Sorry for change the name of blog.

Thursday, February 2, 2012

Add User for Multiple Servers

This is common task for most of the administrator. As example, new employee join  or someone assign to the role he/she need access to range of servers. Most of the time system administrators do manually. If data centre has centralised authentication (LDAP/AD) this is not issue. If not  the you have to add one by one.
To run this script remote servers able to access via SSH. If you are not configured key authentication you have to login each server.

On my script, you have to run it on one of your management server. If you do not have management server you can chose one of your current server to execute the script. When you execute the script it is asking some questions. Those question based on information that require to add user. Then it is creating script and store in same location. This script would not delete after end the task. If you want you can create it manually. But I keep it record purpose.
The script generated script then copy on remote machine and execute. after that then it will delete. Deleting process is on the script

#!/bin/bash
###################################################
#                                                 #
#      ADD ONE USER FOR MULTIPLE LINUX SERVERS    #
#               Version 1.0                       #
#        Created Date 08/01/2012                  #
#     Last Modified Date 08/01/2012               #
# INSTRUCTIONS-                                   #
# Save the server_list.csv file same place        #
# which you saved script. (linux server)          #
# (Both script and server_list.csv file in same   #
# location                                        #
###################################################
host_name=""
file_name="server_list.csv"
echo -n "Which login ID are you using to connect to remote servers? "
read login_id
echo
echo ":::::::::<<<<<<<< ENTER USER DETAILS >>>>>>>>:::::::::::"
echo -n "Enter username : "
read username
echo -n "Enter password : "
read password
echo -n "Enter user ID (employee number) : "
read uid
echo -n "Enter First Name of user : "
read fname
echo -n "Enter Last Name of user : "
read lname
echo -n "Enter email address of user : "
read email
echo -n "Does user required su/sudo privilege? (y/n)  : "
read susudo
echo
echo "::::::::::<<<<<<<<<< USER INFORMATION ARE AS BELOW >>>>>>>>>>:::::::::::"
echo "username : " $username
echo "password : " $password
echo "user ID : "$uid
echo "First Name : "$fname
echo "Last Name : "$lname
echo "email address : "$email
echo "su/sudo privilege : "$susudo
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
echo
echo -n "Does this infromation correct (y/n)? "
read answer
PASSWORD=$(perl -e 'print crypt($ARGV[0], "password")' $password)
home_dir="/home/"$username"/"
file_name="server_list.csv"
PRIVILAGE="wheel,trusted"
COMMENT="'"$fname' '$lname' '$email"'"
if [ $answer = y ]
then
        if [ $susudo = y ]
        then
                 echo "#!/bin/sh" >> $username.sh
                 echo "egrep $username /etc/passwd >/dev/null">$username.sh
                echo "if [ $""? -eq 0 ]; then
                                echo '!!!!!!!!!! Username $username exsists in /etc/passwd file !!!!!!!!!!'
                                else
                                cp -p /etc/passwd /etc/passwd.`date +%d%b%Y`;
                                cp -p /etc/passwd /etc/shadow.`date +%d%b%Y`;
                                echo '<<<<<<< /etc/passwd and /etc/shadow files backuped! <<<<<<'
                                /usr/sbin/useradd -m $username  -u $uid -c $COMMENT -d $home_dir  -p $PASSWORD -G $PRIVILAGE
                                echo '>>>>>>>>>>>> user $username created with su and sudo privileges >>>>>>>>>>>>'
                                fi
                                exit 0" >>$username.sh
        else
                 echo "#!/bin/sh" >> $username.sh
                 echo "egrep $username /etc/passwd >/dev/null">$username.sh
                echo "if [ $""? -eq 0 ]; then
                                echo '!!!!!!!!!!!! Username $username exsists in /etc/passwd file !!!!!!!!!!!!!!'
                                else
                                cp -p /etc/passwd /etc/passwd.`date +%d%b%Y`;
                                cp -p /etc/passwd /etc/shadow.`date +%d%b%Y`;
                                echo '<<<<<<< /etc/passwd and /etc/shadow files backuped! <<<<<<'
                                /usr/sbin/useradd -m $username  -u $uid -c $COMMENT -d $home_dir  -p $PASSWORD
                                echo '>>>>>>>>>>>> user $username created without su and sudo privileges >>>>>>>>>>>>'
                                fi
                                exit 0" >>$username.sh
        fi
else
echo '>>>>>>>>>>>>> YOU GIVEN INFORMATION NOT CONFIRMED AS CORRECT. BYE!!!!!!!!! <<<<<<<<<<<<'
exit 0
fi
for i in $(cat $file_name);
        do
        hostname=$(echo $i| cut -f1 -d',')
        cat $username.sh | ssh $login_id@$hostname "
        cat > /tmp/$username.sh;
        chmod 755 /tmp/$username.sh;
        sudo /tmp/$username.sh
        rm -r /tmp/$username.sh
        exit 0
        "
        done
exit 0
You have to prepare user_list.csv file. This file content looks like below
(username,user ID, first name, Last name, user email, sudo access,)
example
username1,11111,test1,user1,test1.user1@example.com,yes,
username2,22222,test2,user2,test2.user2@example.com,yes,
username3,33333,test3,user3,test3.user3@example.com,yes,

Try this and send feed back. Sometimes you need to do small modification to get result