Operating System - HP-UX
1755350 Members
5410 Online
108831 Solutions
New Discussion юеВ

Re: Creating new user with script

 
Jerry P Antony_1
Occasional Advisor

Creating new user with script

Hi
I have one text file containing first field as user id and second field as passwd.I would like to write a script which will add the new users with this user id and password with out any interaction.(it should take the second field as the input for passwd command).Can anybody help in this?

Thanx in advance
Jerry
7 REPLIES 7
Yang Qin_1
Honored Contributor

Re: Creating new user with script

Are those passwords encrypted? If they are not encrypted, you may need to encrypt passowrds first because the command to assign the password for a user use encrypted password. I assume that 1st and 2nd fields in passwd.1 are with the same format as /etc/passwd
user_account_name:encrypted_password
You will let the system to select UID and you will use default group id (otherwise, you can use -u UID -g GID in useradd command to define UID and GID):

#!/usr/bin/ksh

while read line
do
uname=`echo $line|cut -d":" -f1`
passwd=`echo $line|cut -d":" -f2`
useradd -m $uname
/usr/sam/lbin/usermod.sam -p $passwd
done < passwd.1

exit

Yang
Jerry P Antony_1
Occasional Advisor

Re: Creating new user with script

Yang
Thanx for your reply.
The password is not encripted.I perfer not to use usermod.sam because I need to do the same excercise on my linux file server also.Is there any common shell script which can do this?
Jerry
Arturo Galbiati
Esteemed Contributor

Re: Creating new user with script

Yang Qin_1
Honored Contributor

Re: Creating new user with script

Hi, Jerry, If you want to run the same script on both HPUX and Linux. You may have a look at "expect" http://wiki.tcl.tk/201

Yang
Doug O'Leary
Honored Contributor

Re: Creating new user with script

Hey;

If you want to put the password in on an HP box w/o the use of expect, you're stuck with usermod.sam or ueradd.sam. How you differentiate between Linux and HPUX is through a variable:

Os=$(uname -s)
[[ "${Os}" = "HP-UX" ]] && Useradd=/usr/sam/lbin/useradd.sam || \
Useradd=/usr/sbin/useradd
uid=1000

cat passwd.1 | while read user pwd
do
printf "%-8s %s\n" ${user} ${pwd}
${Useradd} -u ${uid} -g users -c "test user" -d /home/${user} \
-p ${pwd} -s /bin/ksh ${user}
mkdir -p -m 755 /home/${user}
cp /etc/skel/.[A-z]* /home/${user}
chown -R ${user}:users /home/${user}
uid=$((uid+1))
done

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
Jerry P Antony_1
Occasional Advisor

Re: Creating new user with script

Friends
Thanx for the reply. We have Holy Ramadan vacation here and not in a position to try out the option.I will try after the vaction and update you.
Igor Sovin
Super Advisor

Re: Creating new user with script

Hi!

You can try this for adding users:

while read username password
do
useradd [options] ${username}
done < your_file

Still thinking about how to automatically assign passwords to new user.