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

Hi PerlMonks, I have two text files on the "remote" sever, one is 29864 bytes (about 30 K) and the other is 35498 bytes (about 35 K). And the following code
use strict; use Net::SSH::Perl; my $ssh = Net::SSH::Perl->new('localhost',debug => 1, protocol => '2,1 +'); $ssh->login('username','password'); my ($stdout, $stderr, $exit) = $ssh->cmd('cat /tmp/a 1>&2'); print $stdout . "\n"; print $stderr . "\n"; print $exit . "\n";
Although everything works when the remote program outputs to stdout, when it outputs to stderr, and the output is large, my script hangs. /tmp/a is the smaller file and the script runs to completion. However /tmp/b is the larger file and causes the above to hang. Any information would be helpful. Thank you kind monks.

Replies are listed 'Best First'.
Re: Script hangs when executing command over ssh
by marinersk (Priest) on Jun 18, 2015 at 16:47 UTC

    One oddity I ran into, too long ago to remember the details, was an issue with the way STDERRwas buffered. You may be bumping into that; I wish I remembered more about the problem. Something about STDERRbeing held until the end of the run and then released? Sorry, it's been too long.

    One thing I'm noticing is that your command line is redirecting STDOUTto STDERRusing the 1>&2technique. However, in your invocation, you seem to be wishing to capture $stdoutand $stderrseparately.

    Perhaps you'd have more luck getting rid of the command redirect and simply capturing the data raw with your call to $ssh->cmd?

    my ($stdout, $stderr, $exit) = $ssh->cmd('cat /tmp/a');

      Thank you for your reply. Yes perhaps it does have to do with the STDERR buffering, and it seems to be at about 32K. Do I need to increase that buffer somehow?

      The actual program I am running is outputting to STDERR (and STDOUT) and I have no control over that output. I just gave the above code as an example of the more complicated one.

        If there's a way to work with the buffer, hopefully a more Unix/Linux-oriented Monk will come along with a suggestion. However, I don't think you understood my point about getting rid of the piping.

        Try removing the 1>&2from your command, and see if the problem goes away.

Re: Script hangs when executing command over ssh
by salva (Canon) on Jun 19, 2015 at 06:53 UTC
    That looks like a Net::SSH::Perl Bug. 30k is quite suspecting, the max size of a SSH packet.

    You should try using Net::OpenSSH instead.

Re: Script hangs when executing command over ssh
by eg2014 (Initiate) on Jun 18, 2015 at 18:38 UTC
    Regarding the last comments, I would like to add that I need to distinguish the STDOUT from the STDERR and redirecting STDERR to STDOUT is not an option.

      redirecting STDERR to STDOUT is not an option

      Then it is might be doubly imperative that you stop using 1>&2, because 1>&2causes STDERR STDOUTto be redirected to STDOUT STDERR.