Script to add multiple user account in Linux

mdltorre_at_gmail.com
Date: 09/18/05


Date: 17 Sep 2005 19:41:33 -0700

Here is my shell script to add multiple user account (batch mode) in my
linux box. I hope you like it, and if you have any suggestions please
let me know.

#!/bin/sh

## SCRIPT TO ADD MULTIPLE USERS TO A LINUX SYSTEM
##
## The script will add users, generate secure password and mail
## info to the users. Also a log file is made!
##
## You need to make it work:
##
## mailx - traditional command-line-mode mail user agent
## pwgen - password generator
## http://sourceforge.net/projects/pwgen/
##
## user_list format: USERNAME NAMES LASTNAME CLASS EMAIL
##
## (c) 2005 Manuel de la Torre
##

# Modify this variables if you need

MINDAYS=0 # Change password at anytime
MAXDAYS=45 # Max days password is valid
WARNDAYS=10 # Warning message before expire passwd
EXPDAYS=180 # Days to expire account from now
INACTIVE=45 # Days to lock after passwd expires

# Calculte days from Epoch
YEARS_FROM_EPOCH="$((($(date +%G) - 1970 ) * 365 ))"
DAYS_THIS_YEAR="$((($(date +%j))))"
DAYS_FROM_EPOCH=$(( $YEARS_FROM_EPOCH + $DAYS_THIS_YEAR + 8 ))

# Define some colors first:
red='\e[0;31m'
RED='\e[1;31m'
blue='\e[0;34m'
BLUE='\e[1;34m'
cyan='\e[0;36m'
CYAN='\e[1;36m'
NC='\e[0m' # No Color

# Ensure that root is running the script
WHOAMI=`/usr/bin/whoami`
if [ $WHOAMI != "root" ]; then
  echo "Sorry. You must be root to add new users"
  exit 1
fi

# Ensure proper format of the command

thiscmd=`basename $0`

if [ "$#" -ne 1 ]; then
  echo "USAGE: $thiscmd user_file" && exit 1
fi

USR_FILE=$1

# Remove blank lines from input file
# Used this solution because of problems
# with the IFS in a if [ -n ] statement
#

# Check if buffer file exist, then remove

if [ -a /tmp/buffer ]
  then
  rm /tmp/buffer

fi

# Read input file, and delete blank lines

cat $USR_FILE | while read TEMP

  do
    if [ -n "$TEMP" ]; then
      echo "$TEMP" >> /tmp/buffer
    fi
  done

# Copy temporal file to input file

cp /tmp/buffer $USR_FILE
rm /tmp/buffer

#
# Save the current value of the IFS
ifs="$IFS"

# Define the separator (TAB) between fields
# if your input has tabs between fields
#IFS=`echo t | tr t '\t'`

# Define the separator (COMMA) between fields
# if your input has spaces between fields
IFS=","

# assumning the file has one line per user, in a layout like:
#
# USERNAME NAMES LASTNAME CLASS EMAIL
#

# Configure the useradd program globaly:
# useradd -D -b $DEF_HOME -e $EXPIRE -g $GROUP

cat $USR_FILE | while read USERNAME NAMES LASTNAME CLASS EMAIL

do

  USERNAME=`echo $USERNAME | tr A-Z a-z` #lower case
  FULLNAME="$NAMES $LASTNAME"
  COMMENT="$FULLNAME,$CLASS"

  # Check if users exists in system

  NOEXISTE=`cut -d: -f1 /etc/passwd | grep -i $USERNAME`

 if [ -n "$NOEXISTE" ]; then
    echo -e "Creating user $USERNAME: \t ${RED}FAILED${NC}"
 else
    # Some output to keep you happy
    echo -e "Creating user $USERNAME: \t ${CYAN}SUCCESS${NC}"

  # Add the user

  useradd $USERNAME \
       -c "$COMMENT" \
       -m

  # Set the initial password

  PASSWORD=`pwgen -s`
  echo $USERNAME:$PASSWORD | chpasswd

  # Change expitation of passwords

  chage -m $MINDAYS \
        -M $MAXDAYS \
        -E $(( $EXPDAYS + $DAYS_FROM_EPOCH )) \
        -I $INACTIVE \
        -d 0 $USERNAME

  # Mail password

  echo -e "login: $USERNAME \npassw: $PASSWORD" | \
     mail -s "Account Info" \
          -b mail@yahoo.com $EMAIL

  # Log the results
  echo "$USERNAME:$FULLNAME:$PASSWORD:$CLASS:`date`" >>
users_created_log

 fi
done



Relevant Pages

  • Re: CTRL-M problem
    ... > I have a script running fine in Win32 environment. ... > script in Linux, ... > When i run dos2unix on input file and run the tclsh script, ...
    (comp.lang.tcl)
  • Re: Will Linux become as vulnerable as MS ??
    ... > beeing vulnerable to viruses. ... > that they know are executable, and execute intentionally. ... >> Linux, each distro is a little different, and even within the distro, ... > Since clicking on a script is easier than typing it's name, ...
    (comp.os.linux.security)
  • wonk mode
    ... workable Linux system for my home computer, ... MEPIS release (replacing a much older "Fedora" ... Red Hat Package Monitor formatted software archives ... This little dandy script, line wrapped ...
    (comp.os.linux.misc)
  • Re: Compiling gnuplot with libgd
    ... of running this script. ... installing the gnuplot graphing program ... By building a package for damn small linux I can have it automatically ... install gnuplot and run my required script without having to customize ...
    (comp.graphics.apps.gnuplot)
  • Re: Really Baffled any clues as to what is going on?
    ... > I am working on a script, it has an input file of machine names. ... > Dim fstrLogFilePath ... > fstrInputFile = InputBox("Enter name of file containing machines to ...
    (microsoft.public.scripting.vbscript)