Code examples from 'Internet Forensics'
Chapter 2 (Names and Numbers)

Example 2-1: scan_ip_range.pl
#!/usr/bin/perl -w
# Example 2-1: scan_ip_range.pl

# Excerpted from 'Internet Forensics' by Robert Jones
# Published 2005 by O'Reilly Media (ISBN 0-596-10006-X)

die "Usage: $0 <start IP addr> <end IP addr>\n" unless @ARGV == 2;

my $start_dec = dotted_quad_to_decimal($ARGV[0]);
my $end_dec   = dotted_quad_to_decimal($ARGV[1]);

for(my $i=$start_dec; $i<=$end_dec; $i++) {
    my $i_ip = decimal_to_dotted_quad($i);
    my $hostname = `dig +short -x $i_ip`;
    printf "%-15s %s", $i_ip, $hostname;
}


sub dotted_quad_to_decimal {
   my @fields = split /\./, shift;
   ($fields[0] * 16777216) + ($fields[1] * 65536) +
   ($fields[2] * 256)     +  $fields[3];
}


sub decimal_to_dotted_quad {
    my $decimal = shift;
    my $factor = 16777216;
    my @quad = ();
    for(my $i=0; $i<4; $i++) {
       $quad[$i] = int($decimal / $factor);
       $decimal -= $quad[$i] * $factor;
       $factor /= 256;
    }
    join ".", @quad;
}