impor java.net.InetAddress;
impor java.net.NetworkInterface;
impor java.net.SocketException;
import java.util.Enumeration;
Tes Internet kelas publik {
public static void main(String[] args) {
String netCard = "lo";
mencoba {
Pencacahan<NetworkInterface> netInterfaces = NetworkInterface
.getNetworkInterfaces();
jika (netInterfaces.hasMoreElements()) {
NetworkInterface netInterface = netInterfaces.nextElement();
if (netCard.equals(netInterface.getName())) {
// 子接口,linux下会取到父接口??
Pencacahan<NetworkInterface> subnetInterfaces = netInterface
.getSubInterface();
while (subnetInterfaces.hasMoreElements()) {
NetworkInterface subnetInterface = subnetInterfaces
.nextElement();
Sistem.keluar.println(subnetInterface.getName());
Pencacahan<InetAddress> sub-alamat = netInterface
.getInetAddresses();
while (sub-alamat.hasMoreElements()) {
Subalamat InetAddress = subalamat.nextElement();
Sistem.keluar.println(subalamat.getHostAddress());
}
}
// 打印接口下所有IP
Sistem.keluar.println(netInterface.getName());
Pencacahan<InetAddress> alamat = netInterface
.getInetAddresses();
while (alamat.hasMoreElements()) {
Alamat InetAddress = alamat.nextElement();
Sistem.keluar.println(alamat.getHostAddress());
}
}
}
} tangkapan (SocketException e) {
e.printStackTrace();
}
}
}