3

I need to get computer IP from Ubuntu using Java. I tried with InetAddress.getLocalHost.getHostAddress().toString(); but it returns 127.0.0.1 . I was searching for solution and found out this code:

NetworkInterface ni = NetworkInterface.getByName("eth0");
    Enumeration<InetAddress> inetAddresses =  ni.getInetAddresses();


    while(inetAddresses.hasMoreElements()) {
        InetAddress ia = inetAddresses.nextElement();
        if(!ia.isLinkLocalAddress()) {
            System.out.println("IP: " + ia.getHostAddress());
        }
    }
}

This code worked for me but problem is when computer uses "eth1" interface or computer can use wireless adapter to connect to network (wlan0). On that situatuon program will fail. Can you guys advise me with safe method to get IP from UNIX systems ? Regards.

user2496520
  • 881
  • 4
  • 13
  • 36
  • 1
    By 'safe', you mean 'reliable' rather than the opposite of 'dangerous', right? – Gabe Jun 25 '13 at 08:05
  • Yes. I mean method that will work on all UNIX system. My method is dangerous if machines use other interfaces except "eth0" For Window InetAddress.getLocalHost.getHostAddress().toString() is working. – user2496520 Jun 25 '13 at 08:07
  • 1
    What if the computer is used behind a home wireless firewall/router? Those routers typically assign addresses like 192.168.1.x to the computers in the home -- and multiplex the actual internet address assigned to the home. The address of the computer(192.168.1.101) may not be the address at which the computer could be contacted on the internet (32.78.23.4) if incoming is permitted and routed by the firewall. – Paul Jun 25 '13 at 08:12
  • Apart from the fact that a machine can use many network interfaces, each of which can have many ip addresses assigned to it, there's also the question, if you want the local addresses (for example, to decide which address a server process should bind to) or a public internet address (in order to know, to what address a peer should connect to). – T-Bull Jun 25 '13 at 08:22
  • Some computers wont be aware of their external IP - try sending a request of to a remote server – exussum Jun 25 '13 at 08:44
  • There is one more problem with "InetAddress.getLocalHost". This is prone to DNS spoofing. – Vishnu Dahatonde Sep 03 '21 at 06:50

3 Answers3

1

Use the enumeration getNetworkInterfaces(); and cycle through them.

                   Enumeration<NetworkInterface> eni = NetworkInterface.getNetworkInterfaces();

                    while (eni.hasMoreElements()) {
                            NetworkInterface ni = eni.nextElement();
                            Enumeration<InetAddress> inetAddresses =  ni.getInetAddresses();


                            while(inetAddresses.hasMoreElements()) {
                                    InetAddress ia = inetAddresses.nextElement();
                                    if(!ia.isLinkLocalAddress()) {
                                            System.out.println("Interface: " + ni.getName() + "   IP: " + ia.getHostAddress());

                                    }
                            }
                    }

On my linux box the isLinkLocalAddress() doesn't seem to work properly, as I get the 127.0.0.1 but that and the ipv6 version is easy to filter out manually.

Interface: wlan0   IP: 192.168.0.8
Interface: lo   IP: 0:0:0:0:0:0:0:1%1
Interface: lo   IP: 127.0.0.1

My machine is connected only on the wireless interface on 192.168.0.8

GregHNZ
  • 7,946
  • 1
  • 28
  • 30
1

Although a computer can have multiple network interfaces and different IPs, some of the interfaces can also be loopback or not running. To be "safe" you might even have to check names of the interface to see if you use the IP address from desired one.

Following method will give you a list of ip addresses from non-loopback, up and running interfaces.

 public static List<InetAddress> getIPAddress() throws SocketException {

    List<InetAddress> ipAddresses = new ArrayList<InetAddress>();
    Enumeration e;
    e = NetworkInterface.getNetworkInterfaces();
    while (e.hasMoreElements()) {
        NetworkInterface ni = (NetworkInterface) e.nextElement();
        if (ni.isLoopback() || !ni.isUp()) continue;

        for (Enumeration e2 = ni.getInetAddresses(); e2.hasMoreElements(); ) {
            InetAddress ip = (InetAddress) e2.nextElement();
            ipAddresses.add(ip);
        }
    }
    return ipAddresses;
}
Amila
  • 5,195
  • 1
  • 27
  • 46
0

Check out the answers to this SO question:

How to enumerate IP addresses of all enabled NIC cards from Java?

I haven't tested it, but it seem to apply.

Community
  • 1
  • 1
jpw
  • 44,361
  • 6
  • 66
  • 86