Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

redirecting stdin on windows

by arkamedis21 (Acolyte)
on Aug 20, 2002 at 18:12 UTC ( [id://191539]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I am looking for a way to redirect stdout and stdin of an interactive program. This program asks me for a username and a password. It looks like follow on the command prompt.

username :
password :


where the user enters his password and username. Where the user first enters his username and than password, and then the prgram exits. I wanted to redirect stdout and stdin in such a way that my perl program could run this small program for me, where the username, and the password are stored in my perl program as variables. I will be running this script on windows 2000. I am new to perl, any help would be greatly appreciated.

Thanks.

Replies are listed 'Best First'.
Re: redirecting stdin on windows
by fsn (Friar) on Aug 20, 2002 at 20:36 UTC
    Like, I don't know anything about Perl, Win32 and IPC, but if it were Unix, I would use some Expect stuff to handle this. The open FIFO, "/path/to/program|"; might actually work, at least on my Unix boxes.

    But as I said, I really haven't been dealing with Win32 much. However, according to CPAN the Expect module should be working with cygwin perl.

    And you do know about the security implications of storing a cleartext password in a script, dontcha?

      I know, I won't be storing the password in the perl script. I already have a perl/Tk GUI that takes the username and password, and is supposed to call this command line utility that stores the password in the windows registry.
Re: redirecting stdin on windows
by defyance (Curate) on Aug 20, 2002 at 18:31 UTC
    Well, I don't like to give it away, but i'll try to point you in the right direction:
    #!/path/to/perl -w use strict; my $uname; my $passwd; print "username:"; chomp($uname = <STDIN>); #catch stdin, get rid of trailing newline print "Your Username is: $uname"; #print it out print "password:"; chomp($passwd = <STDIN>);

    Alternativly, if your wanting to pass these vars to another program, you could do this:

    #!/path/to/perl -w use strict; my $uname; my $passwd; print "username:"; chomp($uname = <STDIN>); print "password:"; chomp($passwd = <STDIN>); system("/path/to/prog", "$uname", "$passwd");

    It all depends on what you want to do with it, if you wanna elaborate on what it is you want to do, we could help with it, there's just some basic ideas. Also, you say you are new to Perl, You should check out the Tutorials, also, a book called "Learning Perl", an O'reilly Book, devoted to learning the rich language we call Perl.

    UPDATE:Fixed a syntax error, heh.

    --~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--

    perl -e '$a="3567"; $b=hex($a); printf("%2X\n",$a);'

      No I don't want to write a program that gets the username or password, there is this program there already that runs interactively, which prompts the user fro a username and a password. I was trying to get a way to pass a username and a password from a perl script to this executable. This executable doesn't take any command line options and runs interactively as I have shown above.

      I tried doing the following, but it fails for some reason:

      open (FIFO, "$program |");
      print FIFO "$username\n";
      print FIFO "$passwd\n";
      print FIFO "$passwd\n";
      print FIFO "y\n";
      close FIFO || die "error closing redirector \n";


      The program looks like the following again. <BR
      username :
      password :
      confirm :
      Do you want the action to be persistent (y/n) :

      The code I have above doesnt run well for some reason. I am trying to make it so the user runs the perl script and doesnt see any output by this program, and the perl script enters the username and password for him.
        I meant

        open (FIFO, "| $program");
        Woah, okay, lets see, what exactly is in $program? and why are you open();ing it, your not going to be able to send anything to $program unless you do something like this:
        open(FIFO), ">$program") || die "Darn : $!"; #will give you a good rea +son why it can't open. the ">" says, write to this file. print FIFO "$username\n"; and so on.....
        Thats assuming that $program is a writable file, and your printing the values to it..

        Am I misunderstanding what you are trying to do?

        --~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--

        perl -e '$a="3567"; $b=hex($a); printf("%2X\n",$a);'

Re: redirecting stdin on windows
by myocom (Deacon) on Aug 20, 2002 at 22:14 UTC

    What you're asking to do doesn't require Perl at all - the shell can already do it. Create a text file with the username on one line and the password on the next <insert speech about how you shouldn't store passwords in plaintext>. Then, you can simply do myexe.exe < answer.txt where answer.txt is the "answer file" you created.

    If you really wanted to get Perl involved for some other reason, you could do this same thing from a call to system.

    "One word of warning: if you meet a bunch of Perl programmers on the bus or something, don't look them in the eye. They've been known to try to convert the young into Perl monks." - Frank Willison
      I tried doing

      command < input_file

      but it only works if the command asks for one input entry, in windows.

        Funny, it works for the following (on WinXP):

        script.pl
        print "username: "; chomp ($uname = <STDIN>); print "\npassword: "; chomp ($password = <STDIN>); print "\nYou entered username $uname and password $password\n";
        answer.txt
        someusername somepassword
        And I'm calling it like this:
        perl script.pl < answer.txt
        ...which produces this:
        C:\test\scratch>perl script.pl < answer.txt username: password: You entered username someusername and password somepassword
        ...which is the expected output.
        "One word of warning: if you meet a bunch of Perl programmers on the bus or something, don't look them in the eye. They've been known to try to convert the young into Perl monks." - Frank Willison
Re: redirecting stdin on windows
by rinceWind (Monsignor) on Aug 21, 2002 at 09:00 UTC
Re: redirecting stdin on windows
by John M. Dlugosz (Monsignor) on Aug 20, 2002 at 18:45 UTC
    A few years ago, I wrote a magazine article detailing how to mix "main" and "WinMain" concepts in a Win32 program, and how the differences weren't exactly clear cut. I don't have a copy handy, alas.

    However, I use stdout and stderr in GUI programs all the time, for debugging/logging. It's harmless and does nothing unless the program was started with redirected output. I think stdin is the same way: if you have a GUI program and don't supply a file handle when it's started (i.e. "redirect" it), it is connected to nothing. A Console program, on the other hand, will create a new console the first time it is needed, it it wasn't hooked up to anything (because it was launched by a GUI process or with flags telling it not to).

    Hmm, on rereading, I think you don't need any of that. You are asking about ordinary command-line programs, right? Your problem is that | won't give you both input and output at the same time?

    In that case, see the module IPC::Open2. It does exactly this.

    —John

      I tried that I don't think it works on windows.
        The comment in the open3.pm file (which open2 calls) says, "...ported to Win32 by Ron Schmidt,..."

        There are issues about ActiveState Perl launching outside programs at all; you may have a problem unrelated to the double piping.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://191539]
Approved by footpad
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2024-04-19 07:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found