DNS解析器来自多个名称服务器

时间:2012-04-11 16:30:39

标签: perl dns

我将两个名称服务器传递给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";
    }
}

1 个答案:

答案 0 :(得分:3)

我假设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;
    }
  }

}