#!/usr/bin/perl ################################################################# # Anastasios Monachos (secuid0) - [anastasiosm(at)gmail(dot)com] # # Usage: perl dnsforver.pl start_ip_address end_ip_address # Example: perl dnsforver.pl 172.28.108.1 172.28.108.179 # # Purpose: Check for consistency your reverse-forward DNS entries # # Version: 0.1 # # Demo: #$perl dnsforver.pl 172.28.108.30 172.28.108.42 # #IP is 172.28.108.30 Reverse lookup failed to find name Hostname is 172.28.108.30 and maps to IPs: IP: 172.28.108.30 --No Reverse DNS record exists-- --DNS record matches-- #IP is 172.28.108.31 Hostname is lthp24xk.corp.dom.com Lookup failed to find address and maps to IPs: --No Forward DNS record exists-- #IP is 172.28.108.32 Reverse lookup failed to find name Hostname is 172.28.108.32 and maps to IPs: IP: 172.28.108.32 --No Reverse DNS record exists-- --DNS record matches-- #IP is 172.28.108.33 Hostname is lthp1p8j.corp.dom.com Lookup failed to find address and maps to IPs: --No Forward DNS record exists-- #IP is 172.28.108.34 Hostname is lthp1plb.corp.dom.com Lookup failed to find address and maps to IPs: --No Forward DNS record exists-- #IP is 172.28.108.35 Hostname is lthp206q.corp.dom.com and maps to IPs: IP: 172.27.106.48 --Probable MISMATCH !!!-- #IP is 172.28.108.36 Reverse lookup failed to find name Hostname is 172.28.108.36 and maps to IPs: IP: 172.28.108.36 --No Reverse DNS record exists-- --DNS record matches-- #IP is 172.28.108.37 Reverse lookup failed to find name Hostname is 172.28.108.37 and maps to IPs: IP: 172.28.108.37 --No Reverse DNS record exists-- --DNS record matches-- #IP is 172.28.108.38 Reverse lookup failed to find name Hostname is 172.28.108.38 and maps to IPs: IP: 172.28.108.38 --No Reverse DNS record exists-- --DNS record matches-- #IP is 172.28.108.39 Reverse lookup failed to find name Hostname is 172.28.108.39 and maps to IPs: IP: 172.28.108.39 --No Reverse DNS record exists-- --DNS record matches-- #IP is 172.28.108.40 Hostname is srv01.corp.dom.com Lookup failed to find address and maps to IPs: --No Forward DNS record exists-- #IP is 172.28.108.41 Hostname is dthp2gwh.corp.dom.com and maps to IPs: IP: 172.28.108.41 --DNS record matches-- #IP is 172.28.108.42 Hostname is lthp1r87.corp.dom.com and maps to IPs: IP: 172.28.110.147 --Probable MISMATCH !!!-- ################################################################# use strict; use warnings; use Socket qw(AF_INET); sub usage() { print STDERR << "EOF"; ******************************************************************************************** Usage: perl $0 start_ip_address end_ip_address ******************************************************************************************** EOF exit; } if ($#ARGV != 1 ) { usage(); } my $start = ip_from_dotted($ARGV[0]); my $end = ip_from_dotted($ARGV[1]); my $ip1; my $ip2; for (my $ip=$start; $ip<=$end; $ip++) { dnslookup_info(ip_to_dotted($ip)); } sub dnslookup_info { foreach (shift) { my $host1='0'; my ($ip, $host, $aliases, $addrtype, $length, @addrs); $ip = $_; if ( /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/ ) { print "IP is $ip\t"; $ip1=$ip; ($host, $aliases, $addrtype, $length, @addrs) = gethostbyaddr( pack( 'C4', $1, $2, $3, $4 ), AF_INET ); unless($host) { print "\tReverse lookup failed to find name\t"; $host1='-1'; } } $host = $ip unless $host; print "\tHostname is ".$host; ($host, $aliases, $addrtype, $length, @addrs) = gethostbyname( $host ); unless(@addrs) { print "\tLookup failed to find address\t"; $ip1='-2'; } print " and maps to IPs: "; foreach (@addrs) { print "IP: ".join( '.', unpack( 'C4', $_ ) )."\t"; $ip2= join( '.', unpack( 'C4', $_ ) ); } if ($host1 eq '-1') {print "--No Reverse DNS record exists--";} if ($ip1 eq '-2') {print " --No Forward DNS record exists--";} elsif ($ip1 eq $ip2) {print " --DNS record matches--";} elsif ($ip1 ne $ip2) {print " --Probable MISMATCH !!!--";} } print "\n"; } sub ip_from_dotted { unpack 'N', pack 'C4', split /\./, $_[0] } sub ip_to_dotted { join '.', unpack 'C4', pack 'N', $_[0] }