Tuesday, December 30, 2008

Reverse DNS Lookup

I am writing a web service to perform a reverse lookup on an IP address.

During the course of my search for existing packages to build on I discovered a nice Perl script that can perform either a reverse DNS lookup on an IP address or dump the IP addresses associated with a particular domain name.

If you have a need for such functionality have a look at Peter's script!

I am including it here for your convenience with a link to the author's website.

http://www.peter.com.au/perl/index.html#getdnsinfo

This script displays the IP associated with a DNS name (or vice versa).

#!/usr/bin/perl

use strict;
use warnings;

use Socket qw(AF_INET);

usage() if $#ARGV == -1;
display_info( @ARGV );

sub display_info {
foreach (shift) {
my ($ip, $host, $aliases, $addrtype, $length, @addrs);
$ip = $_;
if ( /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/ ) {
print "IP is $ip\n";
($host, $aliases, $addrtype, $length, @addrs) =
gethostbyaddr( pack( 'C4', $1, $2, $3, $4 ), AF_INET );
die "Reverse lookup failed to find name for $ip\n" unless $host;
}
$host = $ip unless $host;
print "Hostname is $host\n";
($host, $aliases, $addrtype, $length, @addrs) = gethostbyname( $host );
die "Lookup failed to find address for $host\n" unless @addrs;
print "Maps to these IPs:\n";
foreach (@addrs) {
print "IP: ".join( '.', unpack( 'C4', $_ ) )."\n";
}
}
}

sub usage {
print STDERR <...
Example `getdnsinfo.pl www.interarchy.com'
EOM
exit( 0 );

}

No comments: