我们的数据中心为我们提供了所有机器的 JSON 转储,以便导入到我们自己的库存管理系统中。这提供了 IP 块,例如 (192.168.1.1/26),但当我导入数百个块时,我还想计算网络掩码和网关。
我查看了 PHP Doxygen 上的网络功能,但找不到任何方法来做到这一点。如何从 IP 块计算网络掩码/网关?
您可以使用如下方法计算 ip 和 mask:
$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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)