Register
It is currently Mon Dec 22, 2014 3:30 am

Script to create user accounts!!!


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Thu Nov 13, 2008 4:37 pm   

Joined: Thu Oct 16, 2008 3:05 pm
Posts: 13
I have created this script to create user accounts on a server based on using a satellite server. If you can use this script in your environment, you can.



#!/bin/bash

TDATE=$(date)

if [ -e "/tmp/$HOSTNAME.log" ]; then

echo > "/tmp/$HOSTNAME.log";echo "[ $TDATE ] " >> "/tmp/$HOSTNAME.log";echo \n >> "/tmp/$HOSTNAME"
else

touch > "/tmp/$HOSTNAME.log"; echo "[ $TDATE ] " >> "/tmp/$HOSTNAME.log";echo \n >> "/tmp/$HOSTNAME"
fi

while read line
do


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: )
LOGINNAME=$(echo $line | cut -f6 -d: )

TRLOWER=$(echo $LOGINNAME | 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 $LOGINNAME /etc/shadow | awk -F ":" '{print $1}')

if [ "$TRLOWER" != "$USERACCT" ] && [ -z "$SECGROUP" ]; then

useradd -u$USERUID -g$PRGROUP -c"$NAME" -s$USRSHELL $LOGINNAME;usermod -p 'hashed password that you want to use' $LOGINNAME;chage $CHGCOM $LOGINNAME

elif [ "$TRLOWER" = "$USERACCT" ]; then
echo "The user account $TRLOWER exists on server: $SERVERNAME" >> /tmp/$HOSTNAME.log

else

useradd -u$USERUID -g$PRGROUP -G$SECGROUP -c"$NAME" -s$USRSHELL $LOGINNAME;usermod -p 'hashed password that you want to use' $LOGINNAME;chage $CHGCOM $LOGINNAME

fi

else
echo >> "/tmp/$HOSTNAME.log"
echo "The user account $LOGINNAME does not need to be created on this server." >> /tmp/$HOSTNAME.log

fi
done <<+
servername:UID:FULL NAME:PRIMARY GROUP:SECONDARY GROUP:USERID
+


The script uses the here document format, and the script processes the tuple of data located after the first "<<+". You can use the script to create as many users as you need at 1 time by adding extra fields in the same order between the "+" signs.


Top
 Profile  
 PostPosted: Sat Nov 15, 2008 9:28 pm   

Joined: Thu Oct 16, 2008 3:05 pm
Posts: 13
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
+


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP