• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 839
  • Last Modified:

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

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
hintco
Asked:
hintco
3 Solutions
 
WebF00LCommented:
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
 
hintcoAuthor Commented:
thank you but i need a php script not a service in website
0
 
WebF00LCommented:
Ahh Sorry mist that part.

Try this instead.

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


//WebFooL Untangle Evangelsit
0
Prepare for an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program curriculum features two internationally recognized certifications from the EC-Council at no additional time or cost.

 
hintcoAuthor Commented:
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
 
WebF00LCommented:
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
 
maeltarCommented:
0
 
Ray PaseurCommented:
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
 
zicevaCommented:
@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

Identify and Prevent Potential Cyber-threats

Become the white hat who helps safeguard our interconnected world. Transform your career future by earning your MS in Cybersecurity. WGU’s MSCSIA degree program was designed in collaboration with national intelligence organizations and IT industry leaders.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now