DNS cheatsheet

From Lolly's Wiki
Revision as of 17:49, 25 November 2021 by Lollypop (talk | contribs) (Text replacement - "<source " to "<syntaxhighlight ")
Jump to navigationJump to search

dig

Compare several nameserver if SOA matches

<syntaxhighlight lang=bash> $ 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 </source>

dns2hosts

<syntaxhighlight lang=perl>

  1. !/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';

  1. cut_off_domain=0 : host.domain
  2. cut_off_domain=1 : short name only
  3. 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'}});
  1. Get the zone

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

  1. All A records

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

  1. Filter out net if $net_regex is set

@addresses = grep { $_->address =~ /$net_regex/ } @addresses if(defined($net_regex));

  1. 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";

} </source>