【问题标题】:How to get each IP address in subnet?如何获取子网中的每个 IP 地址?
【发布时间】:2015-02-24 15:22:48
【问题描述】:

所以下面的代码是不久前由另一个用户发布的。

我想知道在 "net['firstip']net['lastip']" 之间获取每个 IP 地址的最佳方法是什么?

我尝试了几种从十进制转换为二进制的方法,但没有成功。我的想法是for() 增加二进制firstip 直到达到lastip,并将每一步转换为等效于放入新数组的十进制。

感谢所有帮助。我把 *** 放在我试图找到 IP 的两个特定部分之间。

function zerofill($val,$len,$reverse = false){
    while(strlen($val)<$len)
        if($reverse)
            $val .= "0";
        else
            $val = "0".$val;
    return $val;

}


function hextoip($hex){
    for($i=0;$i<4;$i++)
        @$ip .= base_convert(substr($hex,$i*2,2),16,10).".";
    $ip = substr($ip,0,-1);
    return $ip;
}



function iptohex($ip){
    $iparr = explode(".",$ip);
    foreach($iparr as $i => $group){
        @$hex .= zerofill(base_convert($group,10,16),2);
    }
    return $hex;
}



function hextobin($hex){
    $bin = zerofill(base_convert($hex,16,2),32);
    return $bin;
}



function bintohex($bin) {
    $hex = zerofill(base_convert($bin,2,16),8);
    return $hex;
}



function getnet($ip, $netmask){
    $iphex = iptohex($ip);
    $netbin = hextobin($iphex);
    $maskhex = iptohex($netmask);
    $maskbin = hextobin($maskhex);

    $hostbits = substr($maskbin,strpos($maskbin,"0"));
    $netbits = substr($maskbin,0,strpos($maskbin,"0"));
    $nethex = bintohex(zerofill(substr($netbin,0,strlen($netbits)),32,true));

    $hosts = pow(2,strlen($hostbits));
    $available_hosts = $hosts-3;

    $net["ip"] = $ip;
    $net["mask"] = $netmask;
    $net["network"] = hextoip($nethex);
    $net["netstr"] = $net["network"]."/".strlen($netbits);  
    $net["hosts"] = $available_hosts;
    *** $net["firstip"] = hextoip(zerofill(base_convert(base_convert($nethex,16,10)+1,10,16),8));;
    *** $net["lastip"] = hextoip(zerofill(base_convert(base_convert($nethex,16,10)+$available_hosts+1,10,16),8));
    $net["broadcast"] = hextoip(zerofill(base_convert(base_convert($nethex,16,10)+$available_hosts+2,10,16),8));
    return $net;
} `

【问题讨论】:

    标签: php arrays binary hex subnet


    【解决方案1】:

    您可以使用以下代码获取从第一个IP到最后一个IP的所有IP:

    <?php
    
    function getIpRange(  $cidr) {
    
        list($ip, $mask) = explode('/', $cidr);
    
        $maskBinStr =str_repeat("1", $mask ) . str_repeat("0", 32-$mask );      //net mask binary string
        $inverseMaskBinStr = str_repeat("0", $mask ) . str_repeat("1",  32-$mask ); //inverse mask
    
        $ipLong = ip2long( $ip );
        $ipMaskLong = bindec( $maskBinStr );
        $inverseIpMaskLong = bindec( $inverseMaskBinStr );
        $netWork = $ipLong & $ipMaskLong; 
    
        $start = $netWork+1;//ignore network ID(eg: 192.168.1.0)
    
        $end = ($netWork | $inverseIpMaskLong) -1 ; //ignore brocast IP(eg: 192.168.1.255)
        return array('firstIP' => $start, 'lastIP' => $end );
    }
    
    function getEachIpInRange ( $cidr) {
        $ips = array();
        $range = getIpRange($cidr);
        for ($ip = $range['firstIP']; $ip <= $range['lastIP']; $ip++) {
            $ips[] = long2ip($ip);
        }
        return $ips;
    }
    
    $cidr = '172.16.0.0/27'; // max. 30 ips
    print_r(getEachIpInRange ( $cidr));
    
    /* output: 
    Array                                                                 
    (                                                                     
        [0] => 172.16.0.1                                                 
        [1] => 172.16.0.2
        [2] => 172.16.0.3
        ...
        [27] => 172.16.0.28                                               
        [28] => 172.16.0.29                                               
        [29] => 172.16.0.30
    ) 
    */
    

    如果您需要更多信息,请查看此处的 PHP 手册:http://php.net/manual/de/function.ip2long.php
    我的示例代码中的某些部分来自本手册。

    【讨论】:

    • 通常有效的代码,但如果您指定 32 的子网掩码... - 会遇到麻烦。 ;-) 我会在 getIpRange ($ cidr) 函数中添加 2 行。 if (empty ($ mask)) $ mask = 32;if ($ mask == 32) $ start = $ end = $ ipLong;
    猜你喜欢
    • 2018-11-24
    • 1970-01-01
    • 1970-01-01
    • 2013-02-06
    • 2015-07-02
    • 1970-01-01
    • 2014-12-31
    • 2014-05-28
    • 1970-01-01
    相关资源
    最近更新 更多