处理并行telnet连接的最佳方法是什么?

时间:2013-06-24 08:52:36

标签: perl parallels

通过Perl处理与cisco设备的并行telnet连接的最佳方法是什么。 我需要打开几个telnet连接以保持后台并提供命令 以交互式或定时批处理方式。这可以用anyevent或POE库来实现吗?

感谢。

2 个答案:

答案 0 :(得分:1)

线程很头疼。事件循环(如AnyEvent)更简单,性能更高,特别是如果您想以定时方式提交命令并需要处理数千个连接。

请参阅AnyEvent :: Socket,了解如何打开连接并阅读&写数据:http://metacpan.org/pod/AnyEvent::Socket

你也可以使用Net :: Telnet作为支持使用已经打开的文件句柄:http://metacpan.org/pod/Net::Telnet#fhopen

如果您遇到AnyEvent问题,只需提出一个新问题。

答案 1 :(得分:0)

最简单的方法是使用踏板。您可以使用“队列”来发送命令并来回接收输出。

你可以简单地创建x个线程,然后排队很多命令并发送它们。

如果你需要处理输出,有点麻烦。

http://metacpan.org/pod/Thread::Queue

它也可以通过基于事件的模块来解决,但是需要一种非常不同的方法。这样你就可以创建一个非线程函数,然后轻松地将它转换为线程函数。

#without processing the output
use strict;
use warnings;

use threads;
use Thread::Queue;

my $q = Thread::Queue->new();    # A new empty queue
my $maxThreads = 20;
# Create Worker threads
for (1..$maxThreads){
  my $thr = threads->create(
    sub {
        # Thread will loop until no more work
        while (defined(my $cmd = $q->dequeue())) {
            do_someting($cmd);
        }
    }
  );
}

# Send work to the threads
$q->enqueue($cmd1, ...);
# Signal that there is no more work to be sent
$q->end();
# Join up with the thread when it finishes
$thr->join();