Solved

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

Posted on 2012-03-14
8
810 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

856 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