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.