-Checks for certain binarys that are installed
-Creates a random password
-Interactivly lets you setup the users email | home | shell | name | nick
-Emails the user there username, password and domain (depending)
-Adds them to a list held in /root/userlist
The program was based off the adduser script found at osix.net, i modified it and my friend bob added the binary checking script to it.
This is a good bash script to learn some things off and to also modify for personal use. This is not an optomised script though
Enjoy
#!/bin/bash
#Add User, Password Generator, Emailer and List maker script :D
# by typedeaF;; Edited by UmInAsHoE
#Check we can run the script first.
function check() {
WHEREIS="";
CHMOD="";
RM="";
MAIL="";
GROUPADD="";
USERADD="";
AWK="";
echo ""
echo "Searching for dependancies:"
sleep 3
xi=`whereis whereis`;
if [ "x$xi" = "x" ]; then
echo " [!] Whereis was unable to find itself";
exit 1;
else
xm=0;
for xl in $xi; do
xm=`expr $xm + 1`;
if [ "x$xm" = "x2" ]; then
WHEREIS="$xl";
fi
done
if [ ! -x $WHEREIS ]; then
echo " [!] Whereis can be executed but not found?";
exit 1;
fi;
fi;
AWK=`$WHEREIS awk `;
xj=0;
for xk in $AWK; do
xj=`expr $xj + 1`;
if [ "x$xj" = "x2" ]; then
AWK="$xk";
fi
done;
if [ ! -x $AWK ]; then
echo " [!] Awk could not be located. Broken System?";
exit 1;
fi
USERADD=`$WHEREIS useradd | $AWK '{print $2}' - `;
if [ "x$USERADD" = "x" ]; then
echo " [!] Unable to find useradd. Broken Environment?";
exit 1;
fi
GROUPADD=`$WHEREIS groupadd | $AWK '{print $2}' - `;
if [ "x$GROUPADD" = "x" ]; then
echo " [!] Unable to find groupadd. Broken Environment?";
exit 1;
fi
MAIL=`$WHEREIS mail | $AWK '{print $2}' - `;
if [ "x$MAIL" = "x" ]; then
echo " [!] Unable to find mail. Broken Environment?";
exit 1;
fi
RM=`$WHEREIS rm | $AWK '{print $2}' - `;
if [ "x$RM" = "x" ]; then
echo " [!] Unable to find Rm. Broken Environment?";
exit 1;
fi
CHMOD=`$WHEREIS chmod | $AWK '{print $2}' - `;
if [ "x$CHMOD" = "x" ]; then
echo " [!] Unable to find chmod. Broken Environment?";
exit 1;
fi
}
# Make a semi-random password using an array of user-friendly characters
function mkpass() {
PASS=""
PASSLEN=8
array1=( q w e r t y u i o p a s d f g h j k l z x c v b n m
Q W E R T Y U I O P A S D F G H J K L Z X C V B N M
1 2 3 4 5 6 7 8 9 0 \! \@ \# \$ \% \^ \& \*
)
MODNUM=${#array1[*]}
count=0
while [ ${count:=0} -lt $PASSLEN ]
do
number=$(($RANDOM%$MODNUM))
PASS="$PASS""${array1[$number]}"
((count++))
done
echo $PASS
}
# center the text on the screen by finding the horizontal center with max_lines
# max_lines divided by two becomes the center line(aka. line_num)
# use the max lenght of the line divided in half to find the center column
# subtract half the output string from the center to create the offset to indent
function center_txt() {
PROMPT="$1"
str_len=${#PROMPT}
indent=$(( ((max_cols / 2)) - ((str_len / 2)) ))
line_num=$(( max_lines / 2 ))
tput cup ${line_num} ${indent}
echo "$PROMPT"
}
# indent_txt is intended to be used AFTER center_txt
# indent_txt just prints on the current line and centers the text horizontally
# line_num is global, so main keeps track of incrementing it
function indent_txt() {
PROMPT="$1"
str_len=${#PROMPT}
indent=$(( ((max_cols / 2)) - ((str_len / 2)) ))
tput cup ${line_num} ${indent}
echo "$PROMPT"
}
# mk_prompt creates an input prompt for the the read function
# 'read -p "==> " variable' would work, but I like this better
function mk_prompt() {
((line_num+=2))
tput cup $line_num 15
PROMPT="==> "
echo -n "${blink}${PROMPT}"
echo -n ${offblink}
}
# This function gathers all the information needed to generate the useradd string
function mkuser() {
USERNAME=""
FULLNAME=""
OTHER=""
GROUP=""
HOME=""
SHELL=""
ADDY=""
echo ${term_clr}
PROMPT="Enter new users E-mail Address"
center_txt "$PROMPT"
((line_num++))
PROMPT="(ex. test@test.com or 0 for none)"
indent_txt "$PROMPT"
mk_prompt
read ADDY
echo ${term_clr}
PROMPT="Enter the new Login account user name"
center_txt "$PROMPT"
((line_num++))
PROMPT="(ex. xxxxN00b)"
indent_txt "$PROMPT"
mk_prompt
read USERNAME
echo ${term_clr}
PROMPT="Enter the users Full Name"
center_txt "$PROMPT"
((line_num++))
PROMPT="(ex. John Doe)"
indent_txt "$PROMPT"
mk_prompt
read FULLNAME
echo ${term_clr}
PROMPT="Select the users Shell"
center_txt "$PROMPT"
((line_num++))
PROMPT="(ex. type \"1\" for Bash)"
indent_txt "$PROMPT"
((line_num+=2))
indent=$(( ((max_cols / 2)) - 6 ))
tput cup $line_num $indent; ((line_num++)); echo "1) Bash"
tput cup $line_num $indent; ((line_num++)); echo "2) Other"
mk_prompt
read REPLY
case "$REPLY" in
1)
SHELL="$BASH"
;;
2)
echo ${term_clr}
PROMPT="Enter a Shell Location for $USERNMAE"
center_txt "$PROMPT"
((line_num++))
PROMPT="(ex. $BASH)"
indent_txt "$PROMPT"
mk_prompt
read SHELL
;;
* )
echo "Invalid choice"
;;
esac
echo ${term_clr}
PROMPT="Select the users Primary Group"
center_txt "$PROMPT"
((line_num++))
PROMPT="(ex. type \"1\" for Their Username)"
indent_txt "$PROMPT"
((line_num+=2))
indent=$(( ((max_cols / 2)) - 6 ))
tput cup $line_num $indent; ((line_num++)); echo "1) Thier Username"
tput cup $line_num $indent; ((line_num++)); echo "2) Other"
mk_prompt
read REPLY
case "$REPLY" in
1)
GROUP="$USERNAME"
HOME="/home/$USERNAME"
;;
2)
echo ${term_clr}
PROMPT="Enter a Group Name for $USERNMAE"
center_txt "$PROMPT"
((line_num++))
PROMPT="(ex. Wheel)"
indent_txt "$PROMPT"
mk_prompt
read OTHER
GROUP="$OTHER"
HOME="/home/$USERNAME"
;;
* )
echo "Invalid choice"
;;
esac
}
# Just reset the terminal to normal
function _quit() {
reset -Q
echo "Exiting Add User Script. Bye"
exit 0
}
# =================================================#
# Begin Main #
# =================================================#
#lets check we can execute script
check
ROOT_UID=0
if [ "$UID" -eq "$ROOT_UID" ]; then
# These are the tput commands used to terminal output formatting
term_clr=$(tput clear)
max_lines=$(tput lines)
max_cols=$(tput cols)
blink=$(tput blink)
offblink=$(tput sgr0)
let CONTINUE=0
# Set up a trap to handle control signals. If you start using temp files then
# this is where you would want to clean up.
trap _quit INT TERM QUIT
ADD=0
while [ $CONTINUE -eq 0 ]
do
if [ "$ADD" = 0 ]; then
mkuser
mkpass
echo ${term_clr}
groups | grep -iw $GROUP
if [ `echo $?` = "0" ]; then
echo ${term_clr}
PROMPT="User with the following criteria"
center_txt "$PROMPT"
((line_num+=2))
PROMPT="groupadd $GROUP"
indent_txt "$PROMPT"
((line_num+=2))
PROMPT="useradd -d $HOME -p $PASS -g $GROUP -s $SHELL $USERNAME"
indent_txt "$PROMPT"
((line_num+=2))
uline=$(tput smul)
offuline=$(tput rmul)
PROMPT="Password: $PASS"
ADD=1
str_len=$(( ${#PROMPT} + 8 ))
indent=$(( ((max_cols / 2)) - ((str_len / 2)) ))
tput cup ${line_num} ${indent}
echo -n "${uline}$PROMPT${offuline} "
tput cup $max_lines
PROMPT="Press Return to continue and Add User. Press ^C (ctrl + c) to EXIT."
echo -n "${blink}${PROMPT}"
echo -n ${offblink}
read -s
else
echo ${term_clr}
PROMPT="User and Group with the following criteria"
center_txt "$PROMPT"
((line_num+=2))
PROMPT="groupadd $GROUP"
indent_txt "$PROMPT"
((line_num+=2))
PROMPT="useradd -d $HOME -p $PASS -g $GROUP -s $SHELL $USERNAME"
indent_txt "$PROMPT"
((line_num+=2))
uline=$(tput smul)
offuline=$(tput rmul)
PROMPT="Password: $PASS"
ADD=1
str_len=$(( ${#PROMPT} + 8 ))
indent=$(( ((max_cols / 2)) - ((str_len / 2)) ))
tput cup ${line_num} ${indent}
echo -n "${uline}$PROMPT${offuline} "
tput cup $max_lines
PROMPT="Press Return to continue and Add User and Add Group. Press ^C (ctrl + c) to EXIT."
echo -n "${blink}${PROMPT}"
echo -n ${offblink}
ADD=1
read -s
fi
elif [ "$ADD" = 1 ]; then
#Add the group
groupadd $GROUP
if [ "$ADDY" = "0" ]; then
# Create the user and add the user to the list with no email
ADDUSER="################################\n Username = $USERNAME\n Password = $PASS\n FullName = $FULLNAME\n Email = None\n
Shell = $SHELL\n Group = $GROUP\n Home DIR = $HOME\n################################\n\n"
echo -e $ADDUSER >> /root/userlist
chmod 600 /root/userlist
useradd -d $HOME -p $PASS -g $GROUP -s $SHELL $USERNAME
mkdir $HOME
chown $USER:$GROUP $HOME
chmod 700 $HOME
ADD=0
else
# Create the user, add them to the list and email them
EMAIL="Blah blah blah blah \n\n Server = place.home.net\n Username = $USERNAME\n
Password = $PASS\n\nblah blah blahb blah\n\nThank You\n\nYou You You\n"
echo -e $EMAIL >> /root/email
chmod 600 /root/email
ADDUSER="################################\n Username = $USERNAME\n Password = $PASS\n FullName = $FULLNAME\n Email = $ADDY\n
Shell = $SHELL\n Group = $GROUP\n Home DIR = $HOME\n################################\n\n"
echo -e $ADDUSER >> /root/userlist
chmod 600 /root/userlist
useradd -d $HOME -p $PASS -g $GROUP -s $SHELL $USERNAME
mkdir $HOME
chown $USER:$GROUP $HOME
chmod 700 $HOME
cat /root/email | mail -s "User Added to Server" $ADDY
rm /root/email
ADD=0
fi
else
echo "Error!!!"
fi
done
else
echo "You Need To Be Root!"
fi
# reset your signal traps
trap - INT TERM QUIT
exit 0












