using passwd in a perl script

Y

yo

I have the following perl script which works fine.

#!/usr/bin/perl

open(INFILE, "@ARGV[0]");
$infile = <INFILE>;
($username,$password) = split(/\|/, $infile);
close(INFILE);

@calladduser = ("/usr/sbin/adduser $username -g 503 -d
/home/mailusers/$username
-s /sbin/nologin -p $password","\n");
system("@calladduser");


my problem is im using shadow passwords and adduser -p doesn't use
crypt on the password.

How can i use the passwd command in this perl script to use the value
from $password without asking for user input. Another words i dont
want it to be interactive.

im not very good with perl and any help would be great

TIA, P
 
A

A. Sinan Unur

yo said:
I have the following perl script which works fine.

#!/usr/bin/perl

You should always have

use strict;
use warnings;

in your script
open(INFILE, "@ARGV[0]");

You should always, yes always, check the return value of open to see if
it succeeded.

That single line contains at least three "errors".
@calladduser = ("/usr/sbin/adduser $username -g 503 -d
/home/mailusers/$username
-s /sbin/nologin -p $password","\n");
system("@calladduser");

This is sick. Do you take pleasure in torturing the readers of your
code?

perldoc -f system

See also:

perldoc -q always
perldoc -q shadow

http://learn.perl.org/

Please read the posting guidelines for this group.

Sinan
 
Y

yo

sorry im new to do and not a perl programmer.

i solved my problem with:


solved the problem with

@cryptpasswd = ("echo $password | passwd $username --stdin");
system("@cryptpasswd");


yo said:
I have the following perl script which works fine.

#!/usr/bin/perl

You should always have

use strict;
use warnings;

in your script
open(INFILE, "@ARGV[0]");

You should always, yes always, check the return value of open to see if
it succeeded.

That single line contains at least three "errors".
@calladduser = ("/usr/sbin/adduser $username -g 503 -d
/home/mailusers/$username
-s /sbin/nologin -p $password","\n");
system("@calladduser");

This is sick. Do you take pleasure in torturing the readers of your
code?

perldoc -f system

See also:

perldoc -q always
perldoc -q shadow

http://learn.perl.org/

Please read the posting guidelines for this group.

Sinan
 
J

Joe Smith

yo said:
solved the problem with

@cryptpasswd = ("echo $password | passwd $username --stdin");
system("@cryptpasswd");

That's not right. The last line should be without quotes:

system(@cryptpasswd);

Better yet, a simple scalar fits your needs:

$cryptpasswd = "echo $password | passwd $username --stdin";
system $cryptpasswd;

-Joe
 
A

Anno Siegel

Joe Smith said:
That's not right. The last line should be without quotes:

system(@cryptpasswd);

Better yet, a simple scalar fits your needs:

$cryptpasswd = "echo $password | passwd $username --stdin";
system $cryptpasswd;

This effectively publishes the password on most Unix systems. A user
who catches the "echo" in ps will also see the password. Your system
commands, including their parameters, are open for everyone to see,
something to think of when using system(). It is safer to open a pipe
to the passwd command (untested):

open my $pwd, '| passwd $username --stdin' or die "passwd command: $!";
print $pwd $password;
close $pwd or die "passwd returned $?";

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,474
Latest member
AntoniaDea

Latest Threads

Top