Friday, April 25, 2008

Class C DNS resolving tool

This could be written better, but it was one of those things where I needed it that 1 time so I wrote it on a whim. Feel free to let me know what you would change.. I'd be interested in hearing it.



#!/usr/bin/perl
####
# this is an IP block resolving utility.
#
# only does a class C space at a time, but that's all I needed when I wrote it. SO.
#
# the output is suitable for a simple copy/paste into /etc/hosts.
#
# - 10/17/2007 - nwo


use Socket;

if(@ARGV[0] eq "") {
print "Enter a starting address and an ending octet within that block.\n";
print "For example: ./dnswalk.pl 10.0.0.1 255\n";
print "If the ending octet is left off, 255 will be assumed.\n";
exit(0);
}


@net = (split /\./, $ARGV[0])[0,1,2];
$begin = (split /\./, $ARGV[0])[3];
$end = $ARGV[1];
if($end eq "") { $end = "255"; }


while($begin <= $end) {
$ip = "$net[0].$net[1].$net[2].$begin";
$iaddr = inet_aton("$ip");
$name = gethostbyaddr($iaddr, AF_INET);
if($name eq "") {
$begin++;
next;
}

print "$ip\t$name\n";
$begin++;
}

No comments: