Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safe method to get computer IP on UNIX using Java

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.

like image 698
user2496520 Avatar asked Nov 26 '25 05:11

user2496520


2 Answers

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;
}
like image 163
Amila Avatar answered Nov 27 '25 17:11

Amila


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

like image 20
GregHNZ Avatar answered Nov 27 '25 18:11

GregHNZ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!