dongyo7931 2014-12-27 22:17
浏览 45
已采纳

如何获取子网中的每个IP地址?

So the code below was posted by another user a while back.

I'm wondering what the best way to get each IP address between "net['firstip'] and net['lastip']" is?

I tried converting from decimal to binary a few ways, but it wasn't working. My thought was to for() increase the binary firstip until it reaches the lastip, and convert each step to a decimal equivalent to put in a new array.

All help is appreciated. I put *** by the two specific pieces I'm trying to find the IP's between.

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;
} `
  • 写回答

1条回答 默认 最新

  • dqsp60748 2015-01-06 20:40
    关注

    You can use the following code to get all IPs from the first to last 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
    ) 
    */
    

    If you need more infos have a look into the PHP manual right here: http://php.net/manual/de/function.ip2long.php
    Some parts from my example code are from this manual.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算