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

Dear Monks,

My scenario involves 3 machines. I run my script on HostC. This script sends commands to HostA to be executed on HostA or sends commands to HostA ansking it to execute them on HostB (for example C asks A do do ls -la or C asks A to ask B to do ls -la). Not reaching HostB directly from C is a constraint I have to live with.

To do this, I send my commands like this:

ssh account@HostA my_command
Then HostA decides if it executes the command or forwards it to HostB (or well, the command for HostA is to fwd another command to HostB).

I used to use the 'system' command and everything was just fine up to the moment when I got interested in the result of one of these commands (as in I wanted to get a string back, not only the exit code of the operation). Reading that 'system' returns the exit code received from the SSH command, suddenly I became interested in printing the result I was interested in, capturing the output and parsing it for my keywords.

Since I was interested in the output, I have moved from using the 'system' call to using back ticks ` ` and my call is now something like

@result = `ssh account@HostA my_command`;
And then I browse the lines in @result looking for my answer. This works OK for the HostC-HostA interaction but when HostA needs to forward the command to HostB and collect the response the same way I get the error "Pseudo-terminal will not be allocated because stdin is not a terminal."

As a side note, when I was using the 'system' call I was able to follow on HostC the debug messages from both HostA and HostB when the command was forwarded. But in this case I cannot get my hands on the output.

Any idea how/if this can be fixed? Please feel free to ask me additional questions if I missed to specify something.

Many thanks!

Replies are listed 'Best First'.
Re: Perl, Unix terminals, and SSH complicated combination
by shmem (Chancellor) on Dec 13, 2007 at 23:37 UTC
    How is my_command forwarded to HostB? Do you do ssh account@HostA "ssh account@HostB my_command" or is the forwarding done via some scripting in .bashr or .profile ? or is my_command itself a script invocation which spawns a ssh session on HostB ?

    Meanwhile, maybe a ssh chain with agent forwarding (see option -A in the ssh manpage) would be a solution, maybe the thread ssh chain helps.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      shmem,

      Your question pointed my in the right direction and I solved the problem without looking at that page (I'll read it soon as I am sure has interesting stuff for me to learn).

      It turns out I need to use back ticks when I invoke a remote command AND print the output, not only return it. If I print the output, that makes it catchable in a cascaded chain of calls. When I execute the command locally (the command is the real stuff, not just a requirement for forwarding) I execute it with 'system' and thus my "parent" 's back ticks can capture the output.

      To summarize: capture with back ticks and also print it for chaining. I was happy to learn this and wanted to share.

      Thanks again!

Re: Perl, Unix terminals, and SSH complicated combination
by almut (Canon) on Dec 13, 2007 at 23:53 UTC

    You probably need to use ssh's -t option to force pseudo tty allocation.