首页 > 解决方案 > 对于我正在 ping 的某些域,超时在 Perl 脚本中不起作用

问题描述

我无法弄清楚这里发生了什么。我的示例代码:

#!/usr/bin/perl

use strict;
use Net::Ping;
use Time::Out qw(timeout) ;

my $p = Net::Ping->new();

my $domain = lc("play9112.com");

print qq|Doing $domain \n|;

my $ping_result = timeout 3 => sub {
    return $p->ping($domain,5);
} ;

unless ($ping_result) {
    print qq|Don't seem to be able to grab $domain! \n|;
} else {
    print qq|needs to delete...\n|; # delete is done below!
}

上面的代码适用于大约 99% 的情况。但是有一些域它只是锁定,并且不会“超时”!作为测试,而不是Time::Out,我只用一个普通的eval {}块来测试它:

my $TIMEOUT_IN_SECONDS = 5;
eval {
    local $SIG{ALRM} = sub { die "alarm\n" };
    alarm($TIMEOUT_IN_SECONDS);

    $p->ping($domain,5);

    # do stuff that might timeout.
    alarm(0);
};
if ($@) {
    # handle timeout condition.
}

这似乎有同样的问题。谁能指出我正确的方向?一定有一个怪癖,或者我错过了什么!

更新:我刚刚用 -d 尝试过并得到:

perl -d test-ping.cgi

Loading DB routines from perl5db.pl version 1.51
Editor support available.

Enter h or 'h h' for help, or 'man perldebug' for more help.

main::(test-ping.cgi:8):                my $p = Net::Ping->new();
  DB<1>

  DB<1>   n
main::(test-ping.cgi:11):               my $domain = lc("play9112.com");
  DB<1> n
main::(test-ping.cgi:13):               print qq|Doing $domain \n|;
  DB<1> n
Doing play9112.com
main::(test-ping.cgi:27):               my $TIMEOUT_IN_SECONDS = 5;
  DB<1>
main::(test-ping.cgi:28):               eval {
  DB<1>
main::(test-ping.cgi:29):                   local $SIG{ALRM} = sub { die "alarm\n" };
  DB<1>
main::(test-ping.cgi:30):                   alarm($TIMEOUT_IN_SECONDS);
  DB<1>
main::(test-ping.cgi:32):                   $p->ping($domain,5);
  DB<1>

它在最后一部分之后挂起。所以它在 Net::Ping 中的东西就是这样做的:/

标签: perlping

解决方案


推荐阅读