导入 java.net.InetAddress;
导入 java.net.NetworkInterface;
导入 java.net.SocketException;
导入java.util.Enumeration;
公共类互联网测试{
公共静态无效主(字符串[] args){
字符串网卡=“lo”;
尝试 {
枚举 <NetworkInterface> netInterfaces = NetworkInterface
.getNetworkInterfaces();
if (netInterfaces.hasMoreElements()) {
NetworkInterface netInterface = netInterfaces.nextElement();
if (netCard.equals(netInterface.getName())) {
// 子接口,linux下会取到父接口??
枚举 <NetworkInterface> 子网接口 = netInterface
.getSubInterfaces();
while (subnetInterfaces.hasMoreElements()) {
网络接口 子网接口 = 子网接口
.nextElement();
System.out.println(subnetInterface.getName());
枚举 <InetAddress> 子地址 = netInterface
.getInetAddresses();
while (subaddresses.hasMoreElements()) {
InetAddress 子地址 = subaddresses.nextElement();
System.out.println(subaddress.getHostAddress());
}
}
// 打印接口下所有IP
System.out.println(netInterface.getName());
枚举<InetAddress> 地址 = netInterface
.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress 地址 = 地址.nextElement();
System.out.println(address.getHostAddress());
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
}
}