简体   繁体   English

Perl 5.8从SSH命令获取标准输出

[英]Perl 5.8 Get Stdout from SSH command

I need to create a script that will work on CentOS 5.x and CentOS 6.x boxes. 我需要创建一个可在CentOS 5.xCentOS 6.x机器上使用的脚本。 CentOS 5.x uses Perl 5.8 and CentOS 6.x uses Perl 5.10 . CentOS 5.x使用Perl 5.8 ,CentOS 6.x使用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 . 目的是能够将ssh放入一个有密钥交换的盒子中,然后运行python -V以确定默认版本是否为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. 我猜如果我得到一个可以与Perl 5.8一起使用的脚本,那么它也将适用于5.10。 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. 我在Net::SSH:Any A上取得了一些进步,因为它看起来可以在Perl 5.12和更高版本上使用,所以必须丢弃它。

I've tried IPC::System::Simple and qx as well, but haven't had luck capturing the output. 我也尝试过IPC::System::Simpleqx ,但是没有运气来捕获输出。

Some of my failed attempts: 我的一些失败尝试:

Fail 1: 失败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: 失败2:

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

Fail 3: 失败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. 我不确定ssh的调用是否正在处理任何事情,是否急需进行健全性检查。 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. $output变量保留为空白。 If I'm missing something, please let me know. 如果我缺少什么,请告诉我。 Thanks :) 谢谢 :)

Figured it out from ThisSuitIsBlackNot's advice. 从ThisSuitIsBlackNot的建议中弄清楚了。 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: 在perl中发现从外部* nix命令获取STDOUT,STDERR和响应代码,这向我展示了如何更新代码以获取stderr,如下所示:

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

This gave me what I needed. 这给了我我所需要的。 Thx! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM