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

In reply to DNS MX checking by htaccess

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.