Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I filter and select only IPV4 adresses on the InetAddress object? How to exlude the IPV6?

I have the following problem: I create an ArrayList and I put in this arraylist all the IP addresses of my client (one if the client have a single network card, n if the client run on a PC having n network card) excluding the loopback adress, the point to point adress and the virtual adress.

I have do this in this way:

private static List<String> allIps = new ArrayList<String>();

static {
    Enumeration<NetworkInterface> nets;
    try {

        nets = NetworkInterface.getNetworkInterfaces();


        while(nets.hasMoreElements()) {

            NetworkInterface current = nets.nextElement();

            if ((current.isUp()) && (!current.isPointToPoint()) && (!current.isVirtual()) && (!current.isLoopback())) {
                System.out.println(current.getName());
                Enumeration<InetAddress> ee = current.getInetAddresses();


                    while (ee.hasMoreElements()) {
                        InetAddress i = ee.nextElement();
                        System.out.println(i.getHostAddress());
                        allIps.add(i.getHostAddress());

                    }
            }
        }
    } catch (SocketException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    System.out.println("List of all IP on this client: "
            + allIps.toString());
    System.out.println("Number of ip: " + allIps.size());

}

It seems work well, the only problem is that my output (in the Eclipse console) is:

eth0
fe80:0:0:0:20c:29ff:fe15:3dfe%2
192.168.15.135
List of all IP on this client: [fe80:0:0:0:20c:29ff:fe15:3dfe%2, 192.168.15.135]
Number of ip: 2

Using the debugger and the console output appear clear to me that, in this case, the only network interface present is eth0 (and this is ok) but, for this network interface, id found 2 IP adresses (the fits one is IPV6 address, the second one is the classic IPV4 address)

So it put in my adresses list allIps both.

I want select and put in my allIps list only the IPV4 adresses and not also the IPV6. What can I do to do it? Can I filter and select only IPV4 on my InetAddress object?

Tnx

Andrea

like image 995
AndreaNobili Avatar asked Dec 03 '13 14:12

AndreaNobili


People also ask

What is IPv4 filter?

IPv4 Filter function controls the access to the Brother machine from a specific computer (IP address). It prevents unauthorized access and reduces unnecessary printing costs. You can specify not only by IP address but also by subnet mask, therefore you can easily specify several IP addresses.

What are some of the ways we can resolve ip4 address shortages?

IPv6 is the only solution to solve the IPv4 public address exhaustion, that is, to actually get rid of the limitation on the number of IP addresses.

What is the name of Python's built in module for IPv4 or IPv6 manipulation?

An introduction to the ipaddress module available on Python 3.3+ for manipulation of IPv4 and IPv6 addresses. In this article we'll take a look at the ipaddress module that is available on Python 3.3 and above.

Which IPv4 block of addresses is reserved for teaching and learning purposes?

The IPv4 address block 192.0. 2.0 to 192.0. 2.255 (192.0. 2.0/24) that is set aside for teaching and learning purposes.


1 Answers

Use instanceof and the Inet4Address type:

for (NetworkInterface ni :
                     Collections.list(NetworkInterface.getNetworkInterfaces())) {
  for (InetAddress address : Collections.list(ni.getInetAddresses())) {
    if (address instanceof Inet4Address) {
      System.out.println(address);
    }
  }
}
like image 176
McDowell Avatar answered Sep 30 '22 02:09

McDowell