?

Log in

No account? Create an account

Previous Entry | Next Entry

There are days

...When I get to remind myself why I still like being a geek. Wednesdays have become my default telecommuting day. I usually use the day to focus on things that need peace, quiet, and concentration to work on. After spending the first two hours of my day in ritual paper-pushing, email-answering and help desk ticket slinging, I tackled the job of figuring our where we were with our new commercial ISP and our reverse DNS (the entries they maintain that allow you to do a DNS query on one of our IP addresses and find out the name of the host that owns it).

I could have simply done fifty or so manual DNS lookups, but hey, it's Wednesday, I'm home and I have the time to write a little script that will be useful this time and the next time I have to do this. I decided to cook up a tool that will perform reverse lookups on an entire subnet, starting with the address entered on the command line. I raided a couple of other scripts (one for the basic structure and the other for the DNS lookup subroutine) and twenty minutes later had arpa_ranger.pl. In fine hacker tradition it does one thing, does it well with the minimum of fuss, and has a name with a double meaning.

The best thing about it are the second-order effects. After packing off my list of changes to the Fibertech NOC I used the script to confirm that they had made all the changes. Then curiosity moved me to scan the IP range my hosts at home are part of. I discovered that what one Cox person had told me many moons ago "you can't get custom reverse DNS from us" wasn't true. A handful of my neighbor IPs had names that were not wisp.yadda-yadda.ri.ri.cox.net. So I called them up, got a helpful geek grrl on the phone, and she submitted a request to get reverse DNS entries put in for my servers. I wonder who I might scan next. ;-)
#!/usr/bin/perl
#
# Script to perform DNS lookups on a range (class C block) of IP addresses
#

$version = "0.1";
$selfname = "ARPA Ranger";
$hostname = "";
$oct1 = 0; $oct2 = 0; $oct3 = 0; $oct4 = 0; 


($baseaddr,$verboselevel) = get_args();

message("\n$selfname v$version\n",1);

($oct1,$oct2,$oct3,$oct4) = split(/\./,$baseaddr);

for ($i = $oct4; $i < 255; $i++) {
  $hostname = resolve_host("$oct1.$oct2.$oct3.$i");
  print "$oct1.$oct2.$oct3.$i\t$hostname\n"
}

message("$selfname $version\n\n",1);

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


sub message {                     # Prints console messages based on verbose
                                  # level
  my $text = shift;
  my $priority = shift;
  
  if ($priority <= $verboselevel) {
    print "$text"
  }

} # End message()



sub get_args {                    # Pulls in command line arguemnt(s)

  my $i = 0;
  my $baseaddr = "";
  my $verboselevel = 1;
  my @octets = ();

  if (@ARGV < 1) {
    print "\nUsage: $selfname baseaddr=<a.b.c.d>\n\n";
    exit
  }

  while (defined($ARGV[$i])) {
    $arg = $ARGV[$i];
    @argels = split(/=/,$arg);
   
    if ($argels[0] eq "baseaddr") {
      $baseaddr = $argels[1]
    } elsif ($argels[0] eq "v") {
      $verboselevel = $argels[1]
    } else {
      print " x Bummer command line argument: $arg\n";
      exit
    }
    $i++    
  }

  @octets = split(/\./,$baseaddr);
  if (@octets < 4) {
    print " x Bummer base IP address: $baseaddr\n";
    exit
  }

  return $baseaddr,$verboselevel;



} # End get_args()



sub resolve_host {                # Subroutine to look up host name from IP
                                  # address; returns IP if name not resolvable
  my $destip = shift;

  use Socket;
  $iaddr = inet_aton("$destip");
  $hostname  = gethostbyaddr($iaddr, AF_INET);
  if (defined $hostname) {
    return $hostname
  } else {
    return $destip
  }

} # End resolve_host()

# End


Of course, in honor of International Talk Like A Pirate Day I should have called it ARRRRPA Ranger.

Tags:

Latest Month

January 2017
S M T W T F S
1234567
891011121314
15161718192021
22232425262728
293031    

Tags

Powered by LiveJournal.com
Designed by Lilia Ahner