摘要:Java 取得Linux 本地IP位飛(抓網卡)
public NetworkInterface getEth0NetworkInterface() throws Exception {
Enumeration nets = NetworkInterface.getNetworkInterfaces();
NetworkInterface netInterfaceEth0 = null;
for (Object obj : Collections.list(nets)) {
NetworkInterface netInterface = (NetworkInterface) obj;
if (netInterface.getName().equals("eth0")) {
netInterfaceEth0 = netInterface;
}
}
return netInterfaceEth0;
}
public String getEth0IP(NetworkInterface netInterface) {
String ip = "";
if (netInterface != null) {
Enumeration addresses = netInterface.getInetAddresses();
for (Object obj : Collections.list(addresses)) {
InetAddress inetAddress = (InetAddress) obj;
if (inetAddress instanceof Inet6Address) {
continue;
} else {
ip = inetAddress.getHostAddress();
}
}
}
return ip;
}