Adamba4
Adamba4

Reputation: 181

Perl Get Nameservers IPs from root server

I'm trying to write a Perl script that should get the IPs of the nameservers of specific domains, as written in the registrar, as if I wrote for example:

dig -t ns example.com @a.gtld-servers.com

And took the additional section.

I tried using the Net::DNS::Resolver::Recursive, but all i'm getting is the same IP address for both NS Servers. (At my company we populate the DNS record as each NS is a standalone server, that's why i'm getting the same address).

here's what I wrote:

my $resolver = Net::DNS::Resolver::Recurse->new();

my @root_ns = map $_ . '.root-servers.net', 'a'..'m';

$resolver->hints( @root_ns );

my $ns_query = $resolver->query_dorecursion($domain, "NS");

foreach my $ns_rr ( $ns_query->additional ) {
    print $ns_rr->name . " " . $ns_rr->rdatastr . "\n";
}

Is there a way, except parsing the dig output, to get the nameservers IPs?

Upvotes: 2

Views: 573

Answers (1)

sanooj
sanooj

Reputation: 493

You can use recursion_callback instead. This will return all the nameservers.

    use Net::DNS::Resolver::Recurse;

    my $res = Net::DNS::Resolver::Recurse->new;
    our %NS;

    $res->hints();
    $res->recursion_callback(sub {
         my $packet = shift;
         for  ($packet->additional){
              next if($_->string =~ /gtld/);
              @temp=split /\s+/,$_->string;
              $NS{$temp[0]}=$temp[-1];

       }
    });

    $res->query_dorecursion('google.com', 'NS');
    map {print "$_\t$NS{$_}\n"} keys %NS;

Upvotes: 1

Related Questions