in reply to Q: Cannot get the capture from connection with Net::Openssh?

The remote command returns a failure error code (87) and Net::OpenSSH sets the error flag to OSSH_SLAVE_CMD_FAILED. Then, in...
$ssh->error and die "remote ls command failed: " . $ssh->error;
The die instruction is actually invoked and the print sentences below never reached.

Instead, write it as follows:

use Net::OpenSSH; use Net::OpenSSH::Constants qw(:error); ... my ($output, $errput) = $ssh->capture2({tty => 1}, $xxx); if ($ssh->error and $ssh->error != OSSH_SLAVE_CMD_FAILED) { die "remote ls command failed: " . $ssh->error; } print "OUT: $output\n"; print "ERR: $errput\n"; print "RC: $?\n";

Note also that in Perl, (@array, $scalar) = whatever() is never what you want because @array "eats" all the values returned by whatever(), and $scalar always becomes undef.

That's the reason why Net::OpenSSH->capture2(...) returns just two scalars in all the occasions, while Net::OpenSSH->capture(...) can be more clever and return lines in list context or one scalar with the full output in scalar context.

Replies are listed 'Best First'.
Re^2: Q: Cannot get the capture from connection with Net::Openssh?
by ryder (Initiate) on May 05, 2015 at 09:54 UTC
    @salva, thanks for the good explenation. This solved the problem for me. Many thanks. ciao ryder