【问题标题】:how to suppress FATAL output from ssh on perl using the "system" call?如何使用“系统”调用在 perl 上抑制 ssh 的 FATAL 输出?
【发布时间】:2012-05-07 18:35:44
【问题描述】:

我正在尝试编写一个使用 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)

非常感谢!!!

丹

【问题讨论】:

    标签: perl ssh messages suppress


    【解决方案1】:

    不确定您是如何想到添加所有转义的,但这应该可行:

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

    【讨论】:

    • 转义太多! system qq/ssh -q -o ConnectionTimeout=3 -o BatchMode=yes $host "echo $host" 2>&1/ ;-)
    • @Michael Slade,对于我试图调试的所有转义感到抱歉.. 刚刚尝试了您的建议,但“警告”错误仍然显示如下:# ./testMachines -n invalid_host 警告:连接到 invalid_host失败:没有与名称关联的地址 响应:invalid_host 不起作用(没有与名称关联的地址) 我只想要 RESPONSE 部分
    • @mob ... 不知道你说的那些 "qq/" 是什么意思 我根据你的回答试过这个:my $output = system(ssh -q -o ConnectionTimeout=3 -o BatchMode=yes $host "echo $host" 2>&1); 但这是我得到的错误: ./testMachines -n invalid_host Bareword found where operator expected at ./testMachines line 57, near "q -o ConnectionTimeout=3 -o" (Do you need to predeclare q?) Number found where operator expected at ./testMachines line 57, near ""echo $host" 2" (Missing operator before 2?) syntax error at ./testMachines line 57, near "q -o ConnectionTimeout=3 -o BatchMode"
    • @all 刚刚尝试过:my $output = system("ssh -q -o ConnectionTimeout=3 -o BatchMode=yes $host \"echo $host\" 2>&1"); 但仍然显示 FATAL 和警告错误:s
    • @Michael Slade - 你的意思是2>/dev/null?
    猜你喜欢
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 1970-01-01
    相关资源
    最近更新 更多