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 | |
by rama133101 (Novice) on Apr 25, 2015 at 20:44 UTC | |
by salva (Canon) on Apr 27, 2015 at 06:39 UTC |