You probably want to take a look at the Expect
module, which will let you handle the program as if you were
typing it from the command line. Generally, sleeping like
that isn't a safe way to go, as you have no idea whether
the program is ready for input or not, and no way of
verifying that the input it's prompting for is the input
you're about to give it. Expect will let you wait for a
certain prompt, such as (on my box)
(current) UNIX password:
Alternatively, you could take a look at IO::Tty,
and use it to set up a pty, which will make the passwd
program behave as if you were sitting at a terminal, but
this is basically what Expect is doing behind the scenes.
The problem you were running into may be related to OS
buffering issues. You might try something like (code taken
from Programming Perl):
select((select(ADD), $| = 1)[0]);
which would turn on autoflushing on the ADD filehandle.
I'd still recommend using Expect though, as it provides a
much more robust and flexible way to accomplish your goal.
Update: I should have looked around a bit
for this first, but you also might want to take a look at
Unix::PasswdFile and/or Passwd::Solaris. | [reply] [d/l] [select] |
If you're going to use the built in passwd command, you probably will need to go with something like
expect, as plaid stated.
But depending on what flavor of unix you're using, there may be another option which would lend itself to batch processing more easily than the passwd command.
On FreeBSD (I'm not sure about other unices), you might
want to check out the commands pw and
chpass. They let you do various
password file munging operations completely from the
command line. You'll need to
encrypt the password manually, unfortunately, but that's not too tricky: perldoc -f crypt for more information.
Another option, which I personally prefer not to use, would be
to have perl change /etc/passwd and /etc/master.passwd manually, and then
rebuild the password databases with pwd_mkdb. That's
how we used to do things in the olden days, but you need to
be very careful about things like file locking, or you discover
There's More Than One Way To Shoot Yourself In The Foot.
Alan
Update: Oops, apparently it's still the Olden Days around here. I'll have to tell The Other Guy about chpass. Not that I don't trust his code, but why reinvent the wheel?
| [reply] [d/l] [select] |
you could do something prompt them for a password, grab the salt from the encrypted password in the shadow file, use the crypt function with that salt, if it matches, prompt for the new password, get a random salt and use the crypt() function to create the new encrypted password, then write it to the shadow file. | [reply] |
Take a look at Brink, available from freshmeat. It's a Perl CGI script that changes users passwords from a web page, but the guts of the code would probably do what you want. | [reply] |
yah iv seen it, but I am kinda scared to trust my perl script to deal directly with the password file which contains. 100's of users.
| [reply] |
Brink actually does all it's work on a copy of the passwd (actually shadow) file, but I guess it's still working on the file.
| [reply] |