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.
According to the article, it says we can use the crypt function also. The only disadvantage is modern systems can break traditional DES passwords very quickly(according to the article.)
I have tried the crypt() with thw code
Quote:
sr/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
if this works, that means crypt can be used. sure there must be a way, the password can be retrieve from the file.txt and use it in the system("sudo /usr/sbin/useradd -m -p $pass $line[0]");
You HAVE to use the method your operating system uses. If your system uses MD5 passwords, you have to use the Crypt::PasswdMD5 module. If it uses DES, you have to use the standard crypt. I haven't seen an operating system that was installed in many years that used DES.
oic, but why, if i tried that code and it was succesfully added a user and then it can be login to tat user also. That means, modern operating system can still use the DES also. Correct me if I am wrong.
If it is allowing you to log in with the password "123" then it is working, and you are using DES. This also means that your operating system is vulnerable to password attacks.
If you are sure you aren't using MD5 passwords, use this:
Code:
#!/usr/bin/perl -w
use strict;
# location of file storing usernames and passwords
my $inputFile = "file.txt";
# Valid characters for a password salt
my @salt = ( '.', '/', 0 .. 9, 'A' .. 'Z', 'a' .. 'z' );
# open the user/password file
open(FILEHANDLE, "<file.txt") or die "cannot open file for reading: $!";
# read the file until all users are used
while(<FILEHANDLE>){
my $username = $_;
my $password = <FILEHANDLE>;
chomp($username);
chomp($password);
# generate traditional (weak!) DES password
my $passwd = crypt( $password, gensalt(2) );
system( qq(sudo /usr/sbin/useradd -m -p '$passwd' $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;
}
If you aren't sure, run
Code:
sudo perl -MCPAN -e "install Crypt::PasswdMD5"
Then, the following will work (slightly different from last one):
Code:
#!/usr/bin/perl -w
use strict;
use Crypt::PasswdMD5 qw(unix_md5_crypt);
# change to 0 if not using MD5 passwords
my $useMD5 = 1;
# location of file storing usernames and passwords
my $inputFile = "file.txt";
# Valid characters for a password salt
my @salt = ( '.', '/', 0 .. 9, 'A' .. 'Z', 'a' .. 'z' );
# open the user/password file
open(FILEHANDLE, "<file.txt") or die "cannot open file for reading: $!";
# read the file until all users are used
while(<FILEHANDLE>){
my $username = $_;
my $password = <FILEHANDLE>;
chomp($username);
chomp($password);
my $passwd;
if ($useMD5 == 1) {
print "Using MD5";
# generate modern md5 password
$passwd = unix_md5_crypt( $password, gensalt(8) );
} else {
print "Using DES";
# generate traditional (weak!) DES password
$passwd = crypt( $password, gensalt(2) );
}
system( qq(sudo /usr/sbin/useradd -m -p '$passwd' $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;
}
OK, After the Crypt::PasswdMD5 module is installed as above, this will determine the correct password type for your system and then use that to make the users' accounts.
Code:
#!/usr/bin/perl -w
use strict;
use Crypt::PasswdMD5 qw(unix_md5_crypt);
# Location of Commands
my $head = "/usr/bin/head";
my $sudo = "/usr/bin/sudo";
my $useradd = "/usr/sbin/useradd";
my $awk = "/usr/bin/awk";
# location of file storing usernames and passwords
my $inputFile = "file.txt";
my $useMD5;
# Test to see what kind of password entries are being used
my $pwlength=`$sudo $head -1 /etc/shadow | $awk -F: '{print \$2}' |wc -c`;
if ($pwlength == 35) {
$useMD5 = 1;
} else {
$useMD5 = 0;
}
# Valid characters for a password salt
my @salt = ( '.', '/', 0 .. 9, 'A' .. 'Z', 'a' .. 'z' );
# open the user/password file
open(FILEHANDLE, "<file.txt") or die "cannot open file for reading: $!";
# read the file until all users are used
while(<FILEHANDLE>){
my $username = $_;
my $password = <FILEHANDLE>;
chomp($username);
chomp($password);
my $passwd;
if ($useMD5 == 1) {
print "Using MD5\n";
# generate modern md5 password
$passwd = unix_md5_crypt( $password, gensalt(8) );
} else {
print "Using DES\n";
# generate traditional (weak!) DES password
$passwd = crypt( $password, gensalt(2) );
}
system( qq($sudo $useradd -m -p '$passwd' $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;
}
Actually, reading from a filehandle at the top of a loop and in the middle is not really recommended.
There are various ways, depending on the file layout and size e.g. for the file as described:
Code:
file.txt
user1
passwd1
code:
# Open file , grab recs, close it
open( REQ_FILE, "<$infile_name" ) or
exit_with_error("Unable to open file: $infile_name: $!\n");
@file_recs = <REQ_FILE>;
chomp(@file_recs); # remove newline chars
close(REQ_FILE) or
exit_with_error("Unable to close file: $infile_name: $!\n");
# process array here
If you have a 'small' file like this:
Code:
user1,passwd1
user2,passwd2
you can use a similar technique:
Code:
# Open file , grab recs, close it
open( REQ_FILE, "<$infile_name" ) or
exit_with_error("Unable to open file: $infile_name: $!\n");
@file_recs = <REQ_FILE>;
chomp(@file_recs); # remove newline chars
close(REQ_FILE) or
exit_with_error("Unable to close file: $infile_name: $!\n");
# process array here
for $rec ( @file_recs)
{
($user, $passwd) = split(/,/, $rec);
# process values here
}
for a 'large' file :
Code:
open( REQ_FILE, "<$infile_name" ) or
exit_with_error("Unable to open file: $infile_name: $!\n");
while ( defined ( $file_rec = <REQ_FILE> ) )
{
chomp($file_rec);
($user, $passwd) = split(/,/, $file_rec);
# process values here
}
close(REQ_FILE) or
exit_with_error("Unable to close file: $infile_name: $!\n");
Actually, reading from a filehandle at the top of a loop and in the middle is not really recommended.
There are various ways, depending on the file layout and size e.g. for the file as described
You can also do something something for files like this:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.