0

Hi everyone i want to get ipv6 address of eth0 interface,

for example my eth0 interface:

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

How can I get an inet6 address in java?

I cannot use the InetAdress class correctly. It always returns an ipv4 address.

codelion
  • 1,056
  • 1
  • 11
  • 17
Erol Guzoğlu
  • 486
  • 6
  • 24
  • duplicate http://stackoverflow.com/questions/1602452/can-inetaddress-be-used-with-inet6-ipv6 http://stackoverflow.com/questions/6908398/inetaddress-gethostaddress-ipv6-compliant – pezetem Aug 05 '14 at 09:03
  • Thanks. But, how can I get ipv6 address? I used InetAddress class, but do not return ipv6. I tried lots of code example – Erol Guzoğlu Aug 05 '14 at 09:27

3 Answers3

3

Thanks for suggestions. I got all of information about the interfaces with the following code.

Also, InetAddress class does not work correctly in Linux. (I tried get hardware adress in linux) But the code work on windows and linux correctly.

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");
 }
 }  

Regards.

Erol Guzoğlu
  • 486
  • 6
  • 24
1
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;
}
Vitaliy Polchuk
  • 1,878
  • 19
  • 12
-1

You can use the Inet6Address subclass see http://docs.oracle.com/javase/6/docs/api/java/net/Inet6Address.html

codelion
  • 1,056
  • 1
  • 11
  • 17