Shell Script to crate user from old server to new server.
1) copy /etc/passwd file from old server to new server password.txt in same location where you execute same script
shell script
#!/bin/bash
# Script to add a user to Linux system
# -------------------------------------------------------------------------
groupadd -g 501 amco
#echo "Default Group Amco created"
usernamelist="`cat password.txt | cut -d ":" -f1 `"
#fullname="`cat password.txt |cut -d":" -f5`"
for i in $usernamelist
do
if [ $(id -u) -eq 0 ]; then
# read -p "Enter username : " username
#read -s -p "Enter password : " password
username="$i"
password="123456"
fullname="`grep -i $username password.txt | cut -d":" -f5`"
useruid="`grep -i $username password.txt | cut -d":" -f3`"
egrep -w "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "$username exists!"
# exit 1
else
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
useradd -m -u "$useruid" -G amco -c "$fullname" -p "$pass" -d /home/$username "$username"
[ $? -eq 0 ] && echo "User $username has been added to system!" || echo "Failed to add a user $username!"
#`chage -d 0 $username`
fi
else
echo "Only root may add a user to the system"
exit 2
fi
done
# Script to add a user to Linux system
# -------------------------------------------------------------------------
groupadd -g 501 amco
#echo "Default Group Amco created"
usernamelist="`cat password.txt | cut -d ":" -f1 `"
#fullname="`cat password.txt |cut -d":" -f5`"
for i in $usernamelist
do
if [ $(id -u) -eq 0 ]; then
# read -p "Enter username : " username
#read -s -p "Enter password : " password
username="$i"
password="123456"
fullname="`grep -i $username password.txt | cut -d":" -f5`"
useruid="`grep -i $username password.txt | cut -d":" -f3`"
egrep -w "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "$username exists!"
# exit 1
else
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
useradd -m -u "$useruid" -G amco -c "$fullname" -p "$pass" -d /home/$username "$username"
[ $? -eq 0 ] && echo "User $username has been added to system!" || echo "Failed to add a user $username!"
#`chage -d 0 $username`
fi
else
echo "Only root may add a user to the system"
exit 2
fi
done