Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get local IP-Address without connecting to the internet

So, I'm trying to get the IP-Address of my machine in my local network (which should be 192.168.178.41).

My first intention was to use something like this:

InetAddress.getLocalHost().getHostAddress(); 

but it only returns 127.0.0.1, which is correct but not very helpful for me.

I searched around and found this answer https://stackoverflow.com/a/2381398/717341, which simply creates a Socket-connection to some web-page (e.g. "google.com") and gets the local host address from the socket:

Socket s = new Socket("google.com", 80); System.out.println(s.getLocalAddress().getHostAddress()); s.close(); 

This does work for my machine (it returns 192.168.178.41), but it needs to connect to the internet in order to work. Since my application does not require an internet connection and it might look "suspicious" that the app tries to connect to google every time it is launched, I don't like the idea of using it.

So, after some more research I stumbled across the NetworkInterface-class, which (with some work) does also return the desired IP-Address:

Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()){     NetworkInterface current = interfaces.nextElement();     System.out.println(current);     if (!current.isUp() || current.isLoopback() || current.isVirtual()) continue;     Enumeration<InetAddress> addresses = current.getInetAddresses();     while (addresses.hasMoreElements()){         InetAddress current_addr = addresses.nextElement();         if (current_addr.isLoopbackAddress()) continue;         System.out.println(current_addr.getHostAddress());     } } 

On my machine, this returns the following:

name:eth1 (eth1) fe80:0:0:0:226:4aff:fe0d:592e%3 192.168.178.41 name:lo (lo) 

It finds both my network interfaces and returns the desired IP, but I'm not sure what the other address (fe80:0:0:0:226:4aff:fe0d:592e%3) means.

Also, I haven't found a way to filter it from the returned addresses (by using the isXX()-methods of the InetAddress-object) other then using RegEx, which I find very "dirty".

Any other thoughts than using either RegEx or the internet?

like image 358
Lukas Knuth Avatar asked Jan 06 '12 22:01

Lukas Knuth


People also ask

Can you get IP address without Internet connection?

Yes , IP address can exist without internet. Concept internet is connecting to external world , but even if you are not connected to internet you can assign IP adreess to computer.

How do I find my router's IP address without internet?

0.1." If you cannot find your router's IP address, click "Start," type "cmd" and press "Enter." Type "ipconfig" in the Command Prompt window and press "Enter." Look for "Default Gateway" to locate the IP address.


2 Answers

fe80:0:0:0:226:4aff:fe0d:592e is your ipv6 address ;-).

Check this using

if (current_addr instanceof Inet4Address)   System.out.println(current_addr.getHostAddress()); else if (current_addr instanceof Inet6Address)   System.out.println(current_addr.getHostAddress()); 

If you just care for IPv4, then just discard the IPv6 case. But beware, IPv6 is the future ^^.

P.S.: Check if some of your breaks should have been continues.

like image 104
yankee Avatar answered Oct 04 '22 05:10

yankee


Here is also a java 8 way of doing it:

public static String getIp() throws SocketException {      return Collections.list(NetworkInterface.getNetworkInterfaces()).stream()             .flatMap(i -> Collections.list(i.getInetAddresses()).stream())             .filter(ip -> ip instanceof Inet4Address && ip.isSiteLocalAddress())             .findFirst().orElseThrow(RuntimeException::new)             .getHostAddress(); } 
like image 29
tsds Avatar answered Oct 04 '22 05:10

tsds