Category: Networking
Author/Contact Info ybiC
Description: Confirm that dynamic DNS updates are propagated to other servers, plus report a bit more DNS info. Tested with Perl 5.00404 on Linux. Thanks to lhoward for helping an acolyte toward his first Code Catacombs post. Anything insecure or just plain dumb (grin) in this script is due entirely to my own limitations.
#!/usr/bin/perl -wT

# ckdns.perl
# confirm dynamic DNS update
# ToDo:
#     reverse lookup (just for kicks)
#     CGI form for non-default lookup


use Net::DNS;
use Time::localtime;
use strict;
use vars qw( $default $name  $time  $pid  @ppp
             $pppip @mx  $line  $nameserver @ARGV
             $nameresolution  $res $query
             $rr  $queryNS $resNS  $resMX
           );


my $default = 'myddnshost.dom';
my $ns1  = 'dns.some.dom';
my $ns2  = 'dns.someother.dom';
my $ns3  = 'dns.yetanother.dom';
my $ns4  = 'dns.andanother.dom';
    # using nameservers' address instead of name
    # might be a bit better, but hostname
    # provides more informative output.


select ((select(STDOUT), $|=1) [0]);
    # hot STDOUT fixes sequence problem - TPJ #11
$ENV { 'PATH' } = '';
    # unTaint backticks by clearing path


    ## REPORT HEADER ##
my $tm   = localtime;
printf "\n%02d-%02d-%04d     ", $tm->mon+1, $tm->mday, $tm->year+1900;
printf "%02d:%02d:%02d\n", $tm->hour, $tm->min, $tm->sec;
print "$^X $]   ", "\Net::DNS ", Net::DNS->version, "\n";


    ## PPP0 ADDRESS ##
my $pid = `/bin/cat /var/run/ppp0.pid`
     or die "ppp0 down, abort";
     # abort script if ppp0 down
@ppp = `/sbin/ifconfig ppp0`;
foreach $line (@ppp) {
    if($line =~ /inet addr/) {
    $line =~ s/\s+inet addr:(\S+)\s+.*/$1/;
    $pppip = $line;
    }
}
print "\n\nlocal ppp0\n";
print "================\n";
print "$pppip\n";
# print "$name : ", "$pppip\n";


    ## FORWARD LOOKUPS ##
@ARGV = ("$default" ) unless @ARGV;
my $name = "@ARGV";
print "\n$name forward lookup\n";
print "=============================\n";
my @nameservers=( $ns1, $ns2, $ns3, $ns4 );
my $nameserver;
    foreach $nameserver(@nameservers) {
        print "Nameserver : $nameserver\n";
        my $res = new Net::DNS::Resolver;
        $res->nameservers($nameserver);
        my $query = $res->search("$name");
        if ($query) {
            my $rr;
            foreach $rr ($query->answer) {
                next unless $rr->type eq "A";
                print "$name : ", $rr->address, "\n\n";
            }
        } else {
        print "query failed: ", $res->errorstring, "\n\n";
        }
    }


    ## AUTHORITATIVE NAMESERVERS ##
print "\n$name authoritative nameservers\n";
print   "========================================\n";
my $resNS = new Net::DNS::Resolver;
my $queryNS = $resNS->query("$name", "NS");
if ($queryNS) {
    foreach $rr ($queryNS->answer) {
        next unless $rr->type eq "NS";
        printf "%s\n", $rr->nsdname;
    }
}
else {
    print "\Nameserver query failed: ", $resNS->errorstring, "\n";
}


    ## MX RECORDS ##
print "\n\n$name MX records\n";
print   "==============================\n";
my $resMX = new Net::DNS::Resolver;
@mx = mx($resMX, $name);
if (@mx) {
    foreach $rr (@mx) {
        print $resMX->answerfrom," says : ", $rr->preference, " ", $rr
+->exchange, "\n";
    }
}
else {
    print "MX record query failed: ", $resMX->errorstring, "\n";
}


# END
  • Comment on (code) ixnay to nslookupay (first Catacombs submission, ugly nasty bad code)
  • Download Code