#!/usr/local/bin/perl # # Cross check the a host name using forward and reverse DNS # lookups. If the name passed in is a "cname" (aka alias), # then the second hostname returned will not match the first. # That is ok. The biggest thing to watch for is that the addresses # match. # # The gethostbyname or gethostbyaddr functions use whatever name service # the system you run it on is configured to use. For example, this may be # local hosts files, NIS, NIS+, or of course, DNS. # # Andy Welter # www.the-welters.com # May 16, 2001 # use Socket; # # Get the address for the name passed in. If someone # passed us an address, gethostbyname will return the address anyway. $name1=$ARGV[0]; $addr1=gethostbyname($name1); if ( $addr1 ) { ($a,$b,$c,$d)=unpack ('C4', $addr1); $addr1txt="$a.$b.$c.$d"; } else { die "Lookup failed for name1=$name1\n"; }; # # Do a reverse look up to get the name that goes with this address $name2=gethostbyaddr (inet_aton ($addr1txt), AF_INET); if ( $name2 ) { $addr2=gethostbyname($name2); } else { die "Lookup failed for addr1=$addr1txt\n"; }; # # Now do a forward lookup on the address to get the name. if ( $addr2 ) { ($a,$b,$c,$d)=unpack ('C4', $addr2); $addr2txt="$a.$b.$c.$d"; } else { die "Lookup failed for name2=$name2\n"; }; printf ("%-32s\t%s\n", "Hostname:","Address:"); printf ("%-32s\t%s\n%-32s\t%s\n", $name1, $addr1txt, $name2, $addr2txt);