Script to add multiple user account in Linux
mdltorre_at_gmail.com
Date: 09/18/05
- Next message: Adam McCarthy: "Re: Can Knoppix be installed permanently?"
- Previous message: Adam McCarthy: "Re: Trying to install Linux on a Win XP machine"
- Next in thread: Apostolos P. Tsompanopoulos: "Re: Script to add multiple user account in Linux"
- Reply: Apostolos P. Tsompanopoulos: "Re: Script to add multiple user account in Linux"
- Reply: chris-usenet_at_roaima.co.uk: "Re: Script to add multiple user account in Linux"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Adam McCarthy: "Re: Can Knoppix be installed permanently?"
- Previous message: Adam McCarthy: "Re: Trying to install Linux on a Win XP machine"
- Next in thread: Apostolos P. Tsompanopoulos: "Re: Script to add multiple user account in Linux"
- Reply: Apostolos P. Tsompanopoulos: "Re: Script to add multiple user account in Linux"
- Reply: chris-usenet_at_roaima.co.uk: "Re: Script to add multiple user account in Linux"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|