Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

More IPC::Open2 stuff

by raflach (Pilgrim)
on Jul 05, 2000 at 20:48 UTC ( [id://21159]=perlquestion: print w/replies, xml ) Need Help??

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

Ok this question came to me after reading Interprocess Communication.
That question covered something I have been trying to do for a long time, namely the ability to feed information to a process that won't accept standard shell input redirection. i.e. telnet ( when it asks for the password )
So I attempted to implement the Open2 code given in response here, and ran into this problem:
The first time I attemt to read from the read filehandle, the program blocks until broken. I tried this with other programs as well, and the same thing occurs on any program that requires user input. It works fine on programs like hostname that do not require user input. Here is the code that I am using:
#!/usr/local/bin/perl use IPC::Open2; $SIG{CHLD} = sub { $i = 1 }; local ( *RD, *WR ); print "Opening $ARGV[0]\n"; my $child = open2(\*RD, \*WR, "$ARGV[0]") || die "couldn't open $ARGV[ +0]"; print "Finished opening $ARGV[0]\n"; $i = 0; while (1) { $prompt = <RD>; print $prompt; last if ( $i = 1 ); $cmd = <STDIN>; $cmd1 = "'".$cmd."'"; print WR "$cmd"; }
NOTE: The code is not complete, but I wasn't going to bother adding things like alrm handlers and whatnot until I knew that this would at least work in a normal situation.

Replies are listed 'Best First'.
Re:
by DrManhattan (Chaplain) on Jul 05, 2000 at 23:28 UTC

    If you just need a way to telnet from Perl, Net::Telnet works nicely. Here's a simple example that logs into my redback and prints out its version info.

    #!/usr/bin/perl use Net::Telnet; use strict; my $hostname = "redback"; my $username = "admin"; my $passwd = "password"; # Instantiate the telnet object my $router = new Net::Telnet ( Prompt => '/.*>$/' ); # Open the connection $router->open($hostname); # Log in $router->login($username, $passwd); # Get some info my @version = $router->cmd('show version'); # Close the connection $router->close(); # Print the output print @version; # EOF

    - Matt

Re: Untitled question
by ahunter (Monk) on Jul 05, 2000 at 21:45 UTC
    Hmm, the buffering problems outlined in my select tutorial (here) also apply to open2, and indeed any other type of file that can block. If you're reading from multiple files, you can use the Multiplex package presented there (and you can use it to implement timeouts, too). Use sysread and syswrite to write to the filehandles returned by open2 (if you're only reading from one filehandle, you don't really need the complexities of using select, and $SIG{ALRM} will let you interrupt something that's blocking)

    Setting the filehandle to nonblocking is probably not the answer to this, but it may help with the buffering problem. If you do set the nonblocking flag, use select to avoid having to poll the filehandle.

    Use sysread to read one byte at a time, or set the filehandle nonblocking and read (say) 256 at a time, but use select to wait for more to arrive.

    Andrew.

    (Hrm, I think untitled nodes should be given default names so we can click on them in newest nodes)

Re:
by raflach (Pilgrim) on Jul 05, 2000 at 20:55 UTC
    Oops, no title... Oh well...

    I also should mention that by setting $/=\1; I can get it to produce the first prompt from telnet, read in the first input, and produce the second output up to the first newline, and which point (after the newline not before) it hangs.

Re:
by c-era (Curate) on Jul 05, 2000 at 21:44 UTC
    I always use Comm.pl (it's much easier). I haven't used open2, but you might want to try unbuffering it ($|=1).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2024-04-23 10:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found