大家好,我想要得到eth0接口的ipv6地址,
例如,我的eth0接口:
eth0 : Link encap:Ethernet HWaddr 11:11:11:11:11:11 inet addr:11.11.11.11 Bcast:11.11.11.255 Mask:255.255.255.0 inet6 addr: 1111:1111:1111:1111:1111:1111/64 Scope:Link
如何在java中获取inet6地址?
我无法正确使用InetAdress类。它始终返回ipv4地址。
发布于 2014-08-06 17:26:41
谢谢你的建议。我用下面的代码获得了关于接口的所有信息。
此外,InetAddress类在Linux中不能正常工作。(我尝试在linux中获取硬件地址),但代码在windows和linux上工作正常。
import java.io.*;
import java.net.*;
import java.util.*;
import static java.lang.System.out;
public class GetInfo {
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("Up? %s\n", netint.isUp());
out.printf("Loopback? %s\n", netint.isLoopback());
out.printf("PointToPoint? %s\n", netint.isPointToPoint());
out.printf("Supports multicast? %s\n", netint.supportsMulticast());
out.printf("Virtual? %s\n", netint.isVirtual());
out.printf("Hardware address: %s\n",
Arrays.toString(netint.getHardwareAddress()));
out.printf("MTU: %s\n", netint.getMTU());
out.printf("\n");
}
} 致以问候。
发布于 2018-08-02 17:31:57
private static String getIPAddress(boolean v6) throws SocketException {
Enumeration<NetworkInterface> netInts = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netInt : Collections.list(netInts)) {
if (netInt.isUp() && !netInt.isLoopback()) {
for (InetAddress inetAddress : Collections.list(netInt.getInetAddresses())) {
if (inetAddress.isLoopbackAddress()
|| inetAddress.isLinkLocalAddress()
|| inetAddress.isMulticastAddress()) {
continue;
}
if (v6 && inetAddress instanceof Inet6Address) {
return inetAddress.getHostAddress();
}
if (!v6 && inetAddress instanceof InetAddress) {
return inetAddress.getHostAddress();
}
}
}
}
return null;
}发布于 2014-08-05 21:01:58
您可以使用Inet6Address子类请参阅http://docs.oracle.com/javase/6/docs/api/java/net/Inet6Address.html
https://stackoverflow.com/questions/25134753
复制相似问题