Thursday, February 9, 2012

Shell Script To Add User, Password And Add User To Sudo

Here's the Shell Script which required 2 parameters 1) UserName 2) Password.....

After setting up user name and password, script will ask you if you want to add user to Super User / Sudo or not....




#!/bin/bash
UCOM="/usr/sbin/useradd"
PCOM="/usr/bin/passwd"
DCOM="/usr/sbin/userdel"
GCOM="/bin/grep"

if [ "$#" != "2" ]
then
        echo -e "\033[33m Required 2 Parameters : User Name and Password..."
        exit;
else
        $UCOM $1
        ( echo $2; echo $2 ) | $PCOM $1

        if [ "$?" != "0" ]
        then
                $DCOM -r $1
        else
                echo -e "\033[35m User $1 Successfully Added To System"
                echo -e "\033[0m"
        fi
fi

echo -e "\033[33m Do You Want To Add User to Sudoers/Super User List (y/n)"
read choice
echo -e "\033[0m"

if [ "$choice" == "y" -o "$choice" == "Y" ]
then
        $GCOM $1 /etc/sudoers

        if [ "$?" == "0" ]
        then
                echo -e "\033[31m User $1 Already Added To Sudoers File"
                echo -e "\033[0m"
        else
                echo "$1   ALL=(ALL)       ALL" | cat >> /etc/sudoers
                echo -e "\033[35m User $1 Successfully Added To Sudoers!!!"
                echo -e "\033[0m"
        fi

elif [ "$choice" == "n" -o "$choice" == "N" ]
then
        echo -e "\033[35m $1 User Not Added To Sudoers!!!"
        echo -e "\033[0m"
else
        echo -e "\033[37m Invalid Choice....User Not Added To Sudoers :-("
        echo -e "\033[0m"
fi




That's it!!!


Please use and update your responses......waiting for your responses :)

1 comment: