如何显示特定接口的 IP 地址?
How do you display the IP address for a specific interface?
所以我试图以编程方式从一个特定界面获取 IP 地址并将其显示在文本字段中。问题是它列出了几个。您如何遍历并获取双频 Wireless-AC 3160 的 IP 地址?
Dual Band Wireless-AC 3160 172.16.36.50
Dual Band Wireless-AC 3160 fe80:0:0:0:8877:2e6a:e4a1:c24f%wlan0
Virtual Ethernet Adapter for VMnet1 192.168.31.1
Virtual Ethernet Adapter for VMnet1 fe80:0:0:0:592c:71ac:f8d9:5899%eth3
Virtual Ethernet Adapter for VMnet8 192.168.245.1
Virtual Ethernet Adapter for VMnet8 fe80:0:0:0:ed34:4a7:cb5c:16ce%eth4
这是我用来显示界面的代码:
String ip;
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
int position;
while (interfaces.hasMoreElements()) {
NetworkInterface iface = interfaces.nextElement();
//This filters out the interfaces
// filters out 127.0.0.1 and inactive interfaces
if (iface.isLoopback() || !iface.isUp())
continue;
Enumeration<InetAddress> addresses = iface.getInetAddresses();
while(addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
ip = addr.getHostAddress();
System.out.println(iface.getDisplayName() + " " + ip);
}
}
} catch (SocketException e) {
throw new RuntimeException(e);
}
你得到两个IP地址,因为接口有两个IP地址。一般来说,可能还有更多。
如果您只想列出 IPv4 地址,您可以检查地址是否为 instanceof Inet4Address
。
使用此代码
import java.io.*;
import java.net.*;
import java.util.*;
import static java.lang.System.out;
public class ListNets {
public static void main(String args[]) throws SocketException {
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets))
displayInterfaceInformation(netint);
}
static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
out.printf("Display name: %s\n", netint.getDisplayName());
out.printf("Name: %s\n", netint.getName());
Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
for (InetAddress inetAddress : Collections.list(inetAddresses)) {
out.printf("InetAddress: %s\n", inetAddress);
}
out.printf("\n");
}
}
我通过执行以下操作解决了它。谢谢。
String ip;
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
int position =0;
while (interfaces.hasMoreElements()) {
NetworkInterface iface = interfaces.nextElement();
//This filters out the interfaces
// filters out 127.0.0.1 and inactive interfaces
if (iface.isLoopback() || !iface.isUp())
continue;
Enumeration<InetAddress> addresses = iface.getInetAddresses();
while(addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
ip = addr.getHostAddress();
position++;
if (position == 0 ) {
System.out.println(iface.getDisplayName() + " " + ip);
}
else{
break;
}
}
}
} catch (SocketException e) {
throw new RuntimeException(e);
}
所以我试图以编程方式从一个特定界面获取 IP 地址并将其显示在文本字段中。问题是它列出了几个。您如何遍历并获取双频 Wireless-AC 3160 的 IP 地址?
Dual Band Wireless-AC 3160 172.16.36.50
Dual Band Wireless-AC 3160 fe80:0:0:0:8877:2e6a:e4a1:c24f%wlan0
Virtual Ethernet Adapter for VMnet1 192.168.31.1
Virtual Ethernet Adapter for VMnet1 fe80:0:0:0:592c:71ac:f8d9:5899%eth3
Virtual Ethernet Adapter for VMnet8 192.168.245.1
Virtual Ethernet Adapter for VMnet8 fe80:0:0:0:ed34:4a7:cb5c:16ce%eth4
这是我用来显示界面的代码:
String ip;
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
int position;
while (interfaces.hasMoreElements()) {
NetworkInterface iface = interfaces.nextElement();
//This filters out the interfaces
// filters out 127.0.0.1 and inactive interfaces
if (iface.isLoopback() || !iface.isUp())
continue;
Enumeration<InetAddress> addresses = iface.getInetAddresses();
while(addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
ip = addr.getHostAddress();
System.out.println(iface.getDisplayName() + " " + ip);
}
}
} catch (SocketException e) {
throw new RuntimeException(e);
}
你得到两个IP地址,因为接口有两个IP地址。一般来说,可能还有更多。
如果您只想列出 IPv4 地址,您可以检查地址是否为 instanceof Inet4Address
。
使用此代码
import java.io.*;
import java.net.*;
import java.util.*;
import static java.lang.System.out;
public class ListNets {
public static void main(String args[]) throws SocketException {
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets))
displayInterfaceInformation(netint);
}
static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
out.printf("Display name: %s\n", netint.getDisplayName());
out.printf("Name: %s\n", netint.getName());
Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
for (InetAddress inetAddress : Collections.list(inetAddresses)) {
out.printf("InetAddress: %s\n", inetAddress);
}
out.printf("\n");
}
}
我通过执行以下操作解决了它。谢谢。
String ip;
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
int position =0;
while (interfaces.hasMoreElements()) {
NetworkInterface iface = interfaces.nextElement();
//This filters out the interfaces
// filters out 127.0.0.1 and inactive interfaces
if (iface.isLoopback() || !iface.isUp())
continue;
Enumeration<InetAddress> addresses = iface.getInetAddresses();
while(addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
ip = addr.getHostAddress();
position++;
if (position == 0 ) {
System.out.println(iface.getDisplayName() + " " + ip);
}
else{
break;
}
}
}
} catch (SocketException e) {
throw new RuntimeException(e);
}