0

I have a list of IP address ranges and I want to create a sequential list of all the IP addresses that the list of ranges contains.

The list is similar to below (but much longer)...

From IP To IP   Total IPs   Assign Date Owner
1.0.32.0    1.0.63.255  8192    12/4/2011   
1.1.16.0    1.1.31.255  4096    12/4/2011   
1.1.32.0    1.1.63.255  8192    12/4/2011   
1.2.16.0    1.2.31.255  4096    12/4/2011   
1.2.32.0    1.2.63.255  8192    12/4/2011   
1.2.64.0    1.2.127.255 16384   12/4/2011   
1.3.0.0 1.3.255.255 65536   12/4/2011   

Currently, I'm repeating the same for () loop.

<?php
for ($a = 0; $a <= 255; $a++) {
    echo "1.0.32.$a<br>";
}
for ($a = 0; $a <= 255; $a++) {
    echo "1.0.33.$a<br>";
}
for ($a = 0; $a <= 255; $a++) {
    echo "1.0.34.$a<br>";
}
for ($a = 0; $a <= 255; $a++) {
    echo "1.0.35.$a<br>";
}
for ($a = 0; $a <= 255; $a++) {
    echo "1.0.36.$a<br>";
}
for ($a = 0; $a <= 255; $a++) {
    echo "1.0.37.$a<br>";
}
?>

It spits out a list of IPs, as desired.

1.0.32.0
1.0.32.1
1.0.32.2
1.0.32.3
1.0.32.4
1.0.32.5
1.0.32.6
1.0.32.7
1.0.32.8

This will take me forever, doing it this way. Hehehe...

I'm sure there is some fancy, mathematical, function that would clean that up. I haven't figured it out, though.

doubleJ
  • 1,190
  • 1
  • 16
  • 29

1 Answers1

8

PHP Generate IP Ranges

function ip_range($start, $end) {
$start = ip2long($start);
$end = ip2long($end);
return array_map('long2ip', range($start, $end) );
}
$range_one = "1.1.1.1";
$range_two = "1.1.99.255";
print_r(ip_range($range_one, $range_two) );

Edit: I created 65794 IP addresses in 1 second.

Community
  • 1
  • 1
cagri
  • 807
  • 8
  • 17
  • I had no idea about `ip2long`. I'll look into it, more. – doubleJ Aug 11 '15 at 20:07
  • I created a variant of this code (http://pastebin.com/EWahenDy) and you can see the results (http://flcbranson.org/chineseips.php). But, it isn't displaying all the results. The page loads a lot of blank space, at the bottom, and it doesn't finish displaying the array. Do you think I'm hitting a PHP wall or a server wall? – doubleJ Aug 11 '15 at 20:56
  • Apparently, it was an Internet Explorer wall. Firefox displays it, just fine. – doubleJ Aug 11 '15 at 20:57
  • Unfortunately, Firefox peters out not much later than IE. I can't seem to display my entire results in any browser. – doubleJ Aug 11 '15 at 21:03
  • It is, consistently, breaking when I add one line of IP range. The preceding ranges contain just under 500000 records. Adding one more range takes it over 500000. I wonder if that is a PHP array limit. My whole list is over 300,000,000 addresses. – doubleJ Aug 11 '15 at 21:09
  • You are working with very very big data. You should part this. – cagri Aug 11 '15 at 21:12
  • Yeah, I found that I was running out of memory (http://stackoverflow.com/questions/5925885/array-size-limit-in-php). `ini_set('memory_limit', '1024M');` allowed more of the script to run. Unfortunately, the whole script would take over an hour to process. I don't think it would work, regardless of how high I set the memory. – doubleJ Aug 11 '15 at 21:39