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

Net::SSH2 Command Timeout Before Completion

by taim (Initiate)
on Jun 29, 2010 at 15:38 UTC ( [id://847141]=perlquestion: print w/replies, xml ) Need Help??

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

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?

Replies are listed 'Best First'.
Re: Net::SSH2 Command Timeout Before Completion
by sierpinski (Chaplain) on Jun 29, 2010 at 17:20 UTC
    Take a look at Net::SSH::Expect... I use it quite a lot, and it works really well. It has configurable timeouts for connections, and many other options. Below is an example from one of my scripts:
    my $ssh = Net::SSH::Expect->new ( host => "$serverlist[$host]", user => "$user", raw_pty => 1, restart_timeout_upon_receive => 1, timeout => 5, ssh_option => " -x -o ConnectTimeout=4", log_file=> "/tmp/$serverlist[$host].log", );
    One difference is that this module expects to use an SSH key instead of an interactive password. I don't know if that's an issue for you or not.

      I will be using a key eventually (I hope). I will give it a bit of a test. Thanks.

Re: Net::SSH2 Command Timeout Before Completion
by salva (Canon) on Jun 29, 2010 at 18:42 UTC
    If you have a recent version of OpenSSH installed on the machine where you want to run the script, try Net::OpenSSH:
    use Net::OpenSSH; my $ssh = Net::OpenSSH->new('root@192.168.123.5'); $ssh->error and die "unable to connect to remote host: ". $ssh->error; my $fh = $ssh->pipe_out("/sbin/service", "syslog", "restart") or die "command failed: " . $ssh->error; print while <$fh>; close $fh or die "command failed: $?";

      Another one I will look at. OpenSSH is my client/server of choice.

        Another one I will look at. OpenSSH is my client/server of choice.

        For what it's worth, Net::SSH::Expect lets you choose what ssh binary to use, so if you have openssh installed separately, it can be told to use that. Not trying to be a fanboy, just letting you know.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://847141]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-25 06:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found