6

I am using Java to build a simple method within a class that will grab the LAN IPv4 address of the user's machine. For the most part this works well, with one exception... the IP address I get back is the IPv4 address of my VirtualBox Ethernet adapter, as is proven when I enter ipconfig into the command prompt:

enter image description here

Here is the method that will grab the IP address:

import java.net.InetAddress;
import java.net.UnknownHostException;

...

private String getIP() {
  try {
    return InetAddress.getLocalHost().getHostAddress();
  } catch (UnknownHostException e) {
    return "0.0.0.0";
  }
}

Could anyone please show me how to work around this? I would like to avoid assuming that the end-user will not have VirtualBox (or something of the like) installed.

Thank you for your time.

Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195
  • isn't this a bit of a duplicate of http://stackoverflow.com/questions/7348711/recommended-way-to-get-hostname-in-java/7353473#7353473 ? – Arnout Engelen Nov 21 '11 at 22:55
  • Not quite. They are looking for the hostName. I am looking for the hostAddress. – Oliver Spryn Nov 21 '11 at 22:58
  • I was able to get my hostName just fine... – Oliver Spryn Nov 21 '11 at 22:59
  • Still, the comments that apply to getting the hostname also apply to getting the host address. Basically, you can't, as your machine may have multiple addresses or may be externally known by some address not even visible on the machine itself (e.g. when using NAT). – Arnout Engelen Nov 24 '11 at 16:17

2 Answers2

6

I think you need to look at the NetworkInterface class and seeing if it will help you exclude the virtual interface in this case:

    for (NetworkInterface networkInterface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
        //Perhaps networkInterface.isVirtual() will help you identify the correct one?
    }

I don't have any virtual interfaces on my setup, so I can't tell how well it works, but I hope that gives you a pointer.

Yishai
  • 90,445
  • 31
  • 189
  • 263
  • Thank you. I will look into this and let you know my results! – Oliver Spryn Nov 21 '11 at 23:23
  • Excellent! By using a modified version of your and prunge's suggestion, I was able to get and parse the output of this statement until I was just left with my LAN IP address. Thanks! – Oliver Spryn Nov 22 '11 at 03:18
1

Following on from Yishai's suggestion of using the NetworkInterface class, in my experience isVirtual() did not distinguish between VM network adapters and 'normal' ones.

But you can grab the MAC address using NetworkInterface.getHardwareAddress() and do some pattern matching to guess whether the network interface is for virtual machines. See this page for common virtual machine MAC address patterns.

There is no guarantee this technique will work, as most VM software allows the user to explicitly set the MAC address for network adapters. However, in the majority of cases, users will just get the VM software to generate one and so these patterns should cover the majority of cases.

prunge
  • 22,460
  • 3
  • 73
  • 80