Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a user on linux using useradd

Status
Not open for further replies.

BenRussell

Programmer
Mar 12, 2001
243
US
I want to create a user using a Perl script using the 'useradd' command (on Red Hat Linux).

However, I also want to set the password when I issue the command, such as:

useradd SOMEUSER -p SOMEPASSWORD

The problem is that SOMEPASSWORD has to be encrypted when it is passed to useradd and I do not know which algorithm to use. What Perl module can give me access to this algorithm?

- Ben
 
I searched google with the terms "perl::crypt" One suggestion was to use an expect script:

#!/usr/local/bin/expect -f
set username $env(UNAME)
set password $env(UPASS)

spawn passwd $username
expect "New password:"
send "$password\r"
expect "Re-enter new password:"
send "$password\r"
send "exit\r"
expect eof


It also appears there is an RPM package called "perl-crypt-des" that simulates the crypt function used to generate passwords.
 
I can't use the expect script though because it has to be built into my Perl script.

- Ben
 
I really don't know the answer, just some suggestions from searching google.

Good luck.
 
I figured out that if you use the built-in crypt() function it encrypts the password correctly.

For instance:

Code:
my $linuxPassword = crypt('MYPASSWORD', 'SALT');

And I am using a random 2 character salt (according to Perl documentation).

Then I just did a system command telling it to create the user:
Code:
my $command = `useradd MYUSER -p $linuxPassword`;

- Ben
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top