|
I have modified the above script to add some error checking and (a little bit more complex to read:-). I hope this is the last time that I have to modify the script, but there is always room for improvement. If you want to use this script to create multiple accounts this is the script for you. It is best used when you have to create multiple users (more than 20 for multiple systems more than 20). The great thing I think this script will tell you if the groups exist or not in the log file /tmp/$HOSTNAME.log. You can also add to the script to delete the log file when it's done.
As I stated before, if you could use the script please do so.
#!/bin/bash
TDATE=$(date)
if [ -e "/tmp/$HOSTNAME.log" ]; then
echo > "/tmp/$HOSTNAME.log";echo "[ $TDATE ]" >> "/tmp/$HOSTNAME.log";echo >> "/tmp/$HOSTNAME.log"
else
touch > "/tmp/$HOSTNAME.log"; echo "[ $TDATE ] " >> "/tmp/$HOSTNAME.log";echo >> "/tmp/$HOSTNAME.log"
fi
while read line
do
groupchk()
{
NWGRP=$(echo $SECGROUP | sed 's/,/\ /g')
for i in $NWGRP
do
GRGROUP=$(grep $i /etc/group | awk -F ":" '{print $1}')
if [ a$i == a$GRGROUP ];then
continue
else
echo "group $i not found!! User account $TRLOWER was not created on server: $RANSERVER " >> "/tmp/$HOSTNAME.log"
return -1
fi
done
useradd -u$USERUID -g$PRGROUP -G$SECGROUP -c"$NAME" -s$USRSHELL $JAMESBOND;usermod -p '$1$vFyyoyM9$5/YOVaoJnp18v8yxjRoja1' $JAMESBOND;chage $CHGCOM $JAMESBOND
}
SERVERNAME=$(echo $line | cut -f1 -d: )
USERUID=$(echo $line | cut -f2 -d: )
NAME=$(echo $line | cut -f3 -d: )
PRGROUP=$(echo $line | cut -f4 -d: )
SECGROUP=$(echo $line | cut -f5 -d: )
JAMESBOND=$(echo $line | cut -f6 -d: )
TRLOWER=$(echo $JAMESBOND | tr 'A-Z' 'a-z')
RANSERVER=$(uname -n | awk -F "." '{print $1}')
if [ "$SERVERNAME" = "$RANSERVER" ]; then
USRSHELL=/bin/ksh
USRSHELL2=/bin/bash
CHGCOM="-d0 -m0 -M90 -I90"
USERACCT=$(grep -i $JAMESBOND /etc/shadow | awk -F ":" '{print $1}')
PRGRPEXIST=$(grep -i $PRGROUP /etc/gshadow | awk -F ":" '{print $1}')
if [ "a$TRLOWER" != "a$USERACCT" ] && [ -z "$SECGROUP" ] && [ ! -z "$PRGROUP" ];then
useradd -u$USERUID -g$PRGROUP -c"$NAME" -s$USRSHELL $JAMESBOND;usermod -p '$1$vFyyoyM9$5/YOVaoJnp18v8yxjRoja1' $JAMESBOND;chage $CHGCOM $JAMESBOND
elif [ -z "$PRGRPEXIST" ];then
echo "User Account $TRLOWER was not created!! Primary group $PRGROUP does not exist on the server." >> "/tmp/$HOSTNAME.log"
elif [ "a$TRLOWER" = "a$USERACCT" ]; then
echo "The user account $TRLOWER exists on server: $SERVERNAME" >> "/tmp/$HOSTNAME.log"
else
groupchk $SECGROUP
fi
else
echo >> "/tmp/$HOSTNAME.log"
echo "The user account $JAMESBOND will not be created on server: $RANSERVER" >> "/tmp/$HOSTNAME.log"
fi
done <<+
FamilyGuy:3546:Hank Hill:cmd::hill546
FamilyGuy:1234:Bart Simpson:cmd:linux,expert:simpson123
FamilyGuy:2341:Peter Griffin:::griffin674
+
|