如何检测IP是否在网络中? [英] How can I detect if an IP is in a network?

查看:151
本文介绍了如何检测IP是否在网络中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中找到了( lib ipaddress )。它是python的例子。

I found in python (lib ipaddress). It`s python example.

ip="1.232.12.3"
net="1.232.12.0/20"
ip in net

结果 true

我可以在Java中找到它吗?

Can I find this in Java?

推荐答案

您要问的是IP是否在给定的 cidr范围内。您可以编写自己的代码来确定cidr的开始和结束,看看IP是否属于该范围,或者只使用 Apache Commons Net 库。

What you're asking is if an IP is in a given cidr range. You could write your own code to figure out the start and end of the cidr and see if the IP falls in that range, or just use the Apache Commons Net library.

SubnetUtils 类完全符合你的要求:

The SubnetUtils class does exactly what you want:

String cidrRange = "1.232.12.0/20";
String addr = "1.232.12.3";
SubnetUtils utils = new SubnetUtils(cidrRange);
boolean isInRange = utils.getInfo().isInRange(addr);

这篇关于如何检测IP是否在网络中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆