Linux下用Java获取本机IP
2024-09-22 15:19:52
可能有多个网卡包括虚拟网卡,需要进行排除
String ip = "";
try {
Enumeration<?> e1 = NetworkInterface.getNetworkInterfaces();//获取多个网卡
while (e1.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) e1.nextElement(); if(("eth0").equals(ni.getName()) || ("ens33").equals(ni.getName())){//取“eth0”和“ens33”两个网卡
Enumeration<?> e2 = ni.getInetAddresses();
while (e2.hasMoreElements()) {
InetAddress ia = (InetAddress) e2.nextElement();
if (ia instanceof Inet6Address) {//排除IPv6地址
continue;
}
ip = ia.getHostAddress();
}
break;
}
}
} catch (SocketException e) {
e.printStackTrace();
}
最新文章
- 模板——RMQ
- ubuntu14.04 LTS 更新源
- BZOJ1272: [BeiJingWc2008]Gate Of Babylon
- QQwry
- [Python]peewee使用经验
- Android客户端与服务器交互方式-小结
- springboot 学习进度
- day17 python递归案例(二分查找,三级菜单)
- cocos2d-html5 碰撞检测的几种方法
- Java使用POI插件将数据以excel形式备份
- JAVA JComboBox的监听事件(ActionListener、ItemListener)
- python_ssh连接
- <;[完整版]中国式价值投资>;读书笔记
- java—实现一个监听器HttpServletRequest的创建销毁、在线人数 (56)
- linux 段页式内存管理
- linux memery dump
- spring+jdbc+template+transaction实现
- Windows服务的调试
- django实现密码加密的注册(数据对象插入)
- 小程序框架MpVue踩坑日记(一)