Following the many examples out there, I came up with the following basic code which works 90% of the time. The only time I have a problem is with long-running processes. For example, restarting the syslog service on RHEL 5 doesn't complete before the script closes the channel and exits.

#!/usr/bin/perl use strict; use warnings; use Net::SSH2; use Term::ReadKey; my $host = '192.168.123.5'; my $user = 'root'; my $pass = getpass(); my $ssh = Net::SSH2->new(); $ssh->connect($host) or die $!; $ssh->auth_password($user,$pass) or die $!; my $chan = $ssh->channel(); $chan->shell(); print $chan "/sbin/service syslog restart"; print "$_" while <$chan>; $chan->close; $ssh->disconnect;
I tried setting "blocking" to "0" as suggested in several locations. This didn't work. I set "poll(10000)" and it appears to have ignored the timeout wait. I set debug on ssh and here is what I get (note no output):
$ ./ssh.pl Password: libssh2_channel_open_ex(ss->session, pv_channel_type, len_channel_type +, window_size, packet_size, ((void *)0) , 0 ) -> 0x1b10be0 Net::SSH2::poll: timeout = 10000, array[0] Net::SSH2::poll: timeout = 250, array[1] - [0] = channel - [0] events 1 - libssh2_poll returned 0 - [0] revents 0 Net::SSH2::Channel::DESTROY Net::SSH2::DESTROY object 0x1a055a0

Seeing as I will ultimately be using keys, might it be best if I just drop Net::SSH2 in favor of ssh via system?


In reply to Net::SSH2 Command Timeout Before Completion by taim

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.