Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Unix password change

by pbinstead (Initiate)
on Mar 27, 2003 at 11:21 UTC ( [id://246213]=perlquestion: print w/replies, xml ) Need Help??

pbinstead has asked for the wisdom of the Perl Monks concerning the following question:

Hi, How can I change a unix password from a perl program. I can't seem to find any reference to how to do this. Any help would be much appreciated

Replies are listed 'Best First'.
Re: Unix password change
by Abigail-II (Bishop) on Mar 27, 2003 at 12:43 UTC
    That's because non-interactive changing of passwords is a dangerous thing. Don't go there unless you're absolutely sure what you're going to do. If you mess up, *NOONE* will be able to log anymore, and you'll need to do some massive recovery. Do you have your backup tapes ready? Do you know which steps to take to recover your system? Have to checked that your backup procedure is actually working? Can you read from your backup tape?

    Here's a hint (I ain't going to spoon feed you on things changing the passwords, if you need handhelding, you shouldn't be doing this). /usr/bin/passwd reads directly from the (pseudo)-terminal, not from STDIN.

    Abigail

Re: Unix password change
by defyance (Curate) on Mar 27, 2003 at 12:41 UTC
    This node may be what your looking for. It talks about a few different ways to do what you need. Personaly, I'd go for the first option, which uses Expect.pm. Before you venture down that road, however, make sure you have a copy of your /etc/passwd made, just to be safe.

    -- Can't never could do anything, so give me and inch, I'll make it a mile.

Re: Unix password change
by bronto (Priest) on Mar 27, 2003 at 12:36 UTC

    If you want to interface yourself to the passwd program you should use some Expect module. Take a look on CPAN, I can't suggest any Expect module because search.cpan.org is currently unreachable for me.

    Otherwise, you can calculate a "crypt string" by means of the crypt function and then substitute the it to the password string in the /etc/passwd file.

    Maybe this quick-and-dirty script could help you getting started; I use it to calculate a crypt string, given the password:

    #!/usr/bin/perl print "Password? > " ; $_ = <STDIN> ; chomp ; srand ; print crypt($_, join('',('.', '/', 0..9,'A'..'Z', 'a'..'z')[rand 64, rand +64])), "\n" ;

    Ciao!
    --bronto

    Update: just a note: the srand was needed to make the script run correctly on some old IBM machines that ran Perl 4.


    The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
    --John M. Dlugosz
      Thanks for the help with this. Works great :-)
Re: Unix password change
by zby (Vicar) on Mar 27, 2003 at 12:35 UTC
    You can try to use the IPC::Run module to control the passwd program.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://246213]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-18 02:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found