Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Script to add multiple user account in Linux

0 views
Skip to first unread message

mdlt...@gmail.com

unread,
Sep 17, 2005, 10:41:33 PM9/17/05
to
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 ma...@yahoo.com $EMAIL

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

fi
done

Apostolos P. Tsompanopoulos

unread,
Sep 18, 2005, 3:57:51 PM9/18/05
to
On Κυρ, 18 Σεπ 2005 at 02:41 GMT, mdlt...@gmail.com wrote:
> 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
> ##
> [... snipped ...]

>
> # 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

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

chris-...@roaima.co.uk

unread,
Sep 19, 2005, 4:49:00 AM9/19/05
to
mdlt...@gmail.com wrote:
> # 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

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

mdlt...@gmail.com

unread,
Oct 7, 2005, 1:53:49 AM10/7/05
to
chris-...@roaima.co.uk ha escrito:

> 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.

chris-...@roaima.co.uk

unread,
Oct 7, 2005, 4:56:16 AM10/7/05
to
> You seriously email a yahoo account with details of usernames and
> passwords??

mdlt...@gmail.com wrote:
> NO WAY, this was just an example.

That's a relief!
Chris

Kill Bill

unread,
Nov 10, 2005, 9:58:15 AM11/10/05
to
mdlt...@gmail.com wrote:
> # 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

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.

0 new messages