importer java.net.InetAddress ;
importer java.net.NetworkInterface ;
importer java.net.SocketException ;
importer java.util.Enumeration ;
classe publique InternetTest {
public static void main (String[] arguments) {
Chaîne netCard = "lo" ;
essayer {
Énumération<NetworkInterface> netInterfaces = NetworkInterface
.getNetworkInterfaces();
si (netInterfaces.hasMoreElements()) {
NetworkInterface netInterface = netInterfaces.nextElement();
if (netCard.equals(netInterface.getName())) {
// 子接口,linux下会取到父接口??
Énumération<NetworkInterface> subnetInterfaces = netInterface
.getSubInterfaces();
while (subnetInterfaces.hasMoreElements()) {
NetworkInterface subnetInterface = subnetInterfaces
.nextElement();
System.out.println(subnetInterface.getName());
Enumeration<InetAddress> sous-adresses = netInterface
.getInetAddresses();
while (sous-adresses.hasMoreElements()) {
Sous-adresse InetAddress = subaddresses.nextElement();
System.out.println(sous-adresse.getHostAddress());
}
}
// 打印接口下所有IP
System.out.println(netInterface.getName());
Enumération<InetAddress> adresses = netInterface
.getInetAddresses();
while (adresses.hasMoreElements()) {
Adresse InetAddress = adresses.nextElement();
System.out.println(address.getHostAddress());
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
}
}