Here is the script for adding new local user to the Solaris. I commented it well, but here is again my idea:
#!/bin/sh #set -x # ---------------------------------------------------- # CHECKS: is user root, did you type arguments # ----------------------------------------------------- # Check if user is root if [ `/usr/bin/id | awk '{print $1}' | cut -d= -f2 | cut -d\( -f1` != 0 ] then echo ; echo Sorry, you have to be root to run this script. ; echo exit 1 fi # Check if 2 arguments are provided, first and last name if [ $# != 2 ] then echo ; echo Usage: `basename $0` Last_name First_name ; echo exit 1 fi # --variables SYSADM=sysadmin@domain.com # for GCOS-FIELD FNAME=`echo $2` LNAME=`echo $1` # ------------------------------------------------------ # FUNCTIONS # ------------------------------------------------------ # ----- Function to create 2 digits random number rand2dig () { # function srand() sets new seed for random numbers RN=`nawk ' BEGIN { srand(); print rand() }'` # multiple with 100 and use int() to get 2 digit number IN=`(expr ${RN}*100) | bc -l | nawk '{print int($0)}'` } # ---- Function to create user, change passwd, add dir adding_user() { HOMEDIR=/export/home PRIGROUP=staff USERSHELL=/usr/bin/tcsh # add a user useradd -c "${FNAME} ${LNAME}" -d ${HOMEDIR}/${USERNAME} -m -g ${PRIGROUP} -s ${USERSHELL} ${USERNAME} > /dev/null \ || exit 1 # Generate 8 alphanumeric random password #PASSWORD=`cat /dev/random | tr -dc "[a-z][A-Z][0-9]" | fold -w 8 | head -1` # You can use mkpasswd utility, check man mkpasswd PASSWORD=`mkpasswd -p /usr/bin/passwd -l 8 ${USERNAME}` || exit 2 # force user to change password during first login /usr/bin/passwd -f ${USERNAME} > /dev/null || exit 3 # send email to sys admin and user echo "New Local User added to ${HOST}: ${USERNAME} \nInitial password : ${PASSWORD} \ \nGCOS : ${FNAME} ${LNAME} \nHome dir : ${HOMEDIR}/${USERNAME} \nLogin shell : ${USERSHELL} \ \n-=-=-=-=-=-=-=-=-=-=-=-=-=-= \ \nNote: After entering the initial password, you'll be prompted to change the same." \ | mailx -s "New user : ${USERNAME}" ${SYSADM} ${USERNAME} } # ----------------------------------------------------- # MAIN # ---------------------------------------------------- # connect last and first name # use nawk function tolower() to translate uppercase characters to lowercase LAST=`echo $1 | nawk '{ print tolower($0) }` FIRST=`echo $2 | nawk '{ print tolower($0) }` FULLNAME=${LAST}${FIRST} # awk function length() determines lenght of string LENGHTFN=`echo ${FULLNAME} | nawk '{ print length($0) }'` if [ ${LENGHTFN} -lt 8 ] then # If lenght < 8 then this is username USERNAME=${FULLNAME} # Check if username already exists in /etc/passwd file if [ "${USERNAME}" = "`cat /etc/passwd | nawk -F: '{print $1}' | gegrep -w ${USERNAME}`" ] then if [ `echo ${USERNAME} | nawk '{ print length($0) }'` -eq 7 ] then rand2dig ; USERNAME=`echo ${USERNAME} | nawk '{ print substr($0,1,6) }'`${IN} # check if randomly generated username exist if [ "${USERNAME}" = "`cat /etc/passwd | nawk -F: '{print $1}' | gegrep -w ${USERNAME}`" ] then echo "Randomly generated username exists. Run script again, exit now !!!" exit 1 fi echo Adding local account ${USERNAME} to ${HOST} ; adding_user elif [ `echo ${USERNAME} | nawk '{ print length($0) }'` -le 6 ] then rand2dig ; USERNAME=${USERNAME}${IN} # check if randomly generated username exist if [ "${USERNAME}" = "`cat /etc/passwd | nawk -F: '{print $1}' | gegrep -w ${USERNAME}`" ] then echo "Randomly generated username exists. Run script again, exit now !!!" exit 1 fi echo Adding local account ${USERNAME} to ${HOST} ; adding_user fi else # username doesn't exist in /etc/passwd file, so add it without modification echo Adding local account ${USERNAME} to ${HOST} ; adding_user fi else # if lenght >= 8 then use (only) first 8 character # use awk function substr() USERNAME=`echo ${FULLNAME} | nawk '{ print substr($0,1,8) }'` # Check if username already exists in /etc/passwd file if [ "${USERNAME}" = "`cat /etc/passwd | nawk -F: '{print $1}' | gegrep -w ${USERNAME}`" ] then # generate 2 digits random number ${IN} rand2dig USERNAME=`echo ${USERNAME} | nawk '{ print substr($0,1,6) }'`${IN} # check if randomly generated username exist if [ "${USERNAME}" = "`cat /etc/passwd | nawk -F: '{print $1}' | gegrep -w ${USERNAME}`" ] then echo "Randomly generated username exists. Run script again, exit now !!!" exit 1 fi echo Adding local account ${USERNAME} to ${HOST} ; adding_user else # username doesn't exist in /etc/passwd file, so add it without modification echo Adding local account ${USERNAME} to ${HOST} ; adding_user fi fi exit 0 |