Operating System - HP-UX
1851753 Members
3202 Online
104062 Solutions
New Discussion

Adding 3000 user and setting there password via a script.

 
SOLVED
Go to solution
Troy Johnson_2
Occasional Advisor

Adding 3000 user and setting there password via a script.

I am a new sys admin who is not too fimiliar with scripting. I am looking for a script the will add 3000 users to the same home directory and will set there password.
18 REPLIES 18
rainer doelker
Valued Contributor

Re: Adding 3000 user and setting there password via a script.

Troy,

you may want to use "/usr/sbin/useradd" if you type the command without any option you'll receive a list of options.

If you have a list of users, e.g. form a file you can read in the lines of the file in a while loop. I'll have to lookup the correct syntax....

rainer
rainer doelker
Valued Contributor

Re: Adding 3000 user and setting there password via a script.

Troy,

so if you have a file with a list of users, e.g.
user1
user2
user3
and it is named users.txt you can start off with you script like:

#!/usr/bin/ksh
# this will setup users from users.txt

N=100
cat users.txt |while read i
do
N=`expr $N + 1`
useradd -u $N -g 20 -s /sbin/false -c generic $i
done

This will create passwd entries for all users from users.txt. They will belong to group 20 (users) and have "no shell" and they will have as description "generic"

hope this helps.
rainer
Robin Wakefield
Honored Contributor

Re: Adding 3000 user and setting there password via a script.

You don't say what the usernames are, but I assume they're in electronic form somewhere, not system generated, so you can copy the names into a file.

Save your passwd and group files, just in case

cp -p /etc/passwd /etc/passwd.good
cp -p /etc/group /etc/group.good

Add a dummy user, and assign a passwd. Grab this passwd (make sure it has no metacharacters in it, such as "/$+" etc.) Let's assume it's abcdefgh

Create a file, say, "/tmp/file", one line per user

cat /tmp/file | while read user ; do
useradd $user (+ any other options you like)
sed "s/$user:\*:/$user:abcdefgh:/" > /tmp/passwd
mv /tmp/passwd /etc/passwd
passwd -f $user
done
chmod 444 /etc/passwd

Check all is OK before logging out. Any problems, move the saved files back into place

Note - passwd -f forces them to change their password next time they login, by putting ",.." after the password.

Robin.
Troy Johnson_2
Occasional Advisor

Re: Adding 3000 user and setting there password via a script.

I do have a electronic form of a file that has all 3000 user id's. I know what each id password should be set too. I DO NOT want to force the users to change there password the next time they login. Are these scripts still applicable?
Bill McNAMARA_1
Honored Contributor

Re: Adding 3000 user and setting there password via a script.

I don't think you can assign a password from the passwd command for sure.
The /etc/passwd password field is one way translation.
If you add an entry to the password field, you are adding a decripted string, not the password... thank God for that!
So, I believe the only way is to get the users to set their passwd on first login.
Unless you set 3000 of them yourself.

Man passwd just to see if you can do it, but I really doubt it.

Bill
It works for me (tm)
rainer doelker
Valued Contributor

Re: Adding 3000 user and setting there password via a script.

Robin,

am I understanding it right that you just move the new gernerated file over /etc/passwd?

Did you keep in mind to keep the existing users especially root !

I'd suggest to change the "mv" line to a line like
cat /tmp/passwd >> /etc/passwd

regards,
rainer
Robin Wakefield
Honored Contributor

Re: Adding 3000 user and setting there password via a script.

rainer - I'm not telling sed to ONLY output the changed line, but output ALL lines, changing the user in question.

troy - are your passwords already encrypted. If they're not, I'm not sure you can do it through a standard script.

Robin.

Herve BRANGIER
Respected Contributor
Solution

Re: Adding 3000 user and setting there password via a script.


Hi,

If you have old passwd file insert lines in
new one. If not, I think I know how to do,
see below...

You can create users like others said and you
will obtain a /etc/passwd like that :

....
user2000:...:.......:...
....

After you need to change all passwd, and you
have a file with user and passwd (not crypted)
like that :
user:passwd

So now you need to insert encrypted passwd :

Compile this small program in /tmp :
1) copy it in /tmp/cr.c
2) cc cr.c -o cr

/* DEBUT PRG */
#include
#include

void main (narg, args)
int narg;
char ** args;
{ printf ("%s",crypt (args[1],args[2]));}
/* FIN PRG */

Now you can use this prg like that :

/tmp/cr passwd key

key is a two letters string (which take place
at the beginning of encrypted passwd,
man 3 crypt for more informations)

now a script example (you made a file with
user:passwd) I use az like key:


cat >/tmp/passwd.users
for i in `cat /tmp/file`
do
USER=`echo $i | cut -f1 -d':'`
PWD=`echo $i | cut -f2 -d':'`
grep "$USER" /tmp/passwd | awk -F':' -vVAR=`/tmp/cr $PWD az` '{print $1:VAR:$2:$3:$4:$5:$6:$7}' >> /tmp/passwd.users
done

Now You need to replace lines in passwd by new
ones in /tmp/passwd.users



HTH

Herve



Robin Wakefield
Honored Contributor

Re: Adding 3000 user and setting there password via a script.

Troy,

The following perl one-liner can be used for each password, where $pwd is read from your password list, and "ue" is the encryption salt (randomizer). It doesn't have to be "ue", just any two characters would do.

perl -e 'print crypt("$ARGV[0]","$ARGV[1]") . "\n";' $pwd ue

If you want me to incorporate this into my earlier script, please let me know.

Robin
Bill McNAMARA_1
Honored Contributor

Re: Adding 3000 user and setting there password via a script.

Cool script Herv?!
You learn something new every day here!

Later,
Bill
It works for me (tm)
Troy Johnson_2
Occasional Advisor

Re: Adding 3000 user and setting there password via a script.

Robin,
No my password list is not encrypted.
Please incorporate the perl one-liner in your earlier script.
Herve BRANGIER
Respected Contributor

Re: Adding 3000 user and setting there password via a script.

Troy,

Think you can use :
(/tmp/file format is already the same, a list
of user:passwd:and others informations)


cat >/tmp/passwd.users
cp /etc/passwd /tmp/passwd.ORG
cp /etc/passwd /etc/passwd.ORG
for i in `cat /tmp/file`
do
USER=`echo $i | cut -f1 -d':'`
PWD=`echo $i | cut -f2 -d':'`
# You can do the same for others fields (for example home_dir...)
useradd _u $USER ... # add other parameters
EPWD=`perl -e 'print crypt("$ARGV[0]","$ARGV[1]") . "\n";' $pwd ue `
grep "$USER" /tmp/passwd | awk -F':' -vVAR=$EPWD '{print $1:VAR:$2:$3:$4:$5:$6:$7}' >> /tmp/passwd.users
done
cat /tmp/passwd.users >> /tmp/passwd.ORG

Now verify if /tmp/passwd.ORG is good
and copy it to /etc/passwd


HTH

Herve

Herve BRANGIER
Respected Contributor

Re: Adding 3000 user and setting there password via a script.


Error, in my script I made a copy of Robin's
Perl command but first parameter is $PWD
instead of $pwd

Sorry :-(

Herv?

Curtis Larson_1
Valued Contributor

Re: Adding 3000 user and setting there password via a script.

the steps are pretty easy.

#!/usr/bin/ksh

function getSalt {
typeset Salts
typeset -i num

set -A Salts a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 . /

num=$(($RANDOM / 512 ))
salt1=${Salts[$num]}

num=$(($RANDOM / 512 ))
salt2=${Salts[$num]}

print "$salt1$salt2"
return
}

function encodePwd {
typeset pwd="$1" Salt="$2"
typeset -L8 Pwd
typeset -i num

num="${#pwd}"
if [[ $num -gt 7 ]] ;then
Pwd=$pwd
pwd=$Pwd
else
while (( $num < 8 ))
do
pwd="${pwd}\000"
(( num = $num + 1 ))
done
fi

print "${pwd}$Salt" | /usr/lbin/makekey

return
}

function editFile {

ex -s $pwdFile <<-EODATA
/${1}:
s!:.*:!:${2}:!
wq
EODATA
return
}

cat yourUserAndPasswordFile |
while read user newPwd
do

# get your salt
Salt=$(getSalt)

#encode you password
encodedPwd=$(encodePwd "$newPwd" "$Salt")

#edit your file
editFile "$user" "$encodedPwd" >/dev/null

done

now this does no validation of appropriate usernames or passwords. And doesn't support things like trusted systems, NIS, etc.
Robin Wakefield
Honored Contributor

Re: Adding 3000 user and setting there password via a script.

Troy,

This assumes each line in /tmp/file is of the form:

username password

==============================
cat /tmp/file | while read user pwd ; do
useradd $user (+ any other options you like)
perl -e 'print crypt("$ARGV[0]","$ARGV[1]") . "\n";' $pwd ue | read p
sed "s/^$user:\*:/$user:$p:/" > /tmp/passwd
mv /tmp/passwd /etc/passwd
passwd -f $user
done
chmod 444 /etc/passwd
==============================

Hope this helps,

Robin.
Herve BRANGIER
Respected Contributor

Re: Adding 3000 user and setting there password via a script.

Hi Troy,

It's your first question on this forum...

There is a custom on this forum :when someone
give you informations (sometimes a solution of
your problem), it's a good idea to give him
points...

Regards,


Herv?

Troy Johnson_2
Occasional Advisor

Re: Adding 3000 user and setting there password via a script.

Herve',
You folks were sending some good stuff at me and needed sometime to apply the scritps. This call can be closed.
thanks
Herve BRANGIER
Respected Contributor

Re: Adding 3000 user and setting there password via a script.

Hi Troy,

I think you success now... Well, could you
give the community your really tested and
apply script... For the next one who want to
do that...

Thanx

Herve