importar java.net.InetAddress;
importar java.net.NetworkInterface;
importar java.net.SocketException;
importar java.util.Enumeration;
classe pública InternetTest {
public static void main(String[] args) {
String netCard = "lo";
tentar {
Enumeração<NetworkInterface> netInterfaces = NetworkInterface
.getNetworkInterfaces();
if (netInterfaces.hasMoreElements()) {
NetworkInterface netInterface = netInterfaces.nextElement();
if (netCard.equals(netInterface.getName())) {
// 子接口,linux下会取到父接口??
Enumeração<NetworkInterface> subnetInterfaces = netInterface
.getSubInterfaces();
enquanto (subnetInterfaces.hasMoreElements()) {
NetworkInterface subnetInterface = subnetInterfaces
.nextElement();
System.out.println(subnetInterface.getName());
Enumeração<InetAddress> subendereços = netInterface
.getInetAddresses();
while (subaddresses.hasMoreElements()) {
Subendereço InetAddress = subaddresses.nextElement();
System.out.println(subaddress.getHostAddress());
}
}
// 打印接口下所有IP
System.out.println(netInterface.getName());
Enumeração<InetAddress> endereços = netInterface
.getInetAddresses();
enquanto (addresses.hasMoreElements()) {
Endereço InetAddress = endereços.nextElement();
System.out.println(address.getHostAddress());
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
}
}