AnyEvent下的多个telnet客户端/命令 [英] Multiple telnet clients/commands under AnyEvent
问题描述
如果我通过相同的telnet连接读取多个$_->{thand}->cmd($cmd)
命令,则表示断开连接.
If I'm reading multiple $_->{thand}->cmd($cmd)
commands through same telnet connection, I've got disconnects.
这表现为多次调用ae_connect()
.如何在AnyEvent
下正确发送和获取数据?
This manifests as multiple calls to ae_connect()
. How to properly send and fetch data under AnyEvent
?
use strict;
use warnings;
use Data::Dumper;
use AnyEvent::Socket;
use AnyEvent;
use Net::Telnet;
$| = 1;
my $arr = [
{ username => "user+ct", passwd => "1234", Host => "92.242.254.8", Port => 1094 },
{ username => "user+ct", passwd => "1234", Host => "92.242.254.8", Port => 1095 },
{ username => "user+ct", passwd => "1234", Host => "92.242.254.8", Port => 1096 },
];
sub main_loop {
my $cmd = "/ip firewall filter export";
my $i=0;
for (@$arr) {
if (!$_->{thand}) {
ae_connect($_);
print("skip ", Dumper $_);
next;
}
# print Dumper $_;
$i++;
my $s;
$s = join "", $_->{thand}->cmd($cmd);
# print "\n==1>$i \n$s";
$s = join "", $_->{thand}->cmd($cmd);
$s = join "", $_->{thand}->cmd($cmd);
}
print "\n\n";
#die @$arr*1 if $i;
}
sub ae_connect {
my ($tc) = @_;
print "=========== $tc->{Host} ============\n";
tcp_connect $tc->{Host}, $tc->{Port} //23, sub {
my ($fh) = @_ or return; # die "failed: $!";
#
my $t = new Net::Telnet->new(Fhopen => $fh) or return;
eval { $t->login($tc->{username}, $tc->{passwd}) } or return;
$t->timeout($tc->{Timeout});
$tc->{thand} = $t;
# $tc->{fh} = $fh;
};
}
my $w = AnyEvent->timer(after => 0, interval => 1, cb => \&main_loop);
my $cv = AnyEvent->condvar;
$cv->recv;
推荐答案
我认为AnyEvent和Net :: Telnet不能一起使用. AnyEvent基于事件,您具有多个事件源,并且将根据事件源来处理新数据,而Net :: Telnet仅具有单个文件句柄并阻止其等待当前所需的确切数据.
I don't think AnyEvent and Net::Telnet will work together. AnyEvent is event based where you have multiple event sources and will act on new data depending on the event source, while Net::Telnet just has a single file handle and blocks waiting for exactly the data it needs at the moment.
如果需要并行的多个Net :: Telnet连接,则需要使用多个进程或多个线程,或者您可以尝试使用协程(例如Coro模块).
If you need multiple Net::Telnet connections in parallel you need to use multiple processes or multiple threads or you might try to use Coroutines (e.g. Coro module).
这篇关于AnyEvent下的多个telnet客户端/命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!