1

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 :)

6
  • 2
    python -V may write to STDERR instead of STDOUT. Also, why not just use a different module for SSH like Net::OpenSSH? Commented Sep 26, 2014 at 21:52
  • I looked into SSH, SSH2, and OpenSSH. I've tried to simplify things and just run the command directly to get things working. Is there a way to get qx to capture stderr too? Commented Sep 26, 2014 at 21:54
  • 2
    There is. See How to capture both STDOUT and STDERR in two different variables using Backticks in Perl. However, you should really avoid using bacticks/system when there is a pure Perl equivalent. Net::OpenSSH has this nice system method that lets you control STDIN, STDOUT, STDERR, and more. Commented Sep 26, 2014 at 22:02
  • 1
    I don't know what you're talking about. Net::OpenSSH passes all tests on CPAN Testers for 5.8.x. I think you're just looking at the license terms, which have nothing to do with whether a module will run on earlier Perl versions. Commented Sep 26, 2014 at 22:18
  • 1
    IIRC, Net::SSH::Any only requires perl 5.8. Commented Sep 29, 2014 at 8:22

1 Answer 1

2

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!

Sign up to request clarification or add additional context in comments.

7 Comments

I'm glad I don't have that layer of complexity to have to fight with too :) Might work with cygwin, but would be a side project :)
My point is, Python is cross-platform, so there's no reason your command to check the version should be restricted to a single OS. If you use a module like Net::OpenSSH instead of backticks, you don't have to worry about OS-specific quoting and I/O redirection. my @cmd = qw(python -V); $ssh->system({ stderr_to_stdout => 1 }, @cmd) or die $ssh->error;
@ThisSuitIsBlackNot: Net::OpenSSH does not work on Windows, not even under Cygwin Perl. That's the main reason that pushed me to create Net::SSH::Any.
@salva You mean it doesn't run on Windows, correct? I'm just talking about connecting to a remote Windows machine. Of course, I've never tried it so I could just be talking out my you-know-what.
@ThisSuitIsBlackNot, yes, Net::OpenSSH can connect to Windows machines. Version 0.62 also got support for quoting commands for Win32 and cmd.exe.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.