Operating System - HP-UX
1824941 Members
3640 Online
109678 Solutions
New Discussion юеВ

Batch job to set password

 
Kevin Nikiforuk
Valued Contributor

Batch job to set password

I have to create user accounts for approx 400 users. I know how to do this but I do not want to set the password to null.
I would like to run a script and set the users password to "something" and then force user to change it at first log on.
Here is what I have wrote so far.
for USER in `cat cifsuser1`
do
useradd -g users -m $USER
passwd -d $USER
"I neeed help here"
passwd -f $USER
done
4 REPLIES 4
Yang Qin_1
Honored Contributor

Re: Batch job to set password

If you want to assign a password for an user and then you force him/her to change it afterwards. Then you have to distribute 400 passwords to 400 users before they can change the passwords. It's not to heavy for you?

You can use /usr/sam/lbin/uusermod.sam -F -P user1 to assign the password for user1. But ... (sorry, but) you have to use encrypted password (cW7789FCXD). You need to run crypt of perl crypt to encrypt the passwords.

Yang
Bill Hassell
Honored Contributor

Re: Batch job to set password

Creating the encrypted password is the most difficult part. Use usermod.sam after running this little program to get the encrypted password:

#include

#include



/* 1st param is the desired password */

/* A random seed (2 chars) will be */

/* automatically chosen. */

/* For good passwords: */

/* Use random chars, mixed apha- */

/* numerics and MiXeD CaSe for */

/* better protection. */





main(argc, argv)

int argc;

char *argv[];

{



char salt[3];

char *EncryptedPasswd;

int CheckRand;

int Fixup;

int SeedChar;



printf("\nUsage: pw \n\n");



/* Generate a random starting point for seed charcaters */



srand(time(NULL));

for ( SeedChar = 0; SeedChar <= 1; SeedChar++) {

CheckRand = 46 + rand() % 76; /* random number from 46 to 122 */

Fixup = 7 + rand() % 13; /* random number from 7 to 20 */

salt[SeedChar] = toascii(((CheckRand >= 58 && CheckRand <= 64) ||

(CheckRand >= 91 && CheckRand <= 96) ? CheckRand + Fixup : CheckRand));

}



EncryptedPasswd=crypt(argv[1], salt);

printf("\nRequested pw= %s, Automatic Seed= %s, encrypted pw= %s\n",

argv[1], salt, EncryptedPasswd);

}



Bill Hassell, sysadmin
Doug O'Leary
Honored Contributor

Re: Batch job to set password

Hey;

The trick is to use /usr/sam/lbin/useradd.sam. That program accepts a -p ${pwd} option; however, as mentioned, the password has to be encrypted. Here's how I would do it.

1. Generate a data file for your users in the format:

${user1}|${gecos}|${primary_group}|${alt_groups}
${user2}|${gecos}|${primary_group}|${alt_groups}
${user3}|${gecos}|${primary_group}|${alt_groups}

2. Add a temporary user, set the password you want, then obtain the encrypted password from /etc/shadow, or /tcb/files/auth/${l}/${user}.

3. ID the beginning uid - some stretch that you have 400 uids available.

4. Write the following inline script

pwd=${enc_pwd}
uid=${uid}

cat users | while IFS=| read user gecos pgrp groups
do
printf "%-8s %-5d %s\n" ${user} ${uid} "${gecos}"
/usr/sam/lbin/useradd.sam -p ${pwd} -u ${uid} -g ${pgrp} -G ${groups} -d /home/${user} -s /bin/ksh ${user}
mkdir -m 755 -p /home/${user}
cp /etc/skel/.[A-z]* /home/${user}
chown -R ${user}:${pgrp} /home/${user}
uid=$((uid+1))
/bin/passwd -f ${user}
done

That should do the trick for you.

Doug

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
Kevin Nikiforuk
Valued Contributor

Re: Batch job to set password

Thanks for all your help.