[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2012-03-14
8
Medium Priority
?
824 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 is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Suggested Courses

649 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