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

How do I call sh command that the prompts for a password (excusing security implications). I've tried:

open2(\*Reader,\*Writer, "/usr/tools/prog start"); Writer->autoflush(); print Writer <<"!"; password !


to no avail. The program called works like this:
>prog start
>password:
>pw
TIA

Replies are listed 'Best First'.
Re: calling a sh command which then asks for a password
by atcroft (Abbot) on Oct 18, 2002 at 12:16 UTC

    It might be easier for you to consider using either the Expect or Expect::Simple modules for this.

    If the program in question is making a connection to a remote device, such as telnet to a router or ssh to a server, then you may wish to look at a module specifically for that, such as Net::Telnet or Net::SSH/Net::SSH::Perl.

    Others perhaps can help more-I look forward to their comments-but I hope that helps a little. Good luck, and please let us know how it goes and what works for you.

Re: calling a sh command which then asks for a password
by MZSanford (Curate) on Oct 18, 2002 at 12:54 UTC

    The problem maybe the leading newline produced after the "!"; ... if you try the following it may work better :

    open2(\*Reader,\*Writer, "/usr/tools/prog start"); Writer->autoflush(); print "password\n";

    This may still not work. Programs built for security (like ssh) often open a new TTY to prevent snooping ... if that's the case, it also prevent STDIN redirections like this.


    from the frivolous to the serious
Re: calling a sh command which then asks for a password
by joe++ (Friar) on Oct 18, 2002 at 14:41 UTC
    Let's assume that you will be connecting over ssh. Then making shh connect without asking for a passwd can be an option. See my note on Connecting to machines for references of how to do this.

    Note: this is not a Perl solution, you may have to talk to your sysadmins!

    --
    Cheers, Joe

Re: calling a sh command which then asks for a password
by Anonymous Monk on Oct 18, 2002 at 15:42 UTC
    It's not to connect to another machine using ssh, it's just to run a program on the same box that requires a password in order to do so.There must be a simple way ?
    TIA
      It's not to connect to another machine using ssh, it's just to run a program on the same box that requires a password in order to do so.There must be a simple way ?
      In the general case, no. A lot of programs that ask for passwords do not read them from the standard input, they open /dev/tty instead. For those, you'll have to use a telnet/ssh solution.

      That is, unless they have a command line option to give the password.