Friday, October 07, 2005

useradd/passwd script

RE: useradd/passwd script

I found this script that might work for you. I haven't tried it
myself. I hope it helps.

Here is the link for this script:

http://groups.google.com/group/comp.os.linux.misc/msg/b2867efd801e77ab?dmode=source&hl=en

#!/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 <email@removed> $EMAIL

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

fi
done

0 Comments:

Post a Comment

<< Home