Register
It is currently Fri Nov 28, 2014 12:51 am

adds multiple users from a file


All times are UTC - 6 hours


Post new topic Reply to topic  [ 1 post ] 
Author Message
 PostPosted: Tue May 17, 2005 7:42 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
was originally posted here at usalug.org

here it is....thanks to the collaborated help of 25ft and geeshock :)
but first...an example of the file it will read from (it must match the syntax to work)
Code:
example1:password1:group1:fullname1
example2:password2:group2:fullname2
slick:ghost:users:Slick
username:password:group:Full name

must be like that and colon delimited
if the group you specify doesn't exist...it will create it
if the home directory doesn't exist for the user...it will create one
ofcourse you can take and change it to suit your own needs
Code:
#!/bin/bash
# Author: Joshua Bailey
# Script: multiUserAdd
# Summary: Adds multiple users from a
#      specified file.
# Syntax: multiUserAdd <fileToAddFrom>
# Syntax of fileToAddFrom: "UserName:Password:GroupName:FullName"

if [[ ! `id -u` = 0 ]]
then
   echo "*** Error:: you must be root to use this ***"
   exit
fi

if [[ ! $# > 0 ]]
then
   read -p "Please enter the file you want to use (/path/to/filename): " fileTo

elif [[ $# == 1 ]]
then
   fileTo=$1
fi

if [[ ! -e $fileTo ]]
then
   echo "*** Error:That file does not exist! ***"
fi

((recCount=0))

# until there are no more records in the file
# loop
until ! read Record
do
   ((recCount++))
   errorFlag=n

   # put each line into a file called tempRecord
   # it gets written over ever instance of the loop
   echo $Record > tempRecord

   # cut up the record within the file at each instance
   # assign each variable the part you chopped
   group=`cut -d: -f3 tempRecord`
   name=`cut -d: -f4 tempRecord`
   password=`cut -d: -f2 tempRecord`
   un=`cut -d: -f1 tempRecord`
   
   # some very very basic validation checking (not finished)
   if [[ ${#group} == 0 ]]
   then
      errorFlag=y
      echo "*** Error:Missing group for record $recCount ***"
      errorMsg="$errorMsg:missing group"
      #echo "Record $recCount:$Record - Missing group" >> errorReport
   else   

      # see if the group exists...if not create it
      # first seperate the groups from the group file
      cut -d: -f1 /etc/group > tempGroup
   
      # check each group to see if it matches the group we want
      until ! read Record
      do
         # if the group matches what we want...set our flag
         if [[ $Record != $group ]]
         then
            gmflag=1
         else
            gmflag=0
         fi

      done < tempGroup   
   
      # if our flag was set...we need to create the group
      if [[ $gmflag == 1 ]]
      then
         groupadd -r $group
      fi

   fi
   if [[ ${#name} == 0 ]]
   then
      errorFlag=y
      echo "*** Error:Missing fullname for record $recCount ***"
      errorMsg="$errorMsg:missing fullname"
      #echo "Record $recCount:$Record - Missing fullname" >> errorReport
   fi

   if [[ ${#un} == 0 ]]
   then
      errorFlag=y
      echo "*** Error:Missing username for record $recCount ***"
      errorMsg="$errorMsg:missing username"
      #echo "Record $recCount:$Record - Missing username" >> errorReport
   fi

   if [[ ${#password} == 0 ]]
   then
      errorFlag=y
      echo "*** Error:Missing password for record $recCount ***"
      errorMsg="$errorMsg:missing password"
      #echo "Record $recCount:$Record - Missing password" >> errorReport
   fi

   if [[ $errorFlag == y ]]
   then
      echo "Record $recCount in file $fileTo - $Record: - $errorMsg" >> errorReport
   else
   
   # encrypt the password for useradd
   if [[ ${#password} > 0 ]]
   then
      # this beautiful line of code came from 25ft
      # without this...the automation wouldn't work
      # you can thank him later
      newPass=`openssl passwd -1 $password`
   fi

   # -g sets group, -d specifies default login directory, -m forces the making of the
   # directory if it doesn't exist
   useradd -g $group -p $newPass -d /home/$un -c $name -m $un

   # if the above command ran right...
   if [[ $? == 0 ]]
   then
      # tell us
      echo "*** User created successfully ***"

      # begin creating the report
      echo -n "$name:$un:" >> reportFile

      # get the userid and groupid of each user
      userid=`id -u $un`
      groupid=`id -g $un`
      echo -n "$userid:$groupid:" >> reportFile

      # add the date that all this was performed
      cur_date=`date "+%B%e %Y"`
      echo $cur_date >> reportFile
   fi
   fi
done < $fileTo

# delete the temporary file created for each record
rm -f tempRecord
rm -f tempGroup

i hope this helps someone out there :)


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


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