Tuesday, April 17, 2012

What we take as server information as backup...

This is the very common question to Unix administrators. Sometimes servers will crash during the change or any implementation. sometime backup is not help to recover the server. System administrator needs to build the server from initial stage. So what information require to build server initial level. These are some basic.

  1. Kernel type and version
  2. patch level
  3. disk partition and mount point
  4. installed software
  5. IP address
  6. routing table
  7. running processors
I tried to developed small script to get these information to one file. before do any change to the server, system administrator can run the script and collect information. 
#!/bin/bash
###################################################
#                                                 #
#      BACKUP SERVER INFO BEFORE PATCHING         #
#                      Version 1.0                #
#              Created Date 14/03/2012            #
#     Last Modified Date 14/03/2012               #
# INSTRUCTIONS-                                   #
# Log file will save on /home/admin directory     #
# file name is server_backup_info.<date>.log      #
# example - server_backup_info.14Mar2012.log      #
###################################################
echo "             Server Information" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "             ++++++++++++++++++">> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "HOSTNAME: " $(hostname) >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "DATE: " $(date) >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "=======================================================" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo " " >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "IP Address information" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "-------------------------------------------------------" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
ifconfig -a >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "_______________________________________________________" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "ROUTING TABLE" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "-------------------------------------------------------" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
netstat -rn >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "_______________________________________________________" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "Kernel Information" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "-------------------------------------------------------" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
uname -a >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "_______________________________________________________" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "Disk information" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "-------------------------------------------------------" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
cat /etc/fstab >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "_______________________________________________________" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "Disk Usage" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "-------------------------------------------------------" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
df -h >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "_______________________________________________________" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "Boot Loader Information" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "-------------------------------------------------------" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
cat /boot/grub/menu.lst >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "_______________________________________________________" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "RPMs" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "-------------------------------------------------------" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
rpm -qa >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "_______________________________________________________" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "Running Processors" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "-------------------------------------------------------" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
ps -eaf >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "_______________________________________________________" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "How Long Server UP and Running" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "-------------------------------------------------------" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
uptime >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "_______________________________________________________" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "Patch Level" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "-------------------------------------------------------" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
cat /etc/SuSE-release >> /home/admin/server_backup_info.`date +%d%b%Y`.log
echo "_______________________________________________________" >> /home/admin/server_backup_info.`date +%d%b%Y`.log
exit 0

This script for SuSE Linux. For other Linux version require to do small changes. And sometimes you need more information to collect.If anyone wants modified script please send comment.

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

Tuesday, January 31, 2012

Expression Example

Here are some common expression which can use with if condition

Check Directory
-d file >> True if file is a directory
#!/bin/bash
if [ -d /home ]; then
echo "/home is directory"
else
echo "/home is not directory"
fi
=================================================================
Check the file is exists
-e file >>True if file exists.
#!/bin/bash 
if [ -e /home/admin/.bash_profile ]; 
then 
echo ".bash_profile exists" 
else 
echo ".bash_profile not exists" 
fi
==================================================================
Check the file is regular file
-f file >>True if file exists and is a regular file
#!/bin/bash 
if [ -f /etc/passwd ]; then  
echo "/etc/passwd file is regular file" 
else 
echo " /etc/passwd file is not regular file "
fi
-------------------------------------------------------------------------------------------------
#!/bin/bash 
if [ -f /dev/fd0 ] then 
echo "regular file" 
else 
echo "not regular file" 
fi
===================================================================
Check the file has symbolic link
-L file >>  True if file is a symbolic link

#!/bin/bash 
if [ -L /dev/cdrom ] then 
echo "symbolink link file" 
else 
echo "not symbolink link file" 
fi
===================================================================
Check read, write execute permission
-r file >> True file is readable
-w file >> True file is writeable
-x file >> True file is executable

#!/bin/bash 
if [ -r /etc/passwd ] then 
echo "file is readable" 
else 
echo "file is not readable" 
fi

====================================================================
Check new or old file
file1 -nt file2 >> True  file1 newer than file2
file1 -ot file2 >> True  file1 older than file2

#!/bin/bash 
if [ /home/admin/test1 -nt /home/admin/test2] then 
echo "file test1 is new" 
else 
echo "file test2 is new" 
fi
=====================================================================

Tuesday, January 17, 2012

What is Shebang?

What is Shebang?

All shell script writers using shebang. Shebang is
#!

When you start the script the first line should start with '#!' without quote. as example it looks like
#!/bin/bash

This mean is hereafter parameters and arguments are compatible with 'Bourne shell' or compatible shell.

Shebang was introduced by Dennis Ritchie at Bell Laboratories. It was added to BSD releases first but documented and came out to public with Version 7 Unix at 1979.

Some typical shebang lines:
#!/bin/sh — Execute the file using sh, the Bourne shell, or a compatible shell
#!/bin/csh — Execute the file using csh, the C shell, or a compatible shell
#!/usr/bin/perl -T — Execute using Perl with the option for taint checks
#!/usr/bin/ruby — Execute using Ruby
#!/usr/bin/python -O — Execute using Python with optimizations to code
#!/usr/bin/php — Execute the file using the PHP command line interpreter

*Some documents and books are refer shebang as hashbang

Monday, January 16, 2012

Add bulk users for Linux server

Another time wasting job for system administrator. I will try to automated this job. Same as previous script if you are not configured key authentication you have to provide password.

First 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,

when you execute the script, /etc/passwd file first name, last name and email address coming as comment

#!/bin/bash
###################################################
# #
# ADD BULK USERS FOR LINUX SERVERS #
# Version 1.3 #
# Created Date 20/12/2011 #
# Last Modified Date 22/12/2011 #
# INSTRUCTIONS- #
# Save script on /home/username/ directory #
# create user list as user_list.csv file and save #
# it same directory (ie. /home/username/) #
###################################################

USERSHELL="/bin/bash"
PRIVILAGE="group1, group2"
SUDO_CON="yes"
FILE_NAME="user_list.csv"

if [ $(id -u) -ne 0 ]
then
echo "########### You must be root to run this script! ###########"
exit 1
fi

for i in $(cat $FILE_NAME)
do
username=$(echo $i| cut -f1 -d',')
user_id=$(echo $i| cut -f2 -d',')
f_name=$(echo $i| cut -f3 -d',')
l_name=$(echo $i| cut -f4 -d',')
email=$(echo $i| cut -f5 -d',')

home_dir="/home/"$username"/"

isFileExits(){
ls $1 > /dev/null
[ $? -eq 0 ] && return $TRUE || return $FALSE
}
if ( ! isFileExits $FILE_NAME )
then
echo "########### user_list.csv file not found. This script and user_list.csv file should in same location ###########"
exit 2
fi

cp -p /etc/passwd /etc/passwd.`date +%d%b%Y` #make a copy of passwd file
cp -p /etc/passwd /etc/shadow.`date +%d%b%Y` #make a copy of shadow file


echo "Enter password you want to set for new users : "
read password

PASSWORD=$(perl -e 'print crypt($ARGV[0], "password")' $password)

isUserExits(){
grep $1 /etc/passwd > /dev/null
[ $? -eq 0 ] && return $TRUE || return $FALSE
}
if [ $(echo $i| cut -f6 -d',') = yes ];
then
if ( ! isUserExits $username )
then
/usr/sbin/useradd -m $username -u $user_id -c $f_name" "$l_name" "$email -d $home_dir -p $PASSWORD -G $PRIVILAGE
echo "<---------- user " $username "created ---------->"

else
echo "Username "$username" exists in /etc/passwd"
exit 2
fi

else
if ( ! isUserExits $username )
then
/usr/sbin/useradd $username -u $user_id -c $f_name" "$l_name" "$email -d $home_dir -p $PASSWORD -s $USERSHELL
echo "<---------- user " $username "created ---------->"
else
echo "Username "$username" exists in /etc/passwd"
exit 2
fi
fi
done
exit 0
Try this and give a feedback.

Friday, January 13, 2012

Change the password on other user in multiple Linux server

This is similar to previous script. I added few lines
#!/bin/bash
#####################################################
# #
# CHANGE PASSWORD ON REMOTE HOST #
# Version 1.2 #
# Created by Aruna #
# Created Date 21/12/2011 #
# Last Modified Date 22/12/2011 #
# INSTRUCTIONS- #
# Save both script and server_list.csv file in same #
# location #
#####################################################
host_name=""
file_name="server_list.csv"
echo -n "Enter your login ID : "
read login_id
echo -n "Enter username you want to change the password on the list of server : "
read username
echo -n " The username you entered is "$username". Is it correct (y/n)?"
read answer
if [ $answer = y ]
then
for i in $(cat $file_name);
do
hostname=$(echo $i| cut -f1 -d',')
ssh $login_id@$hostname "sudo passwd $username"
echo ">>>>>>>>> SUCCESSFULLY CHANGED THE PASSWORD >>>>>>>>>>"
done
else
echo "!!!!!!!!! your answer does not match to continue. Thank you!!!!!!!!!!!!"
exit 1
fi
exit 0

Here first you have to provide your login password to get sudo access

Change your own password in multiple Linux servers

All Unix administrations are searching the web to find out automation method to do their work. Most of the system administrator getting job as change password, creating users, deleting user or change ownership etc.. If you read basic Unix tutorial you can do these job without others support. Problem or pain comes when you have to repeat same thing on range of servers.

As example lets see you want to change password on one user on 100 servers. Well very easy...
login each server and execute 'passwd' command. :)

Here I try to automate this job. Anyway changing password is manual intervention. Here all servers required SSH enable and if you are not configured authentication keys you have login to each server
First you create list of servers and save it as server_list.csv (very easy when you use excel sheet)

then create script
#!/bin/bash
#####################################################
# #
# CHANGE PASSWORD ON REMOTE HOST #
# Version 1.2 #
# Created by Aruna #
# Created Date 21/12/2011 #
# Last Modified Date 22/12/2011 #
# INSTRUCTIONS- #
# Save both script and server_list.csv file in same #
# location #
#####################################################
host_name=""
file_name="server_list.csv"
echo -n "Enter username you want to change the password on the list of server : "
read username
echo -n " The username you entered is "$username". Is it correct (y/n)?"
read answer
if [ $answer = y ]
then
for i in $(cat $file_name);
do
hostname=$(echo $i| cut -f1 -d',')
ssh $username@$hostname 'passwd'
echo ">>>>>>>>> SUCCESSFULLY CHANGED THE PASSWORD >>>>>>>>>>"
done
else
echo "!!!!!!!!! your answer does not match to continue. Thank you!!!!!!!!!!!!"
exit 1
fi
exit 0
You have to know your current password to execute this script.