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

I have a command line program that prompts for user input. Is there a way to automate entering the input using perl? A good example of a similiar program is Runas (windows). You cannot pass in the password when it is run but have to enter it manually. Is there a way to start the program and then pass the password in after it prompts for it?
http://www.basgetti.com

Replies are listed 'Best First'.
Re: passing in User Input
by davidrw (Prior) on Jun 12, 2005 at 01:19 UTC
    I haven't used it personally, but Win32::CtrlGUI may very well help you out -- use system to kick off Runas (or whatever commandline program), and use Win32::CtrlGUI to find the resulting dos window by name, and send it the password (possibly after a sleep).
      Isn't there a way to use open to do this? This does not work but it gets close... What am I missing?
      my $cmd=qq|runas /profile /user:$user "$exe"|; open(FH,"| $cmd") || die $^E; print FH "$pass\r\n"; close(FH);
      http://www.basgetti.com
        I don't know the answer to your question. But there are a few things I would check if I had your problem.

        First: do you need to delay before passing the password (as suggested above)? ie. after opening your process put your process to sleep(5); to give your subprogram time to start up and ask the password question?

        Secondly: some programs, like passwd on linux, use tricks to ensure you can't just pass input via STDIN - this is to prevent nasty hacks that bypass security. To get around this a language called TCL/Expect is commonly used for applications that need to call passwd (the trick used is to log in again and execute the program through a login shell). That is why I suggested a Perl-equivalent, Expect. It is possible that the program you're using needs special handling like the linux passwd program.

Re: passing in User Input
by monarch (Priest) on Jun 12, 2005 at 01:39 UTC
    Depending on how much interaction you have to face, you may want to consider using the Expect module.