如何使用“系统”调用抑制perl上ssh的FATAL输出?

时间:2012-05-07 18:35:44

标签: perl ssh messages suppress

我正在尝试编写一个使用ssh的简单脚本,但我想测试ssh是否正常工作 - 如果它不起作用我想知道它为什么不起作用。

以下是我正在尝试的内容:

my $output = system("ssh -q -o ConnectionTimeout=3 -o BatchMode=yes $host \"echo $host\"");
my $real_output = $output/256;
if($real_output == 2){print "RESPONSE: $host is not working, No address associated to the name \n";}
elsif($real_output == 255){print "RESPONSE: $host is not working (connection timed out after 3 secs, it exists but does not respond) \n";}

这有效:它收集错误并测试连接,但是当我运行它时;当主机名不存在时显示以下内容:

./testMachines --n invalid_host
warning: Connecting to invalid_host failed: No address associated to the name
RESPONSE: invalid_host is not working, No address associated to the name

或当主机名确实存在但超时时:

./testMachines --n timeout_host
ssh: FATAL: Connection timed out.  No protocol greeting received in 10 seconds.
RESPONSE: timeout_host is not working (connection timed out after 3 secs, it exists but does not respond)

如何抑制“ssh:FATAL”和“警告”消息?我的印象是ssh的'-q'选项会起作用,但它没有。

我也尝试过ssh上的'-q -q'选项(如手册页所示),但没有运气。

我也试过这个:

my $output = system("ssh -q -o ConnectionTimeout=3 -o BatchMode=yes $host \"echo 2>&1\"");
没有任何运气......任何想法?

基本上我想要的是输出是这样的(没有FATAL和警告ssh消息):

# ./testMachines -n host
RESPONSE: host is not working (connection timed out after 3 secs, it exists but does not respond)

非常感谢!!!

1 个答案:

答案 0 :(得分:1)

不确定如何添加所有转义的想法,但这应该有效:

my $output = system("ssh -q -o ConnectionTimeout=3 -o BatchMode=yes $host \"echo $host\" 2>/dev/null");