我试着在论坛上搜索,却找不到这个。找到的东西将打破一个CIDR块完全分开,但我需要两个单独的功能。
第一个函数将接受大于/24的CIDR块,并将其分解为/24块。
我实际上已经完成的第二个函数是,然后将每个/24分解为其256个IP地址。答案可以在这里找到。Exploding given IP range with the PHP
因此,我试图弄清楚如何创建传递/23或更大的CIDR块的函数,并将其分解为/24s。
示例:
输入: BreakTo24(10.0.0.0/22)
输出:
10.0.0.0/24
10.0.1.0/24
10.0.2.0/24
10.0.3.0/24
编辑:我意识到我没有发布我的代码尝试,这可能使这更难帮助。以下是代码:
function BreakTo24($CIDR){
$CIDR = explode ("/", $CIDR);
//Math to determine if the second part of the array contains more than one /24, and if so how many.发布于 2013-09-20 10:16:30
我已经(通过IRC提供了一些帮助)发现我没有有效地完成这个任务,并且需要使用ip2long函数。
我测试了这个,它做了我想做的事。这是我完成的代码,希望有人会发现它有用。
// Function to take greater than a /24 CIDR block and make it into a /24
Function BreakTo24($CIDR)
{
$CIDR = explode("/", $CIDR); // this breaks the CIDR block into octlets and /notation
$octet = ip2long($CIDR[0]); //turn the first 3 octets into a long for calculating later
$NumberOf24s = pow(2,(24-$CIDR[1]))-1; //calculate the number of /24s in the CIDR block
$OutputArray = array();
for ($i=-256; $i<256 * $NumberOf24s; $OutputArray[] = (long2ip($octet + ($i += 256)))); //fancy math to output each /24
return $OutputArray; //returns an array of ranges}
https://stackoverflow.com/questions/18904895
复制相似问题