Java中获取所有IP子网详细信息的方法

huangapple go评论65阅读模式
英文:

method in Java to get all IP subnet detail

问题

我有一个来自不同子网的IP列表,例如:

10.181.192.18	255.255.255.248
10.181.192.26	255.255.255.248

我想为这些IP生成所有的详细信息,例如网络、前缀(例如在这种情况下是/29),可能还有范围等。

我是否正确,Java没有这样的方法来生成这些详细信息?

英文:

I have a list of IPs from different subnets, e.g.:

10.181.192.18	255.255.255.248
10.181.192.26	255.255.255.248

I want to generate all IP details for such IPs, e.g. network, prefix (e.g. in such case is /29), maybe ranges etc.

Am I right that Java does not have such methods to generate such details?!

答案1

得分: 1

如果这些是4个IP地址,就不可能从中生成网络、网络掩码和子网范围。

如果它们是用于>此<计算机的IP地址,那么您可以使用NetworkInterface API中的方法来查询接口及其绑定的InterfaceAddress列表。InterfaceAddress允许您检索前缀长度,然后您可以使用它来计算IP范围。

如果这些实际上是IP地址 + 子网掩码对(就像它们对我来说是*的),那么您已经拥有所需的信息。只需从子网掩码计算前缀长度,从IP和子网掩码计算网络范围。

我不知道任何Java SE API可以进行这些计算,但它们很简单。将IP地址和掩码转换为整数(或字节数组),计算位数,执行一些位操作,然后转换回标准表示法。

英文:

If those are 4 IP addresses, it is impossible to generate the network, network mask and subnet range from them.

If they are IP addresses for >this< computer, then you can use methods in the NetworkInterface API to interrogate the interfaces and their bound InterfaceAddress list. The InterfaceAddress allows you to retrieve the prefix length which you can then use to compute the IP range.

If those are actually IP address + netmask pairs (as they appear to me to be) then you have the information you need already. It is simply a matter of computing the prefix length from the netmask and the network range from IP and netmask.

I'm not aware of any Java SE APIs that do the calculations, but they are straight-forward. Convert the IP addresses and masks to integers (or byte arrays), count bits, do some bitwise operations and convert back to the standard notation.

huangapple
  • 本文由 发表于 2020年8月29日 08:33:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63642375.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定