Perl 5.8 Get Stdout from SSH command

1.6k Views Asked by At

I need to create a script that will work on CentOS 5.x and CentOS 6.x boxes. CentOS 5.x uses Perl 5.8 and CentOS 6.x uses Perl 5.10. The goal is to be able to ssh into a box, that has a key exchange in place, then run python -V, to determine if the default version is python 2.6.

I'm guessing if I get a script that works with Perl 5.8, that it'll work for 5.10 as well. I made some progress with Net::SSH:Any to have to throw it away, as it looks like it works with Perl 5.12 and newer.

I've tried IPC::System::Simple and qx as well, but haven't had luck capturing the output.

Some of my failed attempts:

Fail 1:

use IPC::System::Simple qw(system systemx capture capturex);
my $output = capture("/usr/bin/ssh root\@10.100.10.56 python -V");
print "out: " . $output . "\n";

Fail 2:

my $output = qx(ssh root\@10.100.10.56 python -V);
print "out: " . $output . "\n";

Fail 3:

my @output = qx(ssh root\@10.100.10.56 python -V);
print "@output\n";

I'm not sure if the call of ssh is playing with anything and am in desperate need of a sanity check. When the command is run, the output is shown on the screen, but not stored in the variable, which I can do substring checks against. The $output variable is left blank. If I'm missing something, please let me know. Thanks :)

1

There are 1 best solutions below

7
On

Figured it out from ThisSuitIsBlackNot's advice. I found Getting STDOUT, STDERR, and response code from external *nix command in perl which showed me how to update my code to get stderr too, which is as follows:

my $output = qx(ssh root\@10.100.10.56 python -V 2>&1);
print "out: " . $output . "\n";

This gave me what I needed. Thx!