Solved

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

Posted on 2013-11-16
8
366 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 110

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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 35

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 110

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

679 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