LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-27-2008, 10:53 AM   #1
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Rep: Reputation: 17
perl script problem in adding user


HI guys, I am trying out adding user through
perl script with file handling. BUt I am stucked. Please help me. New
to perl

this is my script called ya2.pl

Code:
#!/usr/bin/perl -w

use strict;
my @line;
open(FILEHANDLE, "<file.txt") or die "cannot open file for reading: $!";

while(@line = <FILEHANDLE>){

my $pass = crypt($line[1],"password");
system("sudo /usr/sbin/useradd -m -p $pass $line[0]");


}

close(FILEHANDLE);
THis is my text file called file.txt

Code:
sarvin1
123
This script will add user, but the password is not working. Y is that. Is this because of the array. Pls help..THanks..
 
Old 03-27-2008, 11:17 AM   #2
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
I'm guessing the salt you are using ("password") isn't valid. Unless you are really set on using the perl crypt function, try the following instead:
Code:
#!/usr/bin/perl -w

use strict;

$inputFile = "file.txt";

open(FILEHANDLE, "<$inputFile") or die "cannot open file for reading: $!";

while(<FILEHANDLE>){
    my $username = $_;
    my $password = $_;
    system("sudo /usr/sbin/useradd -m $username");
    system("echo $password | sudo /usr/bin/passwd --stdin $username");
}

close(FILEHANDLE);
HTH

Forrest
 
Old 03-27-2008, 12:37 PM   #3
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Original Poster
Rep: Reputation: 17
I have tried that code, it gives error, I am not sure what it says, Please have a look

Quote:
sarvin3
sh: -c: line 1: syntax error near unexpected token `|'
sh: -c: line 1: ` | sudo /usr/bin/passwd --stdin sarvin3'
123
sh: -c: line 1: syntax error near unexpected token `|'
sh: -c: line 1: ` | sudo /usr/bin/passwd --stdin 123'
i used the crypt because the p in, /usr/sbin/useradd -p , is encrypted password. So if i put password = 123, the number 123 will be in shadow file. so when i log in i can put 123 as password because it is encrypted version..so i have to decrypt. so thats y i use crypt, so that i encrypt again the crypted password. Please help
 
Old 03-27-2008, 12:56 PM   #4
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
Sorry, that should be:

Code:
#!/usr/bin/perl -w

use strict;

my $inputFile = "file.txt";

open(FILEHANDLE, "<$inputFile") or die "cannot open file for reading: $!";

while(<FILEHANDLE>){
    my $username = $_;
    my $password = <FILEHANDLE>;
    system("sudo /usr/sbin/useradd -m $username");
    system("echo $password | sudo /usr/bin/passwd --stdin $username");
}

close(FILEHANDLE);
However, I didn't test it, and you are right, the system calls aren't working properly. (although the same works from a shell). I'll let you know when I figure it out.

HTH

Forrest

Last edited by forrestt; 03-27-2008 at 01:01 PM.
 
Old 03-27-2008, 01:00 PM   #5
exscape
Member
 
Registered: Aug 2007
Location: Sweden
Distribution: OS X, Gentoo, FreeBSD
Posts: 82

Rep: Reputation: 15
If the data is "user\npass\n", shouldn't you chomp() the data somewhere?
 
Old 03-27-2008, 01:10 PM   #6
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
OK, the following works for me:

Code:
#!/usr/bin/perl -w

use strict;

my $inputFile = "file.txt";

open(FILEHANDLE, "<$inputFile") or die "cannot open file for reading: $!";

while(<FILEHANDLE>){
    my $username = $_;
    my $password = <FILEHANDLE>;

    system("sudo /usr/sbin/useradd -m $username");

    open(PASSWD, "|sudo /usr/bin/passwd --stdin $username");
    print PASSWD $password;
    close(PASSWD);
}

close(FILEHANDLE);
HTH

Forrest
 
Old 03-27-2008, 01:38 PM   #7
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Original Poster
Rep: Reputation: 17
Thanks 4 the reply.I have tried, but there is an error in my system

here is the error
Quote:
/usr/bin/passwd: unrecognized option `--stdin'
Usage: passwd [options] [LOGIN]

Options:
-a, --all report password status on all accounts
-d, --delete delete the password for the named account
-e, --expire force expire the password for the named account
-h, --help display this help message and exit
-k, --keep-tokens change password only if expired
-i, --inactive INACTIVE set password inactive after expiration
to INACTIVE
-l, --lock lock the named account
-n, --mindays MIN_DAYS set minimum number of days before password
change to MIN_DAYS
-q, --quiet quiet mode
-r, --repository REPOSITORY change password in REPOSITORY repository
-S, --status report password status on the named account
-u, --unlock unlock the named account
-w, --warndays WARN_DAYS set expiration warning days to WARN_DAYS
-x, --maxdays MAX_DAYS set maximim number of days before password
change to MAX_DAYS
what does this means /usr/bin/passwd: unrecognized option `--stdin'
 
Old 03-27-2008, 01:46 PM   #8
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
It means there is a difference between the password command on Debian and the password command on Fedora. Change it to the following:

Code:
#!/usr/bin/perl -w

use strict;

my $inputFile = "file.txt";

open(FILEHANDLE, "<$inputFile") or die "cannot open file for reading: $!";

while(<FILEHANDLE>){
    my $username = $_;
    my $password = <FILEHANDLE>;

    system("sudo /usr/sbin/useradd -m $username");

    open(PASSWD, "|sudo /usr/bin/passwd $username");
    print PASSWD $password;
    print PASSWD $password;
    close(PASSWD);
}

close(FILEHANDLE);
HTH

Forrest
 
Old 03-27-2008, 02:01 PM   #9
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Original Poster
Rep: Reputation: 17
It didnt seem to work. It shows this error

Quote:
Enter new UNIX password: Retype new UNIX password: Sorry, passwords do not match
passwd: Authentication information cannot be recovered
passwd: password unchanged
With my earlier code

Quote:
#!/usr/bin/perl -w

use strict;
my @line;
open(FILEHANDLE, "<file.txt") or die "cannot open file for reading: $!";

while(@line = <FILEHANDLE>){

my $pass = crypt($line[1],"password");
system("sudo /usr/sbin/useradd -m -p $pass $line[0]");


}

close(FILEHANDLE);
I think, this line

Quote:
system("sudo /usr/sbin/useradd -m -p $pass $line[0]");
the useradd -p $pass doesnt take an array. So is there any other way, I could retrieve the password from file.txt, without doing it by array.
 
Old 03-27-2008, 02:15 PM   #10
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
You can do it the same way I am, line by line.
Change it to

Code:
#!/usr/bin/perl -w

use strict;
my $inputFile = "file.txt";
open(FILEHANDLE, "<$inputFile") or die "cannot open file for reading: $!";

while(<FILEHANDLE>){
    my $username = $_;
    my $password = <FILEHANDLE>;
    my $pass = crypt($password,"password");
    system("sudo /usr/sbin/useradd -m -p $pass $username");
}

close(FILEHANDLE);
The problem with your while method is that it will ONLY allow you to have one username and password in the file (well, you can have more, but they won't be looked at). Also, I don't think you will be able to log into the system as the crypt is most likely not using the same encryption technique as the passwd command (It does NOT work on my system as the shadow entry is only 13 characters long instead of the desired 34 characters.

HTH

Forrest

Last edited by forrestt; 03-27-2008 at 02:48 PM.
 
Old 03-27-2008, 02:31 PM   #11
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
Info from http://sial.org/howto/perl/password-crypt/ has shown me what you probably need to change:
Code:
#!/usr/bin/perl -w

use strict;
use Crypt::PasswdMD5 qw(unix_md5_crypt);

my $useMD5 = 1;
my $inputFile = "file.txt";
my @salt = ( '.', '/', 0 .. 9, 'A' .. 'Z', 'a' .. 'z' );

open(FILEHANDLE, "<$inputFile") or die "cannot open file for reading: $!";

while(<FILEHANDLE>){
    my $username = $_;
    my $password = <FILEHANDLE>;

    # generate traditional (weak!) DES password, and more modern md5
    my $des_passwd = crypt( $password, gensalt(2) );
    my $md5_passwd = unix_md5_crypt( $password, gensalt(8) );

    if ($useMD5 == 1) {
        system("sudo /usr/sbin/useradd -m -p $md5_pass $username");
    } else {
        system("sudo /usr/sbin/useradd -m -p $des_pass $username");
    }
}

close(FILEHANDLE);

# uses global @salt to construct a RANDOM salt string of requested length
sub gensalt {
    my $count = shift;
    my $salt;
    for (1..$count) {
       $salt .= (@salt)[rand @salt];
    }
    return $salt;
}
HTH

Forrest

Last edited by forrestt; 03-27-2008 at 02:36 PM.
 
Old 03-27-2008, 02:34 PM   #12
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Original Poster
Rep: Reputation: 17
Actually the crypt works. check this code, I put a value for the password in this script itself

Quote:
#!/usr/bin/perl -w

use strict;
my @line;
open(FILEHANDLE, "<file.txt") or die "cannot open file for reading: $!";

while(@line = <FILEHANDLE>){

my $newpass = 123;
my $pass = crypt($newpass,"password");
system("sudo /usr/sbin/useradd -m -p $pass $line[0]");


}

close(FILEHANDLE
but when its not functioning when the password is retrieved from file.txt, put it in a array and use that array.
 
Old 03-27-2008, 02:34 PM   #13
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Original Poster
Rep: Reputation: 17
I wil try ur latest post nw..didnt see it earlier
 
Old 03-27-2008, 02:46 PM   #14
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Original Poster
Rep: Reputation: 17
an error
Quote:
Can't locate Crypt/PasswdMD5.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.8.8 /usr/local/share/perl/5.8.8 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.8 /usr/share/perl/5.8 /usr/local/lib/site_perl .) at ./ya4.pl line 4.
BEGIN failed--compilation aborted at ./ya4.pl line 4
do i have to install Crypt/PasswdMD5.pm
 
Old 03-27-2008, 02:52 PM   #15
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
Yes, you have to install it (it is giving me the same error, and I'm attempting the install now).

Run:
Code:
sudo perl -MCPAN -e "install Crypt::PasswdMD5"
HTH

Forrest

Last edited by forrestt; 03-27-2008 at 03:14 PM. Reason: fixed MCPAN entry
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM
Perl Script to add user to a group AgentRn007 Programming 4 06-21-2006 09:20 PM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM
Adding multiple user shell script plexus Programming 2 06-19-2004 08:36 PM
my adding user script seems weird and buggy nzx Programming 1 10-21-2003 07:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration