in reply to Exec Fork Trick

The big problem is that /bin/passwd uses your TTY instead of STDIN/STDOUT to interact with the user. The other response(s) mentioned Expect, which is a good general purpose solution to the problem.

Here's another idea:

I'm not sure if OpenBSD supports PAM yet (FreeBSD does..). If it does, you can use Authen::PAM to change passwords. A while back, I'd submitted a a node showing how to use Authen::PAM to change a password.

Replies are listed 'Best First'.
Re: Re: Exec Fork Trick
by andreychek (Parson) on Jul 01, 2001 at 21:05 UTC
         The big problem is that /bin/passwd uses your TTY instead of STDIN/STDOUT to interact with the user.
    
    This is absolutally true in most versions of passwd. However, some of the more recent versions of passwd provide you with an option to accept input from STDIN. This is the case with passwd that came with RedHat 7.1, and is packaged as version 0.64. It's described in the man page (man passwd), but in short all you have to say is

    passwd --stdin

    So ginseng, to modify the code you wrote to make use of this, you could do:
    # The runtime options to be passed to passwd my $passwd_options = "--stdin"; # All the arguments to be passed to passwd, including name my @args = qw($passwd_options $name); open (PASSWD, "|-") || exec 'passwd', @args; print PASSWD "$password\n$password\n"; close PASSWD;
    Again though, remember that you need a recent version of passwd. Just do a 'man passwd' and check to see if the --stdin option exists. Good luck,
    -Eric