Modify users password via script?

Ok, first off, I'm working on a Vmware ESX server, which I guess is loosely based off of Red Hat 9. But I'm brand new to it (today), so be nice.

I'm trying to write a useradd script that will create some users, generate a password, and set their password to this newly generated password.

Now by hand, I can use the useradd command, and then use passwd to change their password. But if I've got to set up a couple hundred users, this is not enjoyable.

So if I can't use passwd in a script (because it requires user interaction), how can I create these users with some default password?

This is on an ESX 3.x.x box, but you've got a few options.

The first is to use the --stdin option to passwd, e.g.

# useradd -m -d /home/foo foo
# echo "foo" | passwd --stdin foo
Changing password for user foo.
passwd: all authentication tokens updated successfully.

This would require you storing the plain text password in your script. A *much* safer option is to add a user and set the password as you normally would to a standard value, e.g.

# useradd -m -d /home/tmpuser tmpuser
# passwd tmpuser
...

Now, you can use the encrypted password for this user when creating other accounts, so that all newly created accounts have the same password as "tmpuser", e.g.

# useradd -m -d /home/newuser -p `awk -vFS=':' '$1 ~ /^tmpuser/ {print $2}' /etc/shadow` newuser

Cheers,
ZB

Thanks for the reply. I would have replied back sooner, but haven't gotten a chance to try it out till now. I actually like the --stdin option. The script actually won't be holding a plain text password. What I would like to do is generate a random password in my script, and pass it to --stdin.

I just have one problem. I'm really new to Vmware ESX, but I was able to find a little script that generates a password. Here it is:

MAXSIZE=8
array1=(
q w e r t y u i o p a s d f g h j k l z x c v b n m
)
MODNUM=${#array1[*]}
pwd_len=0
while [ $pwd_len -lt $MAXSIZE ]
do
    index=$(($RANDOM%$MODNUM))
    echo -n "${array1[$index]}"
    ((pwd_len++))

echo
done

As you can see, all that script does is generate the password, and then echo it out. But I've never seen where you can just use "echo" and not tell it what to echo. So what variable is my password being stored in? If it's $index, then how can I use it with --stdin?

The problem comes when I try to use it, putting this in my code:

# echo "$index" | passwd --stdin foo

because the "echo" is also printing out the password, so do you know how I can use this to my advantage?

thanks again for your help.