ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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..
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
#!/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.
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'
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]");
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.
#!/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;
}
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.