in reply to Printing Output From Net::SSH2 exec

Check Net::OpenSSH, it is far easier to use for this kind of things:
use Net::OpenSSH; my $ssh = Net::OpenSSH->new($remote_host, user => $remote_user, passwo +rd => $password); $ssh->error and die "Unable to connect to $remote_host: " . $ssh->erro +r; my $fh = $ssh->pipe_out('ls -la /Users/vortura') or die "unable to run command: " . $ssh->error; while (<$fh>) { print "OUT: $_"; } close $fh or die "close failed: $?";
The module needs a recent version of OpenSSH (4.2 or later, 5.2 recomended) installed on your system and does not work under Windows.

Replies are listed 'Best First'.
Re^2: Printing Output From Net::SSH2 exec
by vortura (Initiate) on May 14, 2009 at 10:48 UTC
    I hadn't seen that module. I will give it a try. I see it does it's business by running the openssh binary as well, but the multiplexing approach looks a lot nicer than the mess I've inherited.

    Thanks!

      BTW, this is how you read from both stderr and stdout:
      use Net::OpenSSH; my $ssh = Net::OpenSSH->new($host, ...); $ssh->error and die "connection failed: " . $ssh->error; my (undef, $ofh, $efh, $pid) = $ssh->open_ex({ stdout_pipe => 1, stderr_pipe => 1 }, @cmd) or die "ssh command failed: " . $ssh->error; while(1) { # select loop to read from $ofh and $efh without blocking # ... } waitpid($pid, 0); print "result: $?\n";