获取Linux中etho和ens33网卡的ip
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(); }
以上内容原创来自每特学院,每特学员学习并整理!
给学院打波广告:
http://www.mayikt.com/
浙公网安备 33010602011771号