Tuesday, November 18, 2014

script add new user on centos


---------------------------adduser.sh----------------------------------------------------
#!/bin/bash
# Script to add a user to Linux system

if [ $(id -u) -eq 0 ]; then
        read -p "Enter realname : " realname
#       read -p "Enter company : " company
        read -p "Enter username : " username
        read -s -p "Enter password : " password
        egrep "^$username" /etc/passwd >/dev/null
        if [ $? -eq 0 ]; then
                echo "$username exists!"
                exit 1
        else
                sender=email@mydomain.com
                pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
                useradd -p $pass $username -c "$realname" -m -g group -s /sbin/nologin
                [ $? -eq 0 ] && echo "User has been added to system!" && echo | mail -s "Welcome new email, Do not reply mail" < user.txt mooy@mydomain.com -c image@mydomain.com -- -f $sender  || echo "Failed to add a user!"
        fi
else
        echo "Only root may add a user to the system"
        exit 2
fi

-------------------------------------------------------------------------------

----------------------------------user.txt------------------------------------
Hi
You email address: mooy@mydomain.com
password: m12345


By.
Auto Generate mail.
-----------------------------------------------------------------------------

No comments: