導入 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();
}
}
}