Forum Moderators: coopster & phranque

Message Too Old, No Replies

Perl to change a users password

Help with making a perl script that changes linux users passwords

         

Muppet

2:20 pm on Sep 20, 2003 (gmt 0)

10+ Year Member



I am tryng to do is make a perl scrip that changes user passwords on a redhat system. My perl scripting skills are not very good not to put to finer point on things.

I tried;

system("passwd $user");
system("$password");
system("$password");

which does not work, as it mearly returns a password promt for the username. Is there a way to emulate a keyboard input for this, or directly encrypt the password, and write it to the user /etc/passwd file and avoide using the passwd command?

Any ideas, suggestions, hints code examples would be appreciated :)

JollyK

6:46 pm on Sep 20, 2003 (gmt 0)

10+ Year Member



Hi. On some systems, there's a "chpasswd" command (generally in /usr/sbin, but it might be elsewhere). The chpasswd command takes username:newpassword on STDIN with no prompting. So something as simple as:

open(CHPW, "¦/usr/sbin/chpasswd") ¦¦
die "Could not open chpasswd: $!\n";
print CHPW "login:newpassword\n";
close(CHPW);

Will change login's password to "newpassword." It will also encrypt "newpassword" for you.

Note, however, that the command needs to be run by root or it won't work.

Muppet

1:12 am on Sep 21, 2003 (gmt 0)

10+ Year Member



Thanks JollyK the system does have the command on it and in /usr/sbin so it works :).

I am not entirely sure how the “¦”s work and I am presuming the $! variable is the a system error variable that perl picks up.

Does anyone have any recommendations about a good perl book, preferably on that covers system related things like this and database work?

Storyteller

3:40 am on Sep 21, 2003 (gmt 0)

10+ Year Member



Muppet, Passwd::Linux module from CPAN will do just what you want.

For learning Perl, get O'Relly's "Perl Cookbook", and maybe "Learning Perl" if you're new to the whole programming thing. "Effective Perl Programming" and O'Relly's "Perl Programming" will make a good reading as you progress.

wruk999

8:16 am on Sep 21, 2003 (gmt 0)

10+ Year Member



I am not entirely sure how the “¦”s work

The WebmasterWorld software changes standard pipes (without breaks) to broken pipes, eg: ¦

You must remember to change these back to standard pipes in your code, unless, JollyK, they are supposed to be broken?

wruk999

JollyK

8:31 pm on Sep 22, 2003 (gmt 0)

10+ Year Member



No, they're not supposed to be broken!

Sorry, I didn't even notice that they'd been changed. I guess I should copy/paste my code from the preview window and try to run it before posting in future. Nice catch, wruk999, and thanks!

Muppet: yes, generally $! contains the text of an error you get opening the file/command. E.g. "Permission denied" or "No such file or directory," etc. "die" exits the script at that point, and in this case, prints that error message. No sense writing to the pipe if it hasn't been opened.

I also recommend "The Perl Cookbook" wholeheartedly, and O'Reilly's "Programming Perl" (or is it "Perl Programming" now? I have an older edition.) is an excellent reference. Can't go wrong with most O'Reilly books.