DNS cheatsheet: Difference between revisions

From Lolly's Wiki
Jump to navigationJump to search
m (Text replacement - "<source " to "<syntaxhighlight ")
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
[[Category:DNS]]
=dig=
=dig=
==Compare several nameserver if SOA matches==
==Compare several nameserver if SOA matches==
Line 8: Line 9:
Nameserver: ns1.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
Nameserver: ns3.denic.de. => SOA: ns1.denic.de. its.denic.de. 1468491003 10800 1800 3600000 1800
</source>
</syntaxhighlight>


==dns2hosts==
==dns2hosts==
Line 59: Line 60:
   print "\n";
   print "\n";
}
}
</source>
</syntaxhighlight>

Latest revision as of 09:57, 26 November 2021

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";
}