1

我将两个名称服务器传递给Net::DNS::Resolver构造函数,但我只得到一个结果。

我应该如何更改代码以接收来自所有名称服务器的结果?

sub resolve_dns()
{
    my $dns = $_[0];
    my $res   = Net::DNS::Resolver->new(

                nameservers => [qw(24.116.197.232 114.130.11.67 )],
                recurse     => 0,
                debug       => 1,
                tcp_timeout => 3
                );
    my $query = $res->search($dns);

    if ($query) {
        foreach my $rr ($query->answer) {
            next unless $rr->type eq "A";
            print $rr->address, "\n";
        }
    } else {
        warn "query failed: ", $res->errorstring, "\n";
    }
}
4

1 回答 1

3

我认为第一个 DNS 服务器之后的 DNS 服务器是出于后备目的,并且只会返回一个回复。

最好的方法似乎是操纵Net::DNS::Resolver服务器列表并明确地向每个人发出请求。

此示例代码演示了原理

sub resolve_dns {

  my $address = shift;

  my $res = Net::DNS::Resolver->new
    recurse     => 0,
    debug       => 1,
    tcp_timeout => 3,
  );

  for my $ns (qw( 24.116.197.232  114.130.11.67 )) {

    $res->nameservers($ns);

    my $reply = $res->send($address);

    if ($reply) {
      my @type_a = grep $_->type eq 'A', $reply->answer;
      print $_->address, "\n" for @type_a;
    }
    else {
      warn sprintf "Query to %s failed: %s\n", $ns, $res->errorstring;
    }
  }

}
于 2012-04-11T17:29:48.833 回答