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/bashTry this and give a feedback.
###################################################
# #
# 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
No comments:
Post a Comment