private static InetAddress getLinuxLocalIp() throws SocketException {
Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfaces
.nextElement();
logger.error("ni.getName():"+ni.getName());
Enumeration ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
ip = ips.nextElement();
logger.error("ip.isSiteLocalAddress():"+ip.isSiteLocalAddress());
logger.error("ip.isLoopbackAddress():"+ip.isLoopbackAddress());
logger.error("ip.getHostAddress():"+ip.getHostAddress());
if (ip != null && ip instanceof Inet4Address && ip.getHostAddress().indexOf(".") != -1) {// 1.获取符合条件的ip
//return ip;
} else {
ip = null;
}
}
}
return null;
}
代码如上,在测试环境上打印出来的语句如下:
19:24:34,920 [TimerListener] ERROR ni.getName():virbr0
19:24:34,921 [TimerListener] ERROR ip.isSiteLocalAddress():false
19:24:34,921 [TimerListener] ERROR ip.isLoopbackAddress():false
19:24:34,921 [TimerListener] ERROR ip.getHostAddress():fe80:0:0:0:200:ff:fe00:0%4
19:24:34,921 [TimerListener] ERROR ip.isSiteLocalAddress():true
19:24:34,921 [TimerListener] ERROR ip.isLoopbackAddress():false
19:24:34,921 [TimerListener] ERROR ip.getHostAddress():192.168.122.1// 不是我想要的
19:24:34,921 [TimerListener] ERROR ni.getName():eth0
19:24:34,921 [TimerListener] ERROR ip.isSiteLocalAddress():false
19:24:34,921 [TimerListener] ERROR ip.isLoopbackAddress():false
19:24:34,921 [TimerListener] ERROR ip.getHostAddress():fe80:0:0:0:250:56ff:fea4:46e8%2
19:24:34,922 [TimerListener] ERROR ip.isSiteLocalAddress():true
19:24:34,922 [TimerListener] ERROR ip.isLoopbackAddress():false
19:24:34,922 [TimerListener] ERROR ip.getHostAddress():192.168.2.1** // 这个是要获取的,真实的
19:24:34,922 [TimerListener] ERROR ni.getName():lo
19:24:34,922 [TimerListener] ERROR ip.isSiteLocalAddress():false
19:24:34,922 [TimerListener] ERROR ip.isLoopbackAddress():true
19:24:34,922 [TimerListener] ERROR ip.getHostAddress():0:0:0:0:0:0:0:1%1
19:24:34,922 [TimerListener] ERROR ip.isSiteLocalAddress():false
19:24:34,922 [TimerListener] ERROR ip.isLoopbackAddress():true
19:24:34,922 [TimerListener] ERROR ip.getHostAddress():127.0.0.1// 不是我想要的
代码放到正式环境下打印日志如下:
20:09:05,303 [TimerListener] ERROR ni.getName():virbr0
20:09:05,305 [TimerListener] ERROR ip.isSiteLocalAddress():false
20:09:05,305 [TimerListener] ERROR ip.isLoopbackAddress():false
20:09:05,305 [TimerListener] ERROR ip.getHostAddress():fe80:0:0:0:200:**:***:***
20:09:05,306 [TimerListener] ERROR ip.isSiteLocalAddress():true
20:09:05,306 [TimerListener] ERROR ip.isLoopbackAddress():false
20:09:05,306 [TimerListener] ERROR ip.getHostAddress():192.168.122.1// 不是我想要的
20:09:05,307 [TimerListener] ERROR ni.getName():bond0
20:09:05,307 [TimerListener] ERROR ip.isSiteLocalAddress():false
20:09:05,308 [TimerListener] ERROR ip.isLoopbackAddress():false
20:09:05,308 [TimerListener] ERROR ip.getHostAddress():fe80:0:0:0:5ef3:f***:****:******
20:09:05,309 [TimerListener] ERROR ip.isSiteLocalAddress():false
20:09:05,309 [TimerListener] ERROR ip.isLoopbackAddress():false
20:09:05,309 [TimerListener] ERROR ip.getHostAddress():19*.1*.*.*** // 这个是我要获取的
20:09:05,310 [TimerListener] ERROR ni.getName():usb0
20:09:05,310 [TimerListener] ERROR ip.isSiteLocalAddress():false
20:09:05,311 [TimerListener] ERROR ip.isLoopbackAddress():false
20:09:05,311 [TimerListener] ERROR ip.getHostAddress():fe80:0:0:0:5cf3:f***:****:*****
20:09:05,311 [TimerListener] ERROR ip.isSiteLocalAddress():false
20:09:05,312 [TimerListener] ERROR ip.isLoopbackAddress():false
20:09:05,312 [TimerListener] ERROR ip.getHostAddress():1**.2**.9*.1** // 不是我想要的
20:09:05,312 [TimerListener] ERROR ni.getName():lo
20:09:05,313 [TimerListener] ERROR ip.isSiteLocalAddress():false
20:09:05,313 [TimerListener] ERROR ip.isLoopbackAddress():true
20:09:05,314 [TimerListener] ERROR ip.getHostAddress():0:0:0:0:0:0:0:1%1
20:09:05,314 [TimerListener] ERROR ip.isSiteLocalAddress():false
20:09:05,314 [TimerListener] ERROR ip.isLoopbackAddress():true
20:09:05,315 [TimerListener] ERROR ip.getHostAddress():127.0.0.1// 不是我想要的
遍历时要获取我想要的Inet4Address 地址,但是有那么多不是我想要的,怎么把它们过滤掉?看网上的方法都是判断红色日志中的内容,但是为什么测试环境是true false,正式环境中却是false false?