Jim
Jim

Reputation: 19552

How can I get the actual IP in a linux machine from Java

I am trying to get the local IP address from a Linux machine BUT NOT get the loopback.
To do that I am using the following code (I am not sure if what I am doing is correct):

NetworkInterface ni = NetworkInterface.getByName("eth0");    
Enumeration<InetAddress> inetAddresses =  ni.getInetAddresses();
while(inetAddresses.hasMoreElements()) {  
         InetAddress ia = inetAddresses.nextElement();  
         if(!ia.isLinkLocalAddress()) {  
              //this is not loopback  
         }    
}  

When I run this I get 2 IPs (I was interested only in one of these) which when I do an ifconfig I see one (the one I want to get) is in the entry for eth0 while the other is in the entry for eth0:54.
I don't even know what is eth0:54.

How can I get the IP I want?

Upvotes: 2

Views: 2952

Answers (4)

Thirumal
Thirumal

Reputation: 9536

Try this,

import java.io.*;
import java.net.*;
import java.util.*;
import static java.lang.System.out;

public class ListNets {

public static void main(String args[]) throws SocketException, UnknownHostException {
     System.out.println(System.getProperty("os.name"));
     Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
    for (NetworkInterface netint : Collections.list(nets))
        if (netint.getName().equals("wlan0") || netint.getName().equals("en0")) {
             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("\n");
 }
}  

Upvotes: 0

Any modern computer have multiple IP-numbers, 127.0.0.1 being one of them. The actual configuration does not always get correctly reported up to the Java layer (in my experience).

You may simply want to execute /sbin/ifconfig -a on a scheduled basis (or at startup time) and log the complete output.

Upvotes: 1

zaf
zaf

Reputation: 23244

I had the same question but using PHP instead of Java:

Simply find the ip address of server

The best answer was that its not generally possible without serious back flips which doesn't really have to do with the language you are using and more to do with the underlying system.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533432

Linux machines can have more than one IP address including loopback. There is no concept of uniqueness for IP addresses.

What you might be looking for is the hostname (and its IP address) You can get this by reading /etc/hostname and looking up its IP address. Note: its possible it doesn't have an IP address if the machine is not setup in a normal manner.

Upvotes: 3

Related Questions