In article <[EMAIL PROTECTED]>, John W. Krahn wrote:

> Brian Burns wrote:
>> 
>> given a start and finish address like this
>> 
>> $start_address  = "10.11.1.14:;
>> $finish_address = "12.13.2.3";
>> 
>> I need to output generarate a a sequential list of addresses like the
>> following:
>> 
>> 10.11.1.14
>> 10.11.1.15
>> 10.11.1.16
>> ...
>> 12.13.1.252
>> 12.13.1.253
>> 12.13.1.254
>> 12.13.2.1
>> 13.13.2.2
>> 12.13.2.3
[...]

> 
> You need to convert the IP address to a 32 bit integer and back again.
> 
> use Socket;
> 
> my $start_address  = unpack 'N', inet_aton( '10.11.1.14' );
> my $finish_address = unpack 'N', inet_aton( '12.13.2.3' );
> 
> for my $address ( $start_address .. $finish_address ) {
>     print inet_ntoa( pack 'N', $address );
>     }

Aha!

(But this includes numbers such as 12.13.1.255 and 12.13.2.0 which are not
in the sample pattern.)

Here's my less clever (and slower?) method...

my @start_addr = split /\./, $start_address;
my @finish_addr = split /\./, $finish_address;
my @result = @start_addr;

print join ".", @result, "\n";     # first number

while (1) {
  $result[3]++;                    # increment "ones" by 1
  my $value = join ".", @result;   # output new number
  print $value, "\n" ;
  last if $value eq $finish_address;

  if ($result[3] == 254) {         # need to do carryover
    my $x;
    for ($x = 3; $x > -1; $x--) { 
      if ($result[$x] == 254) {
        $result[$x -1]++;          # carry over to next column
        $result[$x] = 0;           # reset column
      }
    }
  }
}

print "\n";

__END__
(Comments, suggestions always welcome.)

-Kevin
-- 
Kevin Pfeiffer


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to