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?
In reply to Net::SSH2 channel returns no output by rama133101
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |