Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2024-04-23 14:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found