#!/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 ma...@yahoo.com $EMAIL
# Log the results
echo "$USERNAME:$FULLNAME:$PASSWORD:$CLASS:`date`" >>
users_created_log
fi
done
An alternative: check if $(id -u) is equal to 0 so you can permit
another user with root privileges to run the script.
Apostolos
--
Replace earth.space with gmail.com for a valid e-mail
AIUI the "portable" approach to this is to use terminfo ("tput setaf 1"
for red text, etc.) to generate terminal independent colour sequences.
However, I don't have many non-ANSI colour terminals to try this on.
> cat $USR_FILE | while read TEMP
> do
> if [ -n "$TEMP" ]; then
> echo "$TEMP" >> /tmp/buffer
> fi
> done
You're reading from a single file, so don't need the cat here. Try this
instead:
while IFS= read TEMP
do
...
done < "$USR_FILE"
You also have to remember that if you've assigned USR_FILE from a command
line parameter, it might have a space or other punctuation in it. It's
always safest to double-quote such variables.
> mail -s "Account Info" \
> -b ma...@yahoo.com $EMAIL
You seriously email a yahoo account with details of usernames and
passwords??
Chris
> You seriously email a yahoo account with details of usernames and
> passwords??
Thanks for your suggestions. The answer is NO WAY, this was just an
example.
Manuel.
mdlt...@gmail.com wrote:
> NO WAY, this was just an example.
That's a relief!
Chris
The use of temporary file here is unsafe, since its filename is
predictable. It becomes even more vulnerable as this script is run by
root. Better use "mktemp" to generate a random file name under /tmp.
Otherwise the script is well written and commented, I'd say.
Thanks for sharing it.
KB
--
Windoze is not the solution; it is the problem -- and the solution is NO.