从IP块计算网络掩码/网关 [英] Calculate Netmask/Gateway from IP Blocks
本文介绍了从IP块计算网络掩码/网关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们的数据中心为我们提供了一个来自我们所有机器的JSON转储,以便导入我们自己的库存管理系统。这提供了诸如(192.168.1.1/26)之类的IP块,但是当我导入数百个块时,我还要计算网络掩码和网关。
Our Datacenter has given us a JSON dump from all of our machines to import into our own inventory management system. This supplies the IP Blocks such as (192.168.1.1/26) but as I am importing hundreds of blocks, I would like to also calculate the Netmask and Gateway.
I查看PHP Doxygen上的网络功能,但无法找到任何方法来执行此操作。如何从IP块计算网络掩码/网关?
I looked over the networking functions on the PHP Doxygen but was not able to find any way to do this. How can I calculate Netmask/Gateway from an IP Block?
推荐答案
您可以使用以下内容计算ip和掩码:
You can calculate the ip and mask using something like this:
$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);
这篇关于从IP块计算网络掩码/网关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文