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 :)
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.
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?
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.