简体   繁体   English

计算哪个子网是IP地址

[英]Calculate in which subnet is IP address

I have an IP address and mask (for example IP is 10.128.20.1, mask is 255.255.248.0). 我有一个IP地址和掩码(例如IP为10.128.20.1,掩码为255.255.248.0)。 How I can calculate, in which subnet range is IP added? 如何计算在哪个子网范围内添加IP?

I try implement founded solution, where I list all subnets by IP and mask and next I do test IP address, whether is in subnet. 我尝试实施已建立的解决方案,在其中按IP和掩码列出所有子网,然后测试IP地址(是否在子网中)。 But I try find easier way. 但我尝试找到更简单的方法。

Thanks, I attach a part of solution: 谢谢,我附上解决方案的一部分:

   $ip_addr = "192.168.36.9"; 
   $subnet_mask = "255.255.248.0"; 

   $ip = ip2long($ip_addr); 
   $nm = ip2long($subnet_mask); 
   $nw = ($ip & $nm); 
   $bc = $nw | (~$nm); 

   echo "IP Address:         " . long2ip($ip) . "\n"; 
   echo "Subnet Mask:        " . long2ip($nm) . "\n"; 
   echo "Network Address:    " . long2ip($nw) . "\n"; 
   echo "Broadcast Address:  " . long2ip($bc) . "\n"; 
   echo "Number of Hosts:    " . ($bc - $nw - 1) . "\n"; 
   echo "Host Range:         " . long2ip($nw + 1) . " -> " . long2ip($bc - 1)  . "\n"; 

This work, if I use zero subnet. 如果我使用零子网,这项工作。 For no zero subnet this example not work. 对于没有零子网的情况,此示例不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM