【问题标题】:Perl script to create new SSH when first SSH aborts第一次 SSH 中止时创建新 SSH 的 Perl 脚本
【发布时间】:2014-02-15 16:43:38
【问题描述】:

第一次海报...

我有一个 perl 脚本(在 Linux 服务器上运行),它与另一台服务器(Linux/Solaris)建立 SSH 会话,然后“执行”许多顺序命令来收集数据。但是,远程服务器随后在数据收集(测试环境)期间被故意关闭,导致“SSHConnectionAborted at ./...”。不幸的是,这也杀死了 perl 脚本。

我的目标是 perl 脚本将识别中止的 SSH 并建立一个到不同服务器(列表中的下一个服务器)的 SSH 以继续收集数据。

我正在使用 Net::SSH::Expect(没有使用 OpenSSH 或 SSH2 的选项)。我使用 1 秒的“超时”,因为脚本需要快速连续发出命令。

请帮忙。

【问题讨论】:

  • 看一些代码会有帮助。

标签: perl ssh


【解决方案1】:

如果你正在做这样的事情:

foreach (@sshTargets) {
    #SSH stuff here...
}

我会将 SSH 方法包装在 eval 中或使用 Try::Tiny 作为 try/catch 块:

use Try::Tiny;
foreach (@sshTargets) {
    my $currentServer = $_;
    #Try to run SSH things
    try {
        #SSH stuff here...
    } 
    #If anything in the try block would cause the script to fail, the catch 'catches' it and prevents crashing. The error is stored in $_
    catch {
        warn "SSH failed on $currentServer because $_";
    };
}

如果由于某种原因你不能使用 Try::Tiny,你可以使用类似的 eval:

foreach (@sshTargets) {
    my $currentServer = $_;
    #Try to run SSH things
    eval {
        #SSH stuff here...
    };
    #If anything in the evalblock would cause the script to fail, the error is stored in $@
    if ( $@ ) {
        warn "SSH failed on $currentServer because $@";
    }
}

【讨论】:

  • 谢谢。 eval 完全符合我的要求。 Try::Tiny 未安装在此服务器上,因此我无法尝试该选项。
猜你喜欢
  • 1970-01-01
  • 2011-02-20
  • 1970-01-01
  • 2014-12-29
  • 2016-08-16
  • 1970-01-01
  • 2016-07-08
  • 2019-05-21
  • 1970-01-01
相关资源
最近更新 更多