2 php ip networking gateway netmask
我们的数据中心为我们提供了所有机器的 JSON 转储,以导入我们自己的库存管理系统。这提供了 IP 块,例如 (192.168.1.1/26),但是当我导入数百个块时,我还想计算网络掩码和网关。
我查看了 PHP Doxygen 上的网络功能,但找不到任何方法来做到这一点。如何从 IP 块计算网络掩码/网关?
您可以使用以下方法计算 ip 和掩码:
$ip_with_mask = '192.168.1.1/26';
list($ip, $mask_int) = explode('/', $ip_with_mask);
$mask_nr = (pow(2, $mask_int) - 1) << (32 - $mask_int);
//pow(2, $x) - 1 changes the number to a number made of that many set bits
//and $y << (32 - $x) shifts it to the end of the 32 bits
$mask = long2ip($mask_nr);
$subnet_ip = long2ip(ip2long($ip) & $mask_nr);
$gateway_ip = long2ip((ip2long($ip) & $mask_nr) + 1);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2524 次 |
最近记录: |