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

Heelo Perl Monks,

I try to connect to a network appliance using Net::Openssh. Simple commands like ls /tmp or cat /etc/hosts are working.

But I try to capture the output from a proprietary command, called dbedit. The appliance have a standard Openssh server installed, version OpenSSH_4.3p2, OpenSSL 0.9.8b 04 May 2006 The server cannot be updated. here my test programm:

#!/usr/bin/perl use Net::OpenSSH; my $host = "10.100.110.51"; my $user = "admin"; my $pass = "my_pw"; #$Net::OpenSSH::debug=-1; my $ssh = Net::OpenSSH->new(host=>$host, user=>$user, password=>$pass, master_opts => [-v, -o => "StrictHostKeyCh +ecking=no"], ); $ssh->error and die "Couldn't establish SSH connection: ". $ssh->error +; my $xxx = "source /opt/CPshrd-R77/tmp/.CPprofile.sh ; /opt/CPsuite-R7 +7/fw1/bin/dbedit --h"; #my (@ls,$errput) = $ssh->capture2({tty => 1},"/opt/CPsuite-R77/fw1/ +bin/dbedit --h"); my (@ls,$errput) = $ssh->capture2({tty => 1},$xxx); $ssh->error and die "remote ls command failed: " . $ssh->error; print"LS:@ls\n"; print"ERR:$errput\n";

As you can see by the code. I already tried some hacks to solve the problem. But any additional option I integrated into the code. Did not solve the problem. Interesting ist that the Debug-Output is showing the output from the command, but it seems this output did not get back through the tunnel to the ssh client. Please finde following the debug output from a connection establishment test:

Is there anybody out there who can help me to capture the output from this command.

ciao ryder

Replies are listed 'Best First'.
Re: Q: Cannot get the capture from connection with Net::Openssh?
by salva (Canon) on May 05, 2015 at 08:25 UTC
    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.

      @salva, thanks for the good explenation. This solved the problem for me. Many thanks. ciao ryder