Skip to content

Instantly share code, notes, and snippets.

@rwp0
Last active January 23, 2023 10:26
Show Gist options
  • Save rwp0/5879395e81a254c7e452a3a174b611f2 to your computer and use it in GitHub Desktop.
Save rwp0/5879395e81a254c7e452a3a174b611f2 to your computer and use it in GitHub Desktop.
Perl NS Query with Net::DNS

Methods

  1. The new method of the Net::DNS::Resolver class returning a Net::DNS::Resolver object

  2. The query method of the Net::DNS::Resolver class returning a Net::DNS::Packet object

  3. The answer method of Net::DNS::Packet class returning a list of Net::DNS::RR objects

  4. The type method: of the Net::DNS::RR class returning a standard DNS record type string

  5. The nsdname method of the Net::DNS::RR::NS class returning a domain string


Get the packet with query, and then records from the packet with answer, check the record's type to get the appropriate value (eg. with nsdname).

my $file = path('dns.txt')->openw;
my $resolver = Net::DNS::Resolver->new;
for my $domain (@domains) {
my $packet = $resolver->query($domain, 'NS');
my @records = $packet->answer if defined $packet;
my @nsdnames;
for my $record (@records) {
if ($record->type eq 'NS') {
push @nsdnames, $record->nsdname;
}
}
printf "%s: %s\n", $domain, join(' ', @nsdnames);
$file->printf("%s: %s\n", $domain, join(' ', @nsdnames));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment