Operating System - HP-UX
1825950 Members
2924 Online
109690 Solutions
New Discussion

script to create user from a list of users

 
uadm26
Super Advisor

script to create user from a list of users

Hy people,

I'm trying to do a shell script to create users from a file that contain something like that:

username fullname

And is something like this:
...
for n in `cat users.txt`
do
useradd ...
passwd user
done;

But I have one problem inserting the password, how can use a default password, without interacting whit the sheel every time i create a user?

Any one has other ideas to improve the script?
10 REPLIES 10
Mark Grant
Honored Contributor

Re: script to create user from a list of users

what some people do is to directly write the default password into the /etc/passwd file. In other words, create a user, change the password to your default password. Then cut & paste the password field from /etc/passwd into your script.

Then write your user add script to insert this string into the password file. It's a bit of a hack and it can be dangerous if you don't test it carefully (i.e make sure you have a good copy of your passwd file).

Never preceed any demonstration with anything more predictive than "watch this"
Mobeen_1
Esteemed Contributor

Re: script to create user from a list of users

Joel,
I am sorry not sure if i have understood your question. If you are looking at avoiding having to update passwd for every user, you can have that password as a variable.

But are you willing to assign the same initial passwd for all your users? If this is allowed then its fine.

Else you can look at generating a password and pass that into a variable each time you go through the loop of adding user.

regards
Mobeen
MarkSyder
Honored Contributor

Re: script to create user from a list of users

I've not actually tested this but suspect it would work.

Assuming your script is called adduser and the default password is defpass, you would run:

adduser defpass

where the passwd line in your file would be:

passwd user $1

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing
Steven E. Protter
Exalted Contributor

Re: script to create user from a list of users

There is a lot that can be done here.


while read -r aa
do
useradd $aa


passwd $aa << EOF
defaultpass
defaultpass
EOF


done < /etc/passwd.copy

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Bill Hassell
Honored Contributor

Re: script to create user from a list of users

A truly universal method cannot use direct /etc/passwd editing (Trusted or Shadow systems). So, the only batch way to do this is to 'borrow' SAM's method: useradd.sam. You'll find it in /usr/sam/lbin/useradd.sam but lbin is a flag here: lbin files are 'backend' or internal use only. What that means is that this SAM tool may change at any time without notice, either by patch or upgrade. Now the good news is that other lbin programs have been 'upgraded' to supported (getprpw and modprpw for example) by getting a man page (11.11 only but the man page works for 10.20 and 11.0 too).

Like most backend commands, finding out how they work can be a challenge so I always start with something like:

/usr/sam/lbin/useradd.sam -abcxyz
/usr/sam/lbin/useradd.sam -abcxyz
Unrecognized Option 'a'
Usage: /usr/sam/lbin/useradd.sam [-p [-u [-o]] [-g ] [-G <
group>[,]] [-d ] [-s ] [-c ] [-m [-k &lt;skel dir&gt;]]&lt;BR /&gt; [-f &lt;inactive&gt;] [-e &lt;expire&gt;] &lt;login&gt;&lt;BR /&gt;Usage: /usr/sam/lbin/useradd.sam -D [-g &lt;group&gt;] [-b &lt;base dir&gt;] [-f &lt;inactive&gt;]&lt;BR /&gt; [-e &lt;expire&gt;&lt;BR /&gt; &lt;BR /&gt;And if you compare the option list, you'll see all the same options as plain old useradd (man useradd) except -p (password). So your script needs to generate the encrypted password entry. You can use /usr/lbin/makekey to create the encrypted password for useradd -p.


Bill Hassell, sysadmin
uadm26
Super Advisor

Re: script to create user from a list of users

when I execute the script he continues to asking me to introduce a password, so, I have allways interacts with the script.
How can improve the script to insert a value from a variable in the password without interact to the shell?

I don't want this:

#sh cria_users
Changing password for u89874
New password:
Re-enter new password:
Passwd successfully changed
Sridhar Bhaskarla
Honored Contributor

Re: script to create user from a list of users

Hi Joel,

I am not sure if your script is going to work. For ex., if you have your users.txt as

user1 FirstName1 LastName1
user2 FirstName2 LastName2

etc., then "for n in `cat users.txt` is not going to work as it will try to create users user1, FirstName1, LastName1, user2 etc.,

So, I would modify the file as

for USER in $(awk '{print $1}' users.txt)
do
useradd bla.bla.bla#your options
done

That will add the users. Make sure your users.txt is good and the login name appears always as the first word.

To set the passwords, I suggest you use Bill's approach of /usr/lib/makekey and usr/sam/lbin/useradd.sam. If you are going to keep one single default password for all these new users, then generate the encrypted of it by using /usr/lib/makekey. makekey supports only upto 8 chars of the password. If you want to set more than 8 chars, then you will need to write a small c program using bigcrypt. "makekey" accepts 10 chars with last two being random (called salt). So, to generate the encrypted string for the password "Test1234", I would do something like this

echo "Test1234xy"|/usr/lib/makekey

xyyB13z8Nr6jw

Then run usermod.sam command for each user

for USER in $(awk '{print $1}' users.txt)
do
usr/sam/lbin/useradd.sam -p xyyB13z8Nr6jw
/usr/bin/passwd -f $USER
done

passwd -f will expire the passwords so that when the user logs in, it will prompt him/her to change.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
MarkSyder
Honored Contributor

Re: script to create user from a list of users

Again, I haven't had time to test this (busy day).

Change the passwd line in my earlier posting to:

passwd user << endpasswd
$1
$1
endpasswd

You can use a shorter flag than endpasswd if you prefer.

Mark
The triumph of evil requires only that good men do nothing
Jakes Louw
Trusted Contributor

Re: script to create user from a list of users

Give Bill 10 points!
I've spent a month fooling around with Expect, when the answer was under my nose!
Trying is the first step to failure - Homer Simpson
Senthil Kumar .A_1
Honored Contributor

Re: script to create user from a list of users

hi Joel Azevedo,

Firstly there is no way of redirecting the default password's for all user through shell script even using "HERE" statement, because for a simple reason that unix always expects the password from the keyboard alone interactively.that indeed is a great security feature without which any intruder can go through all shell script and can get the knowledge about passwords or password setting patterns.

Bills method of setting the password could be followed, though I would be little cautious if I were you,because it is strongly advised against the practice of directly inputing the data into passwd file through a shell script unless it thoroughly checked against a dummy file ,just to be careful about the possible bug in ur script,

Instead I would suggest a option which can be used safely,

Try using 'passwd -d -f user' instead of 'passwd user' in ur for statement of ur shell script.Doing so will set a null password for all the new user u are creating(courtesy -d option) and also will force the users to assign a password the first time they log in (courtesy -f option)

Best of luck

senthil

i have attached a sample script
Let your effort be such, the very words to define it, by a layman - would sound like a "POETRY" ;)