in reply to Net::SSH2 odd polling behavior

Linux, Solaris... if you are familiar with system programing under Unix, Net::OpenSSH will be far more easier to use (and BTW, reliable :-)

On Solaris you will have to install the OpenSSH client.

#!/usr/bin/perl use strict; use warnings; use Time::HiRes qw(time); use Net::OpenSSH; use Error; my $start = time; my $ssh = Net::OpenSSH->new($host, user => $user, master_opts => [-i => $key_file]); warn time - $start . ": connected!\n"; my $timeout = 0.250; my $output = ''; my $ofh = $ssh->pipe_out('(for i in $(seq 1 10); do sleep 1 && echo li +ne number $i ; done)'); my $fn = fileno $ofh; while (1) { my $rb = ''; vec($rb, $fn, 1) = 1; if (select($rb, undef, undef, $timeout) > 0) { my $bytes = sysread($ofh, $output, 1024, length $output); last if (!$bytes and (defined $bytes or $! != Error::EINTR())); warn time - $start . ": $bytes bytes read\n"; } else { warn time - $start . ": timeout!\n"; } } close($ofh); print "output:\n$output\n";

Replies are listed 'Best First'.
Re^2: Net::SSH2 odd polling behavior
by sparvu (Initiate) on Sep 16, 2012 at 14:52 UTC

    I tried same thing to implement, a channel timeout, to clean shutdown the ssh2 connection, in a case of a backend failure.

    Probable the timeout should be implemented on the ssh2 socket level rather than the channel itself. I would be curious to see how the author of the thread, iamb, implemented the timeout setting snd and rcv timeouts on the ssh socket.

    At the moment I am using setsockopt($sock, SOL_SOCKET, SO_KEEPALIVE, 1)

    if($ssh2->connect($dest, $port)) { my $sock = $ssh2->sock; setsockopt($sock, SOL_SOCKET, SO_KEEPALIVE, 1) or die "fatal: cant set socket in SO_KEEPALIVE mode $!"; ... }