This snippet is designed to take a textfile of domain names, one per line as the first command line arg and a nameserver as the second - it then performs a mx lookup on the domain, it then does a lookup on the mx domain returned and does a reverse lookup of the ip address.

Its designed to help administrators maintain nameservers and or mailservers that host lots of domains, identifying dead domains and or domains which your servers may think they are authoratitive for which they are not.

Uses Net::DNS
#!/usr/bin/perl use strict; use Net::DNS; my $domains = $ARGV[0]; my $nameserver = $ARGV[1]; open(DOMAINS, "< $domains") or die "can't open $domains: $ +!"; while(<DOMAINS>){ my $domain = chomp($_); my $domain = $_; print "-------------------------------------\n"; print "Checking domain: $domain\n"; my $res = new Net::DNS::Resolver; $res->nameservers("$nameserver"); my @mx = mx($res, $domain); if (@mx) { foreach my $rr (@mx) { my $ip = &lookup($rr->exchange,); print $rr->preference, " ", $rr->exchange, " - +> $ip\n"; } } else { print "can't find MX records for $domain: ", $res->err +orstring, "\n"; } } close(DOMAINS) or die "can't close $domains: $!"; sub lookup { my $domain = $_[0]; my $res = new Net::DNS::Resolver; my $query = $res->search($domain); if ($query) { foreach my $rr ($query->answer) { next unless $rr->type eq "A"; my $rev = &rev_lookup($rr->address,); my $address = $rr->address,; my $lookedup = "$address -> $rev"; return $lookedup; } } else { return $res->errorstring,; } } sub rev_lookup { my $ip = $_[0]; my $res = new Net::DNS::Resolver; my $query = $res->search($ip); if ($query) { foreach my $rr ($query->answer) { next unless $rr->type eq "PTR"; return $rr->rdatastr,; } } else { return $res->errorstring,; } }

Replies are listed 'Best First'.
Re: DNS MX checking
by blakem (Monsignor) on Aug 14, 2002 at 08:46 UTC
    A very helpful site for DNS management is DNS Bajaj. You type in your domain name and it builds a dynamic diagram (takes about 20 seconds) based on DNS lookups. It even flags certain common errors (i.e. out of sync backup name server) Very useful for determining how the rest of the world views your DNS.

    -Blake

      Thanks for the link Blake. DNS Bajaj is pretty cool. Unfortunately the source seems to be missing from the dl dir, I'd like to take a look at it and maybe extend it - some mx and other lookups would round it out a bit.

      I'm working on a gui frontend for this and some other snippets of similar code in tk. Perhaps called hostfe to do mx, ns lookups on multiple domains and reverse for a whole class C, and perhaps whois functionality - although whois can be pretty variable. *nix doesnt seem to have a Sam Spade equivalent. Id also like the mx checker to check for telnet/netcat to port 25 style banner checking and perhaps pop/110 banners

      I think theres lots of room for a comprehensive reconisance tool for linux - think a tool that does all the functions discussed in Hacking Linux Exposed chapter 3 - mapping the network, also providing a command line interface to netcraft ( http://uptime.netcraft.com ) would be worth while.

      Does anyone know a GPL project in perl doing stuff like this? If so i would like to get involved - otherwise I think i'll work on it myself (slowly :)