#!/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 line 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";