Solved

Need assistance with PHP to compute start and end IP given base IP and subnet mask

Posted on 2013-11-16
8
362 Views
Last Modified: 2013-11-18
Hi Team,

   just need some quick assistance with IP maninpulation in PHP such that given a base IP and a mask, I can compute the start IP of the range and the end IP.

 Example:Given two fields in the form:
IP_BASE = 192.168.88.0
MASK = 24

I need it to come up with
$loweripaddress = 192.168.88.1
$upperipaddress = 192.168.88.255

I have a partial snippet below but Lower_IP and Upper_IP have to be filled in by the person doing the form.   I am looking for a way that php can be able to complete this automatically. I also need them converted to decimal which i was able to figure out already.

  function beforeSave(&$record){
    $lowerdecimal =ip2long($record->val('Lower_IP'));
    $upperdecimal =ip2long($record->val('Upper_IP'));
    $record -> setValue('Lower_Dec', $lowerdecimal); 
    $record -> setValue('Upper_Dec', $upperdecimal);    
}

Open in new window


Thanks very much.
0
Comment
Question by:rleyba828
  • 5
  • 2
8 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39653870
Starting with this...

 Example:Given two fields in the form:
IP_BASE = 192.168.88.0
MASK = 24

I need it to come up with
$loweripaddress = 192.168.88.1
$upperipaddress = 192.168.88.255


I am unable to discern the value of MASK = 24.

Why does that get included in the question?
0
 

Author Comment

by:rleyba828
ID: 39653919
Hi Ray, basically MASK (actually a subnet mask) is one of the two fields the user has to type in the form, the other being the IP_Address.

In Networking terms, if you have an IP of 10.0.0.0/24,  (/24 is the mask), what that really translates to is 10.0.0.1 up to 10.0.0.255, where these two resulting values translate to the lower and upper IP ranges covered by the IP subnet range.

Other examples:

172.16.3.0 /24    --> 172.16.3.1, 172.16.3.255
10.0.5.0/30     --> 10.0.5.1, 10.0.5.2
192.168.0.0/16   --> 192.168.0.1, 192.168.255.255


So if the user types in 10.10.0.0   and 16 as the fields and clicks submit,   the php should come up with

$loweripaddress = 10.10.0.1
$upperipaddress = 10.10.255.255

which i will then use for further processing.

Thanks
0
 

Author Comment

by:rleyba828
ID: 39653925
eventually my code should look something like this.....just don't know how to setup the function.

  function beforeSave(&$record){
    $loweripaddress = somephpfunctiontogetlowerip ($BASE_IP, $MASK);
    $upperipaddress =anotherphpfunctiontogetupperrip ($BASE_IP, $MASK);
    $lowerdecimal =ip2long($loweripaddress);
    $upperdecimal =ip2long($upperipaddress);
    $record -> setValue('Lower_Dec', $lowerdecimal); 
    $record -> setValue('Upper_Dec', $upperdecimal);    
}

Open in new window

0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 34

Accepted Solution

by:
gr8gonzo earned 350 total points
ID: 39653927
This is not my original function - most of it is from an old comment on the php.net site somewhere, but I'm not sure where.
 
<?php

print_r(iprange("192.168.1.0","24"));
// Array(
//     [0] => 192.168.1.0
//     [1] => 192.168.1.255
// )

function iprange($start,$cidrmask) {
$bits1=str_pad(decbin(ip2long($start)),32,"0",STR_PAD_LEFT);
$net=pow(2,(32-$cidrmask))-1;
$bits2=str_pad(decbin($net),32,"0",STR_PAD_LEFT);
for ($i=0;$i<32;$i++) {
if ($bits1[$i]==$bits2[$i]) $end.=$bits1[$i];
if ($bits1[$i]==1 and $bits2[$i]==0) $end.=$bits1[$i];
if ($bits1[$i]==0 and $bits2[$i]==1) $end.=$bits2[$i];
}
return array($start,long2ip(bindec($end)));
}
?>
0
 

Author Comment

by:rleyba828
ID: 39653933
Hi gr8gonzo,  yes, that is exactly what I need....let me adapt it to my code.....will let you know how it goes.
0
 

Author Comment

by:rleyba828
ID: 39654280
Hi Team,

  I've adapted the code above and my final (and working) code is now:

  function beforeSave(&$record){
    $auth =& Dataface_AuthenticationTool::getInstance();
    $user =& $auth->getLoggedInUser();
    $updatedate = date('Y-m-d H:i:s');
    $record->setValue('LAST_POLLED', $updatedate);
    $record->setValue('Record_Owner', $user->val('UserName'));
    $base_ip=($record->val('Base_IP'));
    $subnetmask=($record->val('Mask'));
    $lowerip=$base_ip;
    $bits1=str_pad(decbin(ip2long($base_ip)),32,"0",STR_PAD_LEFT);
    $net=pow(2,(32-$subnetmask))-1;
    $bits2=str_pad(decbin($net),32,"0",STR_PAD_LEFT);
    $end="";
    for ($i=0;$i<32;$i++) {
            if ($bits1[$i]==$bits2[$i]) $end.=$bits1[$i];
            if ($bits1[$i]==1 and $bits2[$i]==0) $end.=$bits1[$i];
            if ($bits1[$i]==0 and $bits2[$i]==1) $end.=$bits2[$i];
            }
    $upperip=long2ip(bindec($end));
    $record -> setValue('Lower_IP', $lowerip);
    $record -> setValue('Upper_IP', $upperip);
    $lowerdecimal =ip2long($record->val('Lower_IP'));
    $upperdecimal =ip2long($record->val('Upper_IP'));
    $record -> setValue('Lower_Dec', $lowerdecimal);
    $record -> setValue('Upper_Dec', $upperdecimal);
}

Open in new window


* The one small issue though is that the $lowerip should actually be $base_ip + 1.  I just don't know how to do this.
Example:  if $base_ip = 192.168.78.0, then $lowerip should be 192.168.78.1
If $base_ip = 10.2.3.0, $lowerip should be 10.2.3.1.

May I request final help in modifying the above code?

Thanks again.
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 150 total points
ID: 39654514
I can help with that part.  See http://www.laprbass.com/RAY_temp_rleyba828.php
Outputs: string(8) "10.2.3.1"

<?php // RAY_temp_rleyba828.php
error_reporting(E_ALL);
echo '<pre>';

// If $base_ip = 10.2.3.0, $lowerip should be 10.2.3.1.
// http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28296204.html#a39653933
// http://php.net/manual/en/function.ip2long.php

$base_ip = '10.2.3.0';
$int_ip  = ip2long($base_ip);
$int_ip++;
$new_ip  = long2ip($int_ip);

var_dump($new_ip);

Open in new window

Regards, ~Ray
0
 

Author Comment

by:rleyba828
ID: 39656161
Ah yes.....why didn't I think of that. Thanks ray and gr8gonzo for all the help.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
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…

785 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