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

Hello monks,
I've read in the Camel Book that you could call open with a command, let's say netstat, with a pipe, like this :
open(NETWORK, "netstat -i -n |");
But it seems that when you call open with a pipe (to get the output of netstat, like in the previous sample code, for example), the opened FileHandle is write-only, or read-only, you can't write and read at the same time ... It isn't "interactive".
What I'm looking for is a way to execute rlogin, and then execute a set of shell commands on the remote host, as well as getting their output ...
Is there some trick to do with the open call, or is this method not the right one ?
Is there another way than using the open call ?

Thanks for your answers

Replies are listed 'Best First'.
Re: open with a command
by Transient (Hermit) on Jun 14, 2005 at 14:54 UTC
    From open:
    If the filename begins with '|' , the filename is interpreted as a command to which output is to be piped, and if the filename ends with a '|' , the filename is interpreted as a command which pipes output to us. See "Using open() for IPC" in perlipc for more examples of this. (You are not allowed to open to a command that pipes both in and out, but see IPC::Open2, IPC::Open3, and "Bidirectional Communication with Another Process" in perlipc for alternatives.)


    (emphasis mine)
Re: open with a command
by moot (Chaplain) on Jun 14, 2005 at 14:54 UTC
    Well you could mess around with the techniques in perlipc, but it seems like Expect might be a good fit for what you're trying to do.
Re: open with a command
by davidrw (Prior) on Jun 14, 2005 at 14:56 UTC
Re: open with a command
by northwind (Hermit) on Jun 14, 2005 at 15:38 UTC

    What you are looking for is a way to do IPC, correct?  If you are wanting to learn the details of how to do IPC (sort of like using LWP vs. WWW::Mechanize), there are two standard modules that will help you do this:  IPC::Open2 and IPC::Open3.  Be forwarned though, these are waters frought with deadlock and race conditions.

    Of the two modules mentioned, IPC::Open2 is marginally safer to use (if you don't mind ignoring the other processes STDERR).  Also, the filehandle order is different for each module:
      $pid = open2(\*READ_FH, \*WRITE_FH, "command -args");
      $pid = open3(\*WRITE_FH, \*READ_FH, \*ERR_FH, "command -args");

Re: open with a command
by Tanalis (Curate) on Jun 14, 2005 at 14:51 UTC
Re: open with a command
by Argel (Prior) on Jun 14, 2005 at 18:57 UTC