Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

IP list generation

by ryan (Pilgrim)
on Feb 16, 2001 at 22:35 UTC ( #58923=perlquestion: print w/ replies, xml ) Need Help??
ryan has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am wishing to create some more efficient code that will generate all IPs between two given ones.

I have done it, but it is a terribly huge conditional looping structure with one or two holes.

ie: 10.0.70.1 -> 10.1.50.20 will produce a list of the 60436 IPs that are in this range (to my calculations). It doesn't need to store it, just dump it to the screen or a file.

It needs to accept ANY IP range (as long as the second one is larger than the first).

Also, I need to know the number of IPs that will be generated through a relatively simple calculation rather than running through the loop slowly implementing a counter. I've got lost in the number of conditions that exist.

Do any such scripts exist to anyone's knowledge, or can someone whip up an efficiently coded version?

Thanks

Comment on IP list generation
Re: IP list generation
by merlyn (Sage) on Feb 16, 2001 at 22:40 UTC
    Perhaps you'd get somewhere by mapping them to a proper enumeration. Maybe this will help:
    my @range = map { unpack "N", pack "C4", split /\./ } qw(10.0.70.1 10. +1.50.20); print "@range\n"; # the numbers themselves print $range[1]-$range[0]+1, "\n"; # how many addresses for ($range[0]..$range[1]) { print join(".", unpack "C4", pack "N", $_), "\n"; }

    -- Randal L. Schwartz, Perl hacker

      Thank you so much, amazing :)

      Being a possessor of only basic Perl knowledge I am still wrapping my intellect around this code, it's getting clearer now.

      It's so depressing comparing the number of lines of code I used to do it :P

      Thanks again

      Great node. I had to modify the "for" loop for large IP addresses, e.g. starting with 128 and above.

      I now use something like this:
      my @range = map { unpack "N", pack "C4", split /\./ } ($ARGV[0], $ARGV +[1]); print "@range\n"; # the numbers themselves print $range[1]-$range[0]+1, "\n"; # how many addresses for (my $i = $range[0]; $i <= $range[1]; $i++) { print join(".", unpack "C4", pack "N", $i), "\n"; }
      I was getting this error:
      Range iterator outside integer range


      $ perldoc perldoc
Re: IP list generation
by CiceroLove (Monk) on Feb 16, 2001 at 22:45 UTC
    Without sitting down to write the code, I would approach this from a piece environment. I would just split(/./,$IP,4) and then calculate the differences between each octet of the two IPS and then multiply to find the number of IPs generated. Something like:
    $OctDifference1*($OctDifference2*($OctDifference3*$OctDifference4));
    Also I would increment the octets spearately and then cat them together.
    HTH
    CiceroLove

    UPDATE:Or you could be much more elegant and faster like Randall was whilst I was typing my crummy response. :O)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://58923]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2014-09-20 22:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (163 votes), past polls