Perl 检测主机网络TCP连接模块-AnyEvent::Ping::TCP
AnyEvent::Ping::TCP - Asynchronous and Synchronous TCP ping functions.该模块提供了异步与同步这两种方式的tcp ping函数,即对tcp端口进行连接性测试。Latency is always returned in milliseconds, and is provided by Time::HiRes.
延迟总是以毫秒为单位返回,由Time::HiRes来提供。
Socket functionality is provided by AnyEvent::Socket.
Socket功能由AnyEvent::Socket来提供。
默认导出所有的函数。
Synchronous API
$latency = tcp_ping $site, $port [, $timeout]
Measures the time taken to connect to the provided $site:$port and returns it synchronously.$timeout is optional, and defaults to 5 seconds if not provided.
同步地返回连接到$site:$port的时间,$timeout参数是可选的,不指定即为默认的5秒。
Asynchronous API
tcp_ping_syn $site, $port [, $timeout]
Initiates the connection to the provided $site:$port and sets a callback to calculate the latency. Correct latency measurement is not dependant on timely calls to tcp_ping_ack.$timeout is optional, and defaults to 5 seconds if not provided.
启动到$site:$port的连接,并设置回调以计算延迟。正确的延迟测量并不依赖于tcp_ping_ack的调用。$timeout参数是可选的,不指定即为默认的5秒。
If this function is called multiple times for the same $site:$port pair, a counter indicating the number of responses requrested is incremented per call, but additional connections are not initiated - it is therefore safe to call this function on an unsorted list of $site:$port pairs.
如果对同一$site:$port对多次调用此函数,则指示每个调用所需响应数的计数器将递增,但不会启动其他连接-因此可以在$site:$port对的未排序列表上调用此函数。
$latency = tcp_ping_ack $site, $port;
Waits for the latency of the connection to the $site:$port pair. If the connection has already completed, it returns the latency immediately.
等待连接到$site:$port对的延迟。如果连接已经完成,它会立即返回延迟。
This function uses the counter maintained by tcp_ping_syn to know how many responses are expected before cleaning up the memory associated with the ping operation. Again, this allows the calling program to be fairly naive about the lists it uses. All tcp_ping_syn calls for a given $site:$port pair will yield the same latency value until tcp_ping_ack has drained the queue. Only then will a new connection and measurement be taken.
此函数使用由tcp_ping_syn维护的计数器,在清除与ping操作相关的内存之前,确定多少响应。同样这使得调用程序对它使用的列表相当原生。给定$site:$port对的所有tcp_ping_syn调用将产生相同的延迟值,直到tcp_ping_syn队列耗尽。只有这样才能进行新的连接和测量。
异步调用必须使用上面的两个函数,顺序不能有变:tcp_ping_syn发起调用,tcp_ping_ack来等待前者的返回结果;两者间的间隔时间不能太短,同时被检测的项目不能太多,否则会导致结果的丢失或没有结果。
下面将使用它们分开对一台主机前20000个端口进行检测,发现并得到那些打开的端口。
示例1:同步tcp ping
ae.tcping.sync.pl
use v5.20;
use Data::Dumper;
use Time::HiRes qw(time);
use AnyEvent::Ping::TCP;
my @h=('192.168.8.1');
my $rs;
foreach my $h (@h){
foreach my $p (1..19999){
my $usedtime=tcp_ping $h,$p;
$rs->{$h}->{$p}=sprintf "%.2f",$usedtime if($usedtime);
}
}
say Dumper($rs);
$ time perl ae.tcping.sync.pl
$VAR1 = {
'192.168.8.1' => {
'139' => '0.55',
'445' => '0.45',
'22' => '0.45',
'2018' => '0.47',
'80' => '0.44',
'53' => '0.39',
'8118' => '0.51'
}
};
real 0m11.377s
user 0m7.063s
sys 0m1.718s
示例1:异步tcp ping
use v5.20;
use Data::Dumper;
use Time::HiRes qw(time);
use AnyEvent::Ping::TCP;
my @h=('192.168.8.1');
my $rs;
foreach my $h (@h){
foreach my $p (1..9999){
tcp_ping_syn $h,$p,3;
}
}
foreach my $h (@h){
foreach my $p (1..9999){
my $usedtime=tcp_ping_ack $h,$p;
$rs->{$h}->{$p}=sprintf "%.2f",$usedtime if($usedtime);
}
}
say Dumper($rs);
$ time perl ae.tcping.async.pl
$VAR1 = {
'192.168.8.1' => {
'8118' => '1027.28',
'2018' => '2022.67',
'22' => '2270.97',
'445' => '2201.20',
'139' => '2253.08',
'53' => '2266.40',
'80' => '2262.01'
}
};
real 0m3.592s
user 0m3.148s
sys 0m0.433s
异步的方式效率要比同步的方式高出许多,从上面所用的时间片就能看出来。但同时也可能导致很多问题,尤其是在系统的一些重要参数没有经过优化时;如最大文件打开数这个限制上,如果以默认的1024来运行后面的异步程序的话,会发现超过1024之后的端口不会出现在结果中,这个原因我花了不少时间来找,后来突然之间想起可能是这个原因导致的,通过修改最大文件打开数限制之后,运行结果也随之改变。这个问题可能不会被轻易发现,这点要注意。
但将异步脚本中的端口范围值扩大到19999时,会发现没有结果输出。且在脚本工作期间,会将单核的cpu资源吃光。
不多建议使用此模块,尤其是其异步方式。核心模块的Net::Ping套用上多线程并不比它慢。
最新版本:1.01
项目主页:https://metacpan.org/release/AnyEvent-Ping-TCP