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

I need to send some commands to remote linux box and fetch responses. I am using Net:SSH2 for creating a session channel and send commands using shell.

The problem I face is that the response I receive is empty. If I add a sleep statement, the output is very well captured. I do not want to add sleep statements as I need to send multiple commands and that would reduce the performance. Please advice what I am missing.

Here is the code.

$session = Net::SSH2->new(); $rc = $session->connect($target_ip, $target_port, Timeout=>4000) ; print "\n rc: $rc"; $session->auth_password($username, $passwd) ; $chan = $session->channel(); $chan->shell() ; print $chan $cmd . " \n" ; my @poll = ({handle=>$chan, events=>['in', 'ext', 'channel_closed']}); $session->blocking(0) ; $session->poll(1, \@poll) ; if ($poll[0]->{revents}->{in}) { while (<$chan>) { $resp .= $_ ; } } print "\nresponse : $resp";

Updates: We found out the issue here.

The problem is, once we receive the cmd output, the EOF is not reaching and so the channel is not getting closed automatically.

Question: How will SSH channel know that the cmd it sent has returned the complete output and when should it close its channel?

Replies are listed 'Best First'.
Re: Net::SSH2 channel returns no output
by Corion (Patriarch) on Apr 25, 2015 at 20:14 UTC

    I think your program is asking too early for output:

    $session->blocking(0) ;

    I think you will need to rework your program logic in a way that it handles results other than the "in" event in a loop. Or you could maybe switch to blocking processing since you will wait for answers from the remote end anyway.

      Thanks for the quick reply

      Here is the full logic.

      $session = Net::SSH2->new(); $rc = $session->connect($target_ip, $target_port, Timeout=>4000) ; $session->auth_password($username, $passwd) ; $chan = $session->channel(); $chan->shell() ; print $chan $cmd . " \n" ; my @poll = ({handle=>$chan, events=>['in', 'ext', 'channel_closed']}); my $job_done = 1; my $status; while ($job_done > 0) { $session->blocking(0) ; $session->poll(1, \@poll) ; if ($poll[0]->{revents}->{in}) { while (<$chan>) { $resp .= $_ ; } $status = 0 ; if ($chan->eof()) { $status = $chan->exit_status() ; } } if ($poll[0]->{revents}->{ext}) { my ($len, $buff) ; while ($len = $chan->read($buff, 1024, 1)) { $resp .= $buff ; } if (! $resp) { $status = $chan->exit_status() || 0 ; } } elsif ($poll[0]->{revents}->{channel_closed}) { while (<$chan>) { $resp .= $_ ; } $status = $chan->exit_status() ; } if ($chan->eof() || $poll[0]->{revents}->{ext} || ($poll[0]->{revents}->{channel_closed} && $status == 0)) { $chan->close ; $job_done = -1; } $session->blocking(1) ; } print "\nresponse : $resp";

      The problem here is, commands that take more than 5 seconds to respond returns null. Whereas, commands that takes less time to respond are returning the exact result

      This is where sleep comes into picture for me

      Please help.

        Using Net::SSH2 reliably is quite hard, specially in non-blocking mode. Also, there were several bugs and mismatches between the underlaying libssh2 and the Net::SSH2 wrapper that I have fixed on the development version, but they are still waiting for a new module release.

        You may like to try Net::SSH::Any with the Net::SSH2 backend instead. The latest stable version (0.04) was able to workaround most of the issues... well, at least it seemed to work most of the time. 0.04, come out before I went into Net::SSH2/libssh2 internals, I wrote it just looking at the docs, with lots of wrong assumptions!!!