Solved

Function to list all ip addresses in a specific network address subnet

Posted on 2012-03-14
8
807 Views
Last Modified: 2012-04-03
Hello ,

i need a function in php to list or store all possible ip addresses in a specific subnet :

for example :

ListIPs(192.168.1.0/24)
will list :
192.168.1.0
192.168.1.1
192.168.1.2
.
.
.
192.168.1.254

and

List(192.168.1.0/16)
will list
192.168.1.0
192.168.1.1
192.168.1.2
.
.
.
.
192.168.2.0
192.168.2.1
192.168.2.2
.
.
.
192.168.254.254

etc  

i search in the internet but didn't found thing like this

thank you
0
Comment
Question by:hintco
8 Comments
 
LVL 3

Expert Comment

by:WebF00L
ID: 37718828
Test this site http://www.ipaddresslocation.org/ip-address-ranges.php

Go down to the Yellow box and insert the Cdir and hit "Generate Ip Address Range"


//WebFooL Untangle Evangelist
0
 
LVL 1

Author Comment

by:hintco
ID: 37718842
thank you but i need a php script not a service in website
0
 
LVL 3

Expert Comment

by:WebF00L
ID: 37718860
Ahh Sorry mist that part.

Try this instead.

http://www.xoxideforums.com/web-design/68233-php-ip-generator-range.html


//WebFooL Untangle Evangelsit
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 1

Author Comment

by:hintco
ID: 37718916
its not working for subnets all subnets

for example the subnet ip : 172.16.0.0 / 17  
ip range is :

172.16.0.1 - 172.16.127.254

i need a function auto calculate the ranges and store them in array for example or list them !
0
 
LVL 3

Expert Comment

by:WebF00L
ID: 37719270
This should work

<?php
$Startip = "172.16.0.1";
$Endip = "172.16.127.254";

$arry1 = explode(".",$Startip);
$arry2 = explode(".",$Endip);
$a1 = $arry1[0]; $b1 = $arry1[1];      $c1 = $arry1[2]; $d1 = $arry1[3];
$a2 = $arry2[0]; $b2 = $arry2[1];      $c2 = $arry2[2]; $d2 = $arry2[3];
while( $d2 >= $d1 || $c2 > $c1 || $b2 > $b1 || $a2 > $a1){
if($d1 > 255){
$d1 = 1;
$c1 ++;
}
if($c1 > 255){
$c1 = 1;
$b1 ++;
}      
if($b1 > 255){
$b1 = 1;
$a1 ++;
}      
echo "$a1.$b1.$c1.$d1 <br>";
$d1 ++;
}
?>
0
 
LVL 11

Accepted Solution

by:
maeltar earned 167 total points
ID: 37719279
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 167 total points
ID: 37719640
See if this makes sense for your needs.  This article might also be helpful.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_3437-IP-Address-to-Country-in-PHP.html
<?php // RAY_temp_hintco.php
error_reporting(E_ALL);

// FUNCTION TO GENERATE ALL SUBNET ADDRESS IN A RANGE
function subnets($loip, $hiip)
{
    // CONVERT TO BINARY - SEE http://php.net/manual/en/function.ip2long.php
    $lo = ip2long($loip);
    if ($lo === FALSE) return FALSE;
    $hi = ip2long($hiip);
    if ($hi === FALSE) return FALSE;

    // BEWARE: RANGE OF IP ADDRESSES MAY CONTAIN NEGATIVE NUMBERS
    $nns = range($lo, $hi);

    // GENERATE THE DOTTED ADDRESS NOTATION
    foreach ($nns as $nn)
    {
        $ips[] = long2ip($nn);
    }
    return $ips;
}

// TEST THE FUNCTION
print_r(subnets('127.0.0.0', '127.0.1.0'));

Open in new window

0
 
LVL 7

Assisted Solution

by:ziceva
ziceva earned 166 total points
ID: 37721554
@Ray your function takes low & hi limits of the subnet ... the poster asked for a function to return all ip's in a subnet defined by netaddress/netmask

This function does exactly that (actually it also works with ip/netmask ...):
<?
function subnetIPS($subnet){
  $ips=array();
  //format: octet.octet.octet.octet/mask
  if(preg_match('/([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\/([0-9]{1,2})/',$subnet,$matches)) 
  { 
   if($matches[1]>255 || $matches[2]>255 || $matches[3]>255 || $matches[4]>255 || $matches[5]>32){
       return -1; //invalid format
   }
   $ip=$matches[1].".".$matches[2].".".$matches[3].".".$matches[4];
   $ip=ip2long($ip);
   
   $mask=0;
   for($i=1;$i<=$matches[5];$i++){
    $mask+=pow(2,32-$i);
   }
   
   $start=$ip&$mask;
   $stop=ip2long(long2ip($ip|~$mask)); 
  
  for($i=$start;$i<=$stop;$i++){
   $ips[]=long2ip($i);  
  }
  return $ips;
  }
  else
  {
   return -1; //invalid format
  }

}
?>

Open in new window

Tests:

print_r(subnetIPS('192.168.1.0/24'));

Open in new window


will display:

Array
(
    [0] => 192.168.1.0
    [1] => 192.168.1.1
    [2] => 192.168.1.2
    [3] => 192.168.1.3
...
    [255] => 192.168.1.255
)

Open in new window


print_r(subnetIPS('192.168.1.11/24'));

Open in new window


will display (same thing):

Array
(
    [0] => 192.168.1.0
    [1] => 192.168.1.1
    [2] => 192.168.1.2
    [3] => 192.168.1.3
...
    [255] => 192.168.1.255
)

Open in new window


print_r(subnetIPS('192.168.1.11/23'));

Open in new window


will display:

Array
(
    [0] => 192.168.0.0
    [1] => 192.168.0.1
    [2] => 192.168.0.2
    [3] => 192.168.0.3
...
    [255] => 192.168.0.255
    [256] => 192.168.1.0
    [257] => 192.168.1.1
...
    [511] => 192.168.1.255
)

Open in new window

0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PRTG Network Monitor lets you monitor your bandwidth usage, so you know who is using up your bandwidth, and what they're using it for.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question