SUMMARY: Batch User Addition - LONG

From: Derald McMillan (
Date: Sat Feb 04 1995 - 02:20:42 CST

Sun Gurus,

  Looks like I wasn't alone in this one...I got a lot of help and a lot of
"me too's" on this subject. I ended up using all scripts in my efforts,
some of which were donated and some of which I wrote. I didn't mention this
earlier but I am working on a machine running Solaris 2.3 so the scripts
are specific to that version of the OS. However, all I use are the useradd
and password programs that come with solaris to create the user account, his/
her home directory and his/her password entry. I use username for the
comment field (Full Name) and generate a random password with the perl script.
So, you don't absolutely need the perl script if you don't want to do random
passwords. There is a short explanation before each script.

The scripts used include:
        csh (standard UNIX script)
        perl (not sure where to find this anymore...use archie)
        tcl (
               (tk is also at the above site)
        expect (

Tcl and expect compile virtually "out of the box" (I had to change my
compiler from cc to gcc for tcl). But perl takes a little work..below
are a few hints that John McHugh posted to the list late last month for
compiling PERL on Solaris 2.3.

   1. Take "/usr/ucb" out of your PATH.
   2. Do not use any BSD/UCB libraries.
   3. Only -lsocket, -lnsl and -lm are needed. (There is apparently
      a problem with -lmalloc also.)
   4. Do not use "-I/usr/ucbinclude".
   5. Do not use vfork.
   6. Hints for Solaris 2.0 are actually better than 2.1 .
   7. If you use cc, do not use the -O flag on util.c/tutil.c .

As always this code is submitted for your use without warranty of any kind
blah, blah, know the drill...USE AT YOUR OWN RISK!

And here are the scripts...

This first script is called pwgen and is a perl script given to me by Jim McLean-Lipinski
Network Administrator, Vermont Dept. of Public Safety. The script originally allowed
for colons (:), commas (,), etc as well, but I decided to take that part out and everything
still works ok.


# Build arrays of characters to be used



# Randomly shuffle arrays
for ($i = 1; $i<rand(100)+10; $i++){
        local (@TEMPORARY1);
        push(@TEMPORARY1, splice(@P1, rand(@P1), 1))
                while @P1;
        @P1 = @TEMPORARY1;

        local (@TEMPORARY2);
        push(@TEMPORARY2, splice(@P2, rand(@P2), 1))
                while @P2;
        @P2 = @TEMPORARY2;

        local (@TEMPORARY3);
        push(@TEMPORARY3, splice(@P3, rand(@P3), 1))
                while @P3;
        @P3 = @TEMPORARY3;

# Select template
# Convert TEMPLATE to an array
for ($i = 0; $i < $MAX; $i++){
# Build Password template
for ($i=0; $i < $MAX; $i++){
# Build password from password template
for ($i = 0; $i < $MAX; $i++){
        if ($TMPLT[$i] eq "1"){
        if ($TMPLT[$i] eq "2"){
        if ($TMPLT[$i] eq "3"){
print "$PW\n";

---------------------------------end pwgen-----------------------------------------

This next script takes as input the username and the password and uses the
password program to change the password for the user specified. This is an
expect script modefied from something that Jim McLean-Lipinski gave me as well.

# wrapper to make passwd(1) be non-interactive
# username is passed as 1st arg, passwd as 2nd

set password [lindex $argv 1]
set username [lindex $argv 0]

spawn /usr/bin/passwd [lindex $argv 0]
expect {*password:}
send "$password\n"
expect {*password:}
send "$password\n"
expect eof
---------------------------------end newpasswd-------------------------------------

This final script is what I wrote, it reads a list of usernames in DOS test format,
converts it to UNIX text format, takes all the capital letters and converts them
to lowercase letters and then creates the user and gives them a password.
The script is written to log all users added along with their password, but BE
CAREFUL WITH THIS!!!! Make sure you secure that file (chmod 600 only readable
and writeable by root)! I also e-mail the most recent additions to the responsible
person, but careful with this as well. Please be very careful with this script...
it will bomb out if anyone but root tries to run it...but still you don't want to
leave those logfiles just laying around anywhere...USE AT YOUR OWN RISK!


# this script will read a file of usernames (uname.dat) created
# on a PC running DOS, convert all the names to lowercase
# characters, convert the DOS text to UNIX text, and then
# create an account with a random password for each username
# that doesn't already have an account on the system
# the accounts created along with their password are logged
# and sent to the person in charge
# Derald H McMillan III
# (
# SolTech Systems Corporation 1995
# Revisions
# ---------

# change to the root (working) directory
cd /

# remove some working files if they exist
if -f uname.dat then
 rm uname.dat
if -f temp then
 rm temp

# set up some filenames
set namefile = users.dat
set temp = temp
set usernames = uname.dat
set oldnames = uname.old
set permlogfile = names.log
set logfile = .logfile

# open and secure the temporary logfile
touch .logfile
chmod 600 .logfile

# echo the date and time to the logfile
echo "Current Date: `date +%m`/`date +%d`/`date +%y` Current Time: `date +%H`:`date +%M`:`date +%S`" >> $logfile
echo "" >> $logfile
# convert all capital letters in the users.dat file to lowercase
tr "[A-Z]" "[a-z]" < $namefile > $temp
# convert a dos text file to a unix text file
dos2unix $temp $usernames
# assign an array of usernames
set namearray = (`cat $usernames`)
# initialize the userid to the predetermined series
set uid = 2001
# set up some defaults for the useradd program
/usr/sbin/useradd -D -b /export/home -g 1
# add each user specified if they don't already exist
# in the /etc/passwd file
foreach uname ($namearray)
 if (`grep -c $uname /etc/passwd` == 0) then
   # create a random password for each user added
   set passwd = `/usr/local/bin/pwgen`
   # make sure the userid is unique for each user added
   while (`grep -c $uid /etc/passwd` != 0)
    @ uid = $uid + 1
   # add the user
   /usr/sbin/useradd -u $uid -s /bin/csh -c $uname -m $uname
   # give the user the randomly password
   /usr/local/bin/newpasswd $uname $passwd
   # echo the username and password to the logfile
   echo "$uname $passwd" >> $logfile
# mail the logfile to the responsible person
/usr/bin/mailx < $logfile
# append the logfile to the permanent log
cat $logfile >> $permlogfile
# move the usernames to an file to keep around until next time
mv $usernames $oldnames
chmod 600 $oldnames
# remove temporary files
rm temp .logfile
# end of script

-------------------------------end nuad-------------------------------------------------

If you are using nis+ I'm not sure how you would change this to fit your needs..but it
should be fairly simple...and if you are using the sun package that allows you to run
NIS on Solaris 2.3, then all you should have to do is put it in the script to change
to the /var/yp directory and "make passwd". So customizing for a particular site should
be easy...just rewrite the input for the useradd program and anything else should be
simple as well.

Several other solutions were using a c program to generate the
encripted password or even using a perl program to generate the encripted password
Jeff Victor, Network Administrator for The Sage Colleges had the following to say:
If you're not using a naming service (NIS+, yp, etc.) you can encrypt
the desired password and insert it directly into /etc/[passwd | shadow].
I use this program to encrypt passwords:

#include <stdio.h>
#include <crypt.h>

main ()
   char s[80];
   gets (s);
   printf ("%s", crypt (s, "sa"));

Where "sa" is the salt for the crypt system call, which should also be

If you're running yp or NIS+, you must also use the encrypted or
unencrypted password text appropriately, i.e. enter them in yp maps or
stuff them into NIS+ tables with nistbladm.

I would like to thank the following people for offering suggestions or comments.
If I left your name out I'm very sorry. Special thanks to Jim McLean-Lipinski
for the scripts and to Simon Burr for the location of expect and Tcl. (Minh Tran) (David Lee) (Brian Wightman) (Jim McLean-Lipinski) (Douglas Carr) (Jon Diekema) (Simon Burr) (Randy Diffenderfer CT/90) (Robert Hill) (Tom Orban) (Phillip Plane)
david@srv.PacBell.COM (David St. Pierre) (Perry Hutchison) (Jos Schaeps)
victoj@kellas.Sage.EDU (Jeff Victor)

Thanks to the following people for their interest. (Phillip Moose) (Bill Zhu) (William Hiscox) (Richard Skelton)

Derald H McMillan III
SolTech Systems Corporation
1180 Sam Rittenberg Blvd. Phone: 803-556-2500
Suite 310 Fax: 803-556-2596
Charleston, SC 29407 email:


This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:10:15 CDT