0

我试过搜索论坛,但在任何地方都找不到。找到了可以将 CIDR 块完全分开的东西,但我分别需要 2 个函数。

第一个函数将采用大于 /24 的 CIDR 块并将其分解为 /24 块。

我实际上主要完成的第二个功能是将每个 /24 分解为它的 256 个 IP 地址。可以在这里找到答案。 使用 PHP 扩展给定的 IP 范围

所以我试图弄清楚如何创建一个传递 /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.
4

1 回答 1

3

我(在 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

}

于 2013-09-20T10:16:30.977 回答