passwd and perl 
Author Message
 passwd and perl

Hi!
I wrote a script to create  a new user with a random password using
Perl, but i have a problem with the "passwd" command:
passwd $user<$password doesn't work
Any tip?
Thanks
alex


Tue, 02 Mar 2004 08:46:02 GMT  
 passwd and perl

Quote:

> Hi!
> I wrote a script to create  a new user with a random password using
> Perl, but i have a problem with the "passwd" command:
> passwd $user<$password doesn't work
> Any tip?

passwd varies from Unix to Unix.  Some verions will accept input from
STDIN and others won't.  This even extends to different distros of
Linux.  Your best bet is probably to use one of the numerous recipes
that exist on-line and use a combination of crypt () and whatever
/etc/shadow equivalent you're dealing with.

Regards,

Mr Q. Z. D.
----
Drinker, systems administrator, wannabe writer, musician and all-round bastard.
"And when I'm dead
 If you could tell them this;
 What was wood became alive." - Suzanne Vega, Kaspar Hauser's Song.



Tue, 02 Mar 2004 09:37:27 GMT  
 passwd and perl

Quote:
>I wrote a script to create  a new user with a random password using
>Perl, but i have a problem with the "passwd" command:
>passwd $user<$password doesn't work

That would pipe the contents of a file named like the value of $passwd
to the passwd command, which is probably not what you're trying to
achieve.

Rather, you'd have to do something like

open( PASS, "|/usr/sbin/passwd $user") or
  die "Can't spawn passwd: $!\n";

print PASS "$passwd\n";
print PASS "$passwd\n";

close PASS;

which will probably not work either, because most version of passwd I
know will only accept interactive input. You can make some versions
work if they have a switch that makes them read from STDIN.

So the best method would actually be to write your own passwd entry
and crypte the password yourself using Crypt::Passwd, which is
available from CPAN:
http://search.cpan.org/doc/LUISMUNOZ/Crypt-Passwd-0.03/Passwd.pm

HTH,
--

split//,unpack('u*','L#`T&)QD5#0`#!!`#%1D)#08`#P05!!(3``$$"``#"0L&``('.
'"`P<!`````0$`'));$t=~s/(\d)(.)/$2x$1/eg;map{$t.=substr$t,$i,1,''while



Tue, 02 Mar 2004 09:48:14 GMT  
 passwd and perl

Quote:

> I wrote a script to create  a new user with a random password using
> Perl, but i have a problem with the "passwd" command:
> passwd $user<$password doesn't work

Check out Expect.  I'm pretty certain that there is a Perl port of it but it
originated for Tcl/Tk.

Changing/Setting passwords is one of the examples.
--

tollfree: 1-87-pro-linux                        thornhill, ontario, canada
http://www.starnix.com              professional linux services & products



Tue, 02 Mar 2004 14:33:44 GMT  
 passwd and perl

Quote:

> Hi!
> I wrote a script to create  a new user with a random password using
> Perl, but i have a problem with the "passwd" command:
> passwd $user<$password doesn't work

First off, passwd generally requires that you enter the particular
password twice -- the second time is to confirm that you typed it right
the first time.  Second, <$password means to read from the file named
$password, when you probably want the contents of the $password variable
as the password [not a file of that name].  So, *if* your passwd accepts
input from stdin, then you could do something like:

        open  CHPASSWD, "|passwd $user" or die horribly;
        print CHPASSWD (($passwd, "\n") x 2);
        close CHPASSWD or die horribly;

If passwd *doesn't* accept data from stdin, and will only work from a
terminal, then you need to create a psuedo-terminal.  The simplest way
to do this is to write an expect script, either with the 'real' expect,
or with perl's Expect.pm:
        use Expect;
        my $chpasswd = Expect->spawn( "passwd", $user );
        print $chpasswd (($passwd, "\n") x 2);
        close $chpasswd or die horribly;
Yes, I realize this uses none of Expect's functionality besides creating
a tty, but this is all that's needed.  

--
"I think not," said Descartes, and promptly disappeared.



Tue, 02 Mar 2004 14:35:25 GMT  
 passwd and perl
Hi,
First, thankyou for your help.
In fact i just want to use the command "passwd" with Perl, and when it
asks for password, gives the value $password without interactive
prompt.


Tue, 02 Mar 2004 15:24:30 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Executing passwd from perl to update passwd file?

2. Perl 5.001 and passwd form Perl book

3. creating new linux user passwd using perl

4. passwd in perl

5. changing passwd with perl (UNIX)

6. Username Passwd in Perl

7. analyse passwd on Perl?

8. passwd in perl on solaris

9. login/passwd in perl

10. CODE POSTING: Taking a userid and passwd in perl ...

11. encrypt() create the same passwd string for the same passwd :(

12. perl binary pack and passwd echo off from windows

 

 
Powered by phpBB® Forum Software