Solved

Set user's password in Perl script?

Posted on 2010-08-25
11
544 Views
Last Modified: 2012-05-10
Hi,

I writing a Perl script which will amongst other things create a load of users on a Linux server. I want to be able to have the script set the password for the new users it creates, and email those to the users (I will have an array with the email addresses in).

At the moment I'm struggling to find a way to set passwords in a Perl script - I can see why its difficult but if I can't do this, my plan is significantly less useful than I had hoped!

Any info welcomed. Thanks in advance.

George
0
Comment
Question by:georgemason
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 33519952
see an example here http://www.experts-exchange.com/OS/Linux/Q_22641743.html
system "sudo /usr/local/bin/chpasswd.sh $user $passwd";
or maybe call a shell script to set it passing in args
0
 
LVL 7

Expert Comment

by:vikas_madhusudana
ID: 33519967
you can use usermod to change the password.

~Vikas
0
 
LVL 1

Author Comment

by:georgemason
ID: 33521319
@rockiroads - I've seen that before - but I'm writing the script to run on a Linux box, not as a web app - how would I apply that in my case?

@vikas - tried the following: system("usermod -p $password $reseller");  (where the two variables contain valid strings) and it doesn't error, but doesn't set the pw either
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 3

Expert Comment

by:gremwell
ID: 33521833
I wonder perhaps you have forgotten to convert the plain text password to hash before passing it to usermod? You can use Crypt::PasswdMD5 available from CPAN for this purpose. The attached code snippet works for me, when executed as root.

The following script works for me (Ubuntu):
use Crypt::PasswdMD5 qw(unix_md5_crypt);
my @salt = ( '.', '/', 0 .. 9, 'A' .. 'Z', 'a' .. 'z' );
sub gensalt {
 my $count = shift;
 my $salt;
 for (1..$count) {
  $salt .= (@salt)[rand @salt];
 }
 return $salt;
}
$reseller = "abb2";
$password = "xyz2";
my $password_hash = unix_md5_crypt($password, gensalt(8));
system("usermod -p '$password_hash' $reseller");

Open in new window

0
 
LVL 3

Expert Comment

by:gremwell
ID: 33521858
I forgot to mention that the above code is based on the example provided by http://sial.org/howto/perl/password-crypt/ web page.
0
 
LVL 1

Author Comment

by:georgemason
ID: 33522710
Sounds very promising - I had forgotten (i.e. didn't know I needed to) hash the pw.

I get this error - do I need to install a perl module?

"Can't locate Crypt/PasswdMD5.pm in @INC "
0
 
LVL 3

Expert Comment

by:gremwell
ID: 33522922
Yes, the module is available from CPAN archive. Try

sudo perl -MCPAN -e 'install Crypt/PasswordMD5'

For this to work your computer has to have internet access, to download modules from CPAN server. If it is the first time you use CPAN, you may get a lot of questions, in general it is safe to accept default options (just press Enter). See also http://perl.about.com/od/packagesmodules/qt/perlcpan.htm for more details.

PS. Some Linux distribution offer better way to do the same, Ubuntu and Gentoo at least, maybe other distributions too.
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33535742
that sample web code is to take in user input of a userid but the password changing code should still be applicable right?

What command are you using right now to create the user?

On your linux o/s do you have the command useradd? If so then that is one way to create users and in there specify the password also.

useradd [-c comment] [-d home_dir]

    [-e expire_date] [-f inactive_time]
    [-g initial_group] [-G group[,...]]
    [-m [-k skeleton_dir] | -M] [-n] [-o] [-p passwd] [-r]
    [-s shell] [-u uid] login

More info on this can be found here http://linux.about.com/od/commands/l/blcmdl8_useradd.htm

0
 
LVL 3

Expert Comment

by:gremwell
ID: 33539642
Sorry, I have made a typo in my previous comment. The correct command is

sudo perl -MCPAN -e 'install Crypt::PasswdMD5'
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 250 total points
ID: 33605491
If you run your script as root, you can just call passwd directly.  To run your script as root, either use sudo, or actually run it as root.

To run passwd:
    system("echo $new_password | passwd --stdin $user");

To run your script using sudo:
    1) save your script, for example to myscript.pl
    2) Run like:    sudo myscript.pl
        You may be asked for your password

Or, to run as root:
    1) su
        You will be asked for root's password
    2) myscript.pl
   
 
0
 
LVL 1

Author Comment

by:georgemason
ID: 33681744
Thanks to you all for your suggestions. In the end the one which (a) worked and (b) was simplest was from Adam314 - so points go to him.
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Hello EE, Today we will learn how to send all your network traffic through Tor which is useful to get around censorship and being tracked all together to a certain degree. This article assumes you will be using Linux, have a minimal knowledge of …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question