PHP calculates all the IP in the network segment, assigns sub-network segments

Source: Internet
Author: User
Tags pow

Due to recent business needs, a function to get all the IP in the network segment is written, as well as a function to allocate the available subnet segments

/** * Calculate all IP * @param string $segment network segment ' 139.217.0.1/24 ' * @return array IP list [' 139.217.0.1 ', ' 139.217.0.2 ' ...] According to the network segment. */functionGetipbysegment ($segment){    $segmentInfo=Explode("/",$segment);//[' 139.217.0.1 ', []    $beginIpArray=Explode(".",$segmentInfo[0]);//[139,217,0,1]    $mask=intval($segmentInfo[' 1 ']);// -    $endIp=Array(); foreach($beginIpArray  as $ipKey=$item) {        $beginFlag= 8 * ($ipKey);//0 8        $endFlag= 8 * ($ipKey+ 1);//8        $decbinItem=Str_pad(Decbin($item), 8, "0",str_pad_left); $endIp[] =$mask>=$endFlag?$item: ($mask>$beginFlag?Bindec(Str_pad(substr($decbinItem, 0,$mask-$beginFlag), 8, "1", Str_pad_right)): ($ipKey<= 2?POW(2, 8)-1:POW(2, 8)-1)); }    $ipArray=Array();  for($beginIp[0] =$beginIpArray[0];$beginIp[0] <=$endIp[0];$beginIp[0]++) {         for($beginIp[1] =$beginIpArray[1];$beginIp[1] <=$endIp[1];$beginIp[1]++) {             for($beginIp[2] =$beginIpArray[2];$beginIp[2] <=$endIp[2];$beginIp[2]++) {                 for($beginIp[3] =$beginIpArray[3];$beginIp[3] <=$endIp[3];$beginIp[3]++) {                    $ipArray[] =implode(".",$beginIp); }            }        }    }    return $ipArray;}
/** * Assign subnet segment in specified network segment * @param string $segment Specify network segment * @param int $ipNum IP required * @param array $usedIpArray unavailable (already used) IP, default is empty Array * @return bool|string successful Returns the allocated network segment*/functionAllocatesegment ($segment,$ipNum,$usedIpArray= []){    $usedIpArray=Empty($usedIpArray) ? [] :Array_flip($usedIpArray); //calculate how many IPs are required    $i= 0; $ipCount=POW(2,$i);  while($ipCount<$ipNum) {        $i++; $ipCount=POW(2,$i); }    $newMask= 32-$i; //start and end IP for large network segments    $segmentInfo=Explode("/",$segment);//[' 139.217.0.1 ', []    $beginIpArray=Explode(".",$segmentInfo[0]);//[139,217,0,1]    $mask=intval($segmentInfo[' 1 ']);// -    if($newMask<$mask) {        return false; }    $endIp=Array(); $step= []; foreach($beginIpArray  as $ipKey=$item) {        $beginFlag= 8 * ($ipKey);//0 8        $endFlag= 8 * ($ipKey+ 1);//8        $step[$ipKey] =$newMask>$endFlag? 1: ($endFlag-$newMask< 8?POW(2,$endFlag-$newMask) :POW(2, 8)); $decbinItem=Str_pad(Decbin($item), 8, "0",str_pad_left); $endIp[] =$mask>=$endFlag?$item: ($mask>$beginFlag?Bindec(Str_pad(substr($decbinItem, 0,$mask-$beginFlag), 8, "1", Str_pad_right)): ($ipKey<= 2?POW(2, 8)-1:POW(2, 8)-1)); }    //Traverse the Build network segment     for($beginIp[0] =$beginIpArray[0];$beginIp[0] <=$endIp[0];$beginIp[0] + =$step[0]) {         for($beginIp[1] =$beginIpArray[1];$beginIp[1] <=$endIp[1];$beginIp[1] + =$step[1]) {             for($beginIp[2] =$beginIpArray[2];$beginIp[2] <=$endIp[2];$beginIp[2] + =$step[2]) {                 for($beginIp[3] =$beginIpArray[3];$beginIp[3] <=$endIp[3];$beginIp[3] + =$step[3]) {                    $newSegment=implode(‘.‘,$beginIp) . ‘/‘ .$newMask; //get all the IP of the network segment                    $ipArray= Getipbysegment ($newSegment); $canUse=true; //Determine if the network segment is available                    if(!Empty($usedIpArray)) {                        foreach($ipArray  as $ip) {                            if(isset($usedIpArray[$ip])) {                                $canUse=false;  Break; }                        }                    }                    if($canUse) {                        return $newSegment; }                }            }        }    }    return false;}

PHP calculates all the IP in the network segment, assigns sub-network segments

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.