【发布时间】:2013-02-06 02:49:17
【问题描述】:
我正在使用 Openssh 模块通过 (async => 1) 选项连接到主机。 如何为那些无法连接的主机捕获连接错误。我不希望错误出现在终端中,而是存储在数据结构中,因为我想最终将所有数据格式化为 cgi脚本。当我运行脚本时,有连接问题的主机在终端中抛出错误。代码进一步执行并尝试在断开连接的主机上运行命令。我想隔离断开连接的主机。
my (%ssh, %ls); #Code copied from CPAN Net::OpenSSH
my @hosts = qw(host1 host2 host3 host4 );
# multiple connections are stablished in parallel:
for my $host (@hosts) {
$ssh{$host} = Net::OpenSSH->new($host, async => 1);
$ssh{$host}->error and die "no remote connection "; <--- doesn't work here! :-(
}
# then to run some command in all the hosts (sequentially):
for my $host (@hosts) {
$ssh{$host}->system('ls /');
}
$ssh{$host}->error and die "no remote connection doesn't work"。
任何帮助将不胜感激。 谢谢
【问题讨论】:
-
考虑使用Net::OpenSSH::Parallel。它会为您处理所有这些低级细节!
标签: perl perl-module