DNS cheatsheet

From Lolly's Wiki
Jump to navigationJump to search

dig

Compare several nameserver if SOA matches

$ domain=denic.de
$ printf "Domain: %s\n" ${domain} ; for ns in $(dig +short ${domain} ns) ; do printf "Nameserver: %s => SOA: %s\n" ${ns} "$(dig +short ${domain} soa @${ns})" ; done
Domain: denic.de
Nameserver: ns2.denic.de. => SOA: ns1.denic.de. its.denic.de. 1468491003 10800 1800 3600000 1800
Nameserver: ns1.denic.de. => SOA: ns1.denic.de. its.denic.de. 1468491003 10800 1800 3600000 1800
Nameserver: ns3.denic.de. => SOA: ns1.denic.de. its.denic.de. 1468491003 10800 1800 3600000 1800

dns2hosts

#!/usr/bin/perl
use Net::DNS;
use Net::DNS qw(rrsort);

my @nameservers = ("auth-dns-1.domain.de","auth-dns-2.domain.de");
my $net_regex = '10\.11\.';
my $domain = 'domain.de';

# cut_off_domain=0 : host.domain
# cut_off_domain=1 : short name only
# cut_off_domain=2 : short name and with domain
my $cut_off_domain=1;

my $res  = Net::DNS::Resolver->new;
$res->nameservers(@nameservers);


Net::DNS::RR::A->set_rrsort_func ('asorted',
             sub {($a,$b)=($Net::DNS::a,$Net::DNS::b);
                  $a->{'address'} cmp $b->{'address'}});

# Get the zone
my @zone = $res->axfr($domain);

# All A records
my @addresses = grep { $_->type eq "A" } @zone;

# Filter out net if $net_regex is set
@addresses = grep { $_->address =~ /$net_regex/ } @addresses if(defined($net_regex));

# All CNAME records
my @cnames = grep { $_->type eq "CNAME" } @zone;

my $host;
foreach $rr (rrsort("A","asorted", @addresses)) {
  $host=$rr->name;
  $host=(split /\./,$host)[0] if ($cut_off_domain eq 1);
  $host=(split /\./,$rr->name)[0]." ".$rr->name if ($cut_off_domain eq 2);
  print $rr->address."\t".$host;
  foreach $cname (grep { $_->cname eq $rr->name } @cnames) {
    $host=$cname->name;
    $host=(split /\./,$host)[0] if ($cut_off_domain eq 1);
    $host=(split /\./,$cname->name)[0]." ".$cname->name if ($cut_off_domain eq 2);
    print " ".$host;
  }
  print "\n";
}