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

Dear Monks,

I am attempting to execute a windows program that expects input from the user once started. I have tried using system and pass the value as one of the arguments, but this did not work because the program will not accept this data as a parameter.

The program in question is "runas". It is run like this:

runas /user:username "command to execute"

Once that command is started, it prompts for a password. The design of the tool does not allow the password to be passed as a parameter. (I am on Windows 2000)

I have tried this without success:

system('runas /user:username "net localgroup administrators %username% +@%userdnsdomain% /ADD"', 'password');

The response I get is:
*Enter password for oitcontractor\administrator:RUNAS ERROR: Unable to accept input
*'administrator' is not recognized as an internal or external command, operable program or batch file.

So then I thought I should try putting the password in backticks on the next line, and that doesn't work either. I know this is a long post for what should be a short question, but I wanted to show you that I have tried to solve this before asking you.

I did a supersearch for "windows program input" and found this, which mentions Win32::Console but I have not been able to figure out how to use it. Can anyone confirm that Win32::Console can accomplish this and point me to some decent examples. I read the perldoc on it and I am a bit confused. If it cannot then are there any other solutions?

Thanks, disciple

Replies are listed 'Best First'.
Re: Pass expected input to Windows program
by Thelonius (Priest) on Apr 02, 2004 at 20:50 UTC
Re: Pass expected input to Windows program
by quinkan (Monk) on Apr 03, 2004 at 02:14 UTC
    If your app uses windows routines, look at Win32::GuiTest, which allows you to get window focus, push buttons, make entries in text boxes etc. (with copious use of "sleep(1)" for pauses etc). It has a SendKeys method which has worked well for me in a lot of places. It's in CPAN, but the latest, more extensive (more experimental) version (1.50, last I checked) is available from Files option if you join the guitest group. Doco is limited, but the group records hold explanations for many things, and examples.
      Just to clarify, "runas" is a command line utility used to execute a command as a different user. It comes with Win2000.
Re: Pass expected input to Windows program
by ant9000 (Monk) on Apr 03, 2004 at 09:32 UTC
    I have used sanur to solve this very same problem: it's not Perl, but it works like a charm :-)
    HTH,
    Ant9000
      I also found CPAU which is a replacement for the "runas" command. It works just fine. However, I was hoping to figure this out in Perl for future reference.
Re: Pass expected input to Windows program
by Plankton (Vicar) on Apr 02, 2004 at 21:11 UTC
      While the module Expect would basically do what he wants, unfortunately Expect doesn't work on any win32 platform ( as far as I've seen.. ).
Re: Pass expected input to Windows program
by mce (Curate) on Apr 05, 2004 at 10:01 UTC
    The Perl module Win32::AdminMisc has API's for stuff like CreateProcessAsUser and more. This is very easy to implement, and gives you all tricks needed to solves your problem.

    ---------------------------
    Dr. Mark Ceulemans
    Senior Consultant
    BMC, Belgium
Re: Pass expected input to Windows program
by amrangaye (Friar) on Apr 05, 2004 at 11:59 UTC

    Hi. If you do not need the output from your runas program, you could try opening an output filter to the command, then printing the password to it:

    open(PROGRAM,"| runas /user:username command"); print PROGRAM "password\n";
      It didn't work for me. I tried this:
      open(PROGRAM, '| runas /user:username "net localgroup administrators +%username%@%userdnsdomain% /ADD"'); print PROGRAM "password\n";
      This is the output:

      Enter password for username:RUNAS ERROR: Unable to accept input