#!/bin/bash # Author: Joshua Bailey # Script: multiUserAdd # Summary: Adds multiple users from a # specified file. # Syntax: multiUserAdd # 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