我尝试使用 IO 循环将并发请求 (5) 发送到主机池 (3),但代码在 3 个请求后停止。我得到了帮助来启动这段代码,但我现在当然理解了其中的大部分内容。我不明白的是为什么处理的请求数量与我的主机池中的主机数量相关联。该代码的目标是确定来自给定 IP 的路由信息。
use strict;
use warnings;
use Net::OpenSSH;
use IO::Async::Loop;
use Future::Utils 'fmap_concat';
my @hosts = qw(host1 host2 host3);
my @ssh;
my $user = 'myuser';
my $pass = 'mypassword';
foreach my $host (@hosts) {
my $ssh = Net::OpenSSH->new(host => $host, user => $user, password => $pass, master_opts => [-o => "StrictHostKeyChecking=no"]);
die "Failed to connect to $host: " . $ssh->error if $ssh->error;
push @ssh, $ssh;
}
my @ipv4 = (
'ip1','ip2','ip3','ip4','ip5'
);
my $loop = IO::Async::Loop->new;
my $future = fmap_concat {
my $ip = shift;
my $ssh = shift @ssh;
my $cmd = 'show ip route '.$ip.' | i \*';
my @remote_cmd = $ssh->make_remote_command($cmd);
return $loop->run_process(command => \@remote_cmd)
->transform(done => sub { [@_] })
->on_ready(sub { push @ssh, $ssh });
} generate => sub { return () unless @ssh and @ipv4; shift @ipv4 }, concurrent => scalar @ssh;
my @results = $future->get;
foreach my $result (@results) {
my ($exit, $stdout) = @$result;
print $stdout, "\n";
}
这是结果
Connection to host1 closed by remote host.
Connection to host2 closed by remote host.
Connection to host3 closed by remote host.
* ip1, from host1, 3w0d ago, via GigabitEthernet0/0/0
* ip2, from host2, 7w0d ago, via GigabitEthernet0/0/0
* ip3, from host3, 3w0d ago, via GigabitEthernet0/0/1
最佳答案
研究该问题后,我发现 Cisco 等网络设备在处理同一连接上的多个请求时可能存在问题。因此,代码做了一些更改,每次调用 future 时都会打开一个新连接,而不是使用预先打开的连接池。
use strict;
use warnings;
use Net::OpenSSH;
use IO::Async::Loop;
use Future::Utils 'fmap_concat';
my @hosts = qw(host1 host2 host3);
my @ssh;
my $user = 'myuser';
my $pass = 'mypassword';
my @ipv4 = (
'ip1','ip2','ip3','ip4','ip5'
);
my $loop = IO::Async::Loop->new;
my $future = fmap_concat {
my $ip = shift;
my $host = shift @hosts;
my $ssh = Net::OpenSSH->new(host => $host, user => $user, password => $pass, master_opts => [-o => "StrictHostKeyChecking=no"]);
die "Failed to connect to $host: " . $ssh->error if $ssh->error;
my $cmd = 'show ip route '.$ip.' | i \*|^Routing entry';
my @remote_cmd = $ssh->make_remote_command($cmd);
return $loop->run_process(command => \@remote_cmd)
->transform(done => sub { [@_] })
->on_ready(sub { push @hosts, $host ; });
} generate => sub { return () unless @hosts and @ipv4; shift @ipv4 }, concurrent => scalar @hosts;
my @results = $future->get;
foreach my $result (@results) {
my ($exit, $stdout) = @$result;
print $stdout, "\n";
}
但这导致了底层 openssh 库的其他问题。 当再次在 $host 上调用 future 时,似乎存在竞争条件,ssh 连接未正确释放。 undef $ssh 修复了它
->on_ready(sub { undef $ssh; push @hosts, $host ; });
关于使用 IO::Async 和 Future::Utils 并发请求的 Perl 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59671087/