?
Solved

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

Posted on 2012-03-14
8
Medium Priority
?
820 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 501 total points
ID: 37719279
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 501 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 498 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

Put Machine Learning to Work--Protect Your Clients

Machine learning means Smarter Cybersecurity™ Solutions.
As technology continues to advance, managing and analyzing massive data sets just can’t be accomplished by humans alone. It requires huge amounts of memory and storage, as well as the high-speed power of the cloud.

Question has a verified solution.

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

Quality of Service (QoS) options are nearly endless when it comes to networks today. This article is merely one example of how it can be handled in a hub-n-spoke design using a 3-tier configuration.
David Varnum recently wrote up his impressions of PRTG, based on a presentation by my colleague Christian at Tech Field Day at VMworld in Barcelona. Thanks David, for your detailed and honest evaluation!
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses

752 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