Operating System - HP-UX
1827464 Members
4550 Online
109965 Solutions
New Discussion

Need to create a script for user creation and passwd

 
shikhar_1
Regular Advisor

Need to create a script for user creation and passwd

Hi,

Can anyone pls let me know how to create a script which will create a user and given passwd also through script?

Like i want to create a user "test" and i also want the passwd for this user is "test" through script.
Appreciaitng your help

Regards,

Shikhar Verma
3 REPLIES 3
Ralph Grothe
Honored Contributor

Re: Need to create a script for user creation and passwd

The creation of the account is easily scriptable because you only need to call the useradd (and if required the groupadd) command.
See their manpage for details about options and arguments.
The tricky part might be the scripted setting of a password because the passwd command can only be executed in interactive mode.
However, some care for proper file format and advisory file locking provided you can edit /etc/passwd (or /etc/shadow if you run a "passwd shadowed" system) throug a script as well. You only need to find some function that would generate the encrypted hash string.
I should think that most "dynamic languages" provide such hash functions.
Personally, I would make a Perl call to the crypt() function (standard Perl, see "perldoc -f crypt") if you have to deal with old-style Unix passwd encryption, or a CPAN module like Crypt::PasswdMd5 (http://search.cpan.org/~luismunoz/Crypt-PasswdMD5-1.3/PasswdMD5.pm) if you have to deal with MD5 encrypted passwords.
Madness, thy name is system administration
VK2COT
Honored Contributor

Re: Need to create a script for user creation and passwd

Hello,

Many ways to do it.

One is in my Perl script:

http://www.circlingcycle.com.au/Unix-sources/add-batch-Unix-accounts.pl.txt

Cheers,

Dusan
VK2COT - Dusan Baljevic
Mark Silvester
New Member

Re: Need to create a script for user creation and passwd

Personally I would use the command useradd.sam, this allows you to specify the password when you add the user.
So a simple script that creates a user (and sets the password) with the supplied parameter would be

#!/usr/bin/ksh
if [ ! -z "$1" ]
then
PASSWORD=$1
while [ $(echo $PASSWORD | wc -c) -lt 9 ]
do
PASSWORD=$PASSWORD"\0"
done
/usr/sam/lbin/useradd.sam -p $(echo ${PASSWORD}xx|/usr/lbin/makekey) $1
else
echo "Syntax: $0 "
fi

This can then be expanded on to add error checking and any other additional options that are required such as defining the uid, group etc etc