Perl Script To Demonstrate DNS Lookups In Linux

Posted: November 30, 2008 in LINUX
Tags: ,

This is what you can expect to see from this script (approximately):

host # ./double.pl www.google.com

#######################################################

#!/usr/bin/perl
#
# double.pl – Double Check That Hostnames Match The IP’s They’re Advertising
#
# 2008 – Mike Golvach – eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#
use Socket;
if ( $#ARGV != 0 )      {
           print “Usage: $0 hostname\n”;    
            exit(1);
}
$entry = $ARGV[0];
$hostname1 = $entry;
$ip1 = gethostbyname($hostname1) || die “error – gethostbyname: $!\n\n”;
$hostip1 = inet_ntoa($ip1) || die “error – inet_ntoa: $!\n\n”;
print “\nHostname: $hostname1 = IP: $hostip1\n\n”;
$hostname2 = gethostbyaddr(inet_aton($hostip1),AF_INET) || die “error – inet_aton: $!\n\n”;
print “IP: $hostip1 = Hostname: $hostname2\n\n”;
if ( $hostname1 eq $hostname2 ) {
  print “$hostname1 and $hostname2 Match!\n”;      
   print “Good Deal!\n\n”;  
       exit(0);
}       else    {
      print “$hostname1 and $hostname2 Do Not Match\n”;
          print “Checking Reverse…\n\n”;
        $ip2 = gethostbyname($hostname2) || die “error – gethostbyname: $!\n\n”;
         $hostip2 = inet_ntoa($ip2) || die “error – inet_ntoa: $!\n\n”;
          if ( $hostip1 eq $hostip2 )     {             
   print “$hostip1 and $hostip2 match\n”;
   print “Everything is probably ok!\n\n”;
   exit(0);
        }       else    {        
        print “$hostip1 and $hostip2 don’t match\n”; 
         print “This DNS may be bogus or setup incorrectly!\n\n”;  
              exit(0);
        }
}

###########################################################################

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s