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

Hi all!

I am using the Expect module to establish and manage an ssh connection to execute some commands and read the answer from a server.

Up to the moment, my script executes a fixed list of commands. I've been using a hard-coded list of commands or a file containing the list.

But now I need to add commands to the list during run time. Furthermore, the program should know immediately if a new command has arrived and execute it (if there weren't other commands waiting). And it should not stop listening to the ssh connection while waiting for these new commands. Commands that come from another Perl process in the same computer.

So I need IPC to allow the Expect-running process to get and immediately detect new commands; while at the same time this Expect-running process is expecting on the ssh shell.

My first question is: Which kind of IPC should I use to transmit the new commands? Maybe a pipe? a socket?

I guess there are more than one valid way to do that... but recommendations are welcome!

The second question is: How can I achieve the coordination?

I read that Expect may listen to multiple process at the same time. So maybe there is a way to establish a pipe or similar from the process that sends new commands to the Expect-running process, and make the last use Expect on that pipe to get the new commands (at the same time it is expecting on the ssh shell). But I'm not sure whether Expect may work also with pipes (or filehandles in general) or if it works only with commands.

In case that can't be done, which is the best way (compatible with Expect) to communicate between processes that a new command is ready on the list.

Thanks in advance and sorry for the long text!

  • Comment on Inter-process communication when using Expect.pm

Replies are listed 'Best First'.
Re: Inter-process communication when using Expect.pm
by Illuminatus (Curate) on Mar 12, 2010 at 14:18 UTC
    expect.pm says that the object returned by the new method is really an IO::Pty object. You should be able to use this like any other IO::Handle within IO::Select. If you are not familiar with select, then you have some reading to do.

    You can then get your 'new commands' from anything with base type IO::Handle, while at the same time monitoring what your expect session is doing. CAVEAT - I have not actually tried this (although I'm sure ikegami will undoubtedly 10min after I post this and tell me I'm wrong :)), and I'm not sure what select will return if you are using an expect call with a timeout and it times out. It may say 'readable' or 'exception'. You will have to experiment.

    fnord

Re: Inter-process communication when using Expect.pm
by ssandv (Hermit) on Mar 12, 2010 at 17:28 UTC
    It sounds like you're communicating with another computer, which almost certainly means using a socket. A pipe is just a pair of filehandles (one for reading, one for writing) tied together on a local machine.