java - InetAddress.getLocalHost().getHostAddress() is returning 127.0.1.1 -
this question has answer here:
my question similar this question . want real ip of machine (not 127.0.0.1) strange, below code in ubuntu returning 127.0.1.1
inetaddress.getlocalhost().gethostaddress()
below complete code, posted in @ here
public string getmachineip() { try { string hostip = inetaddress.getlocalhost().gethostaddress(); if (!hostip.equals("127.0.0.1")) { return hostip; } /* * above method returns "127.0.0.1", in case need * check available network interfaces */ enumeration<networkinterface> ninterfaces = networkinterface .getnetworkinterfaces(); while (ninterfaces.hasmoreelements()) { enumeration<inetaddress> inetaddresses = ninterfaces .nextelement().getinetaddresses(); while (inetaddresses.hasmoreelements()) { string address = inetaddresses.nextelement() .gethostaddress(); if (!address.equals("127.0.0.1")) { return address; } } } } catch (unknownhostexception e1) { system.err.println("error = " + e1.getmessage()); } catch (socketexception e1) { system.err.println("error = " + e1.getmessage()); } return null; }
the above code returning 127.0.1.1 whereas ifconfig
on ubuntu machine giving below output
root@dell:~# ifconfig eth0 link encap:ethernet hwaddr 00:21:70:b7:30:cd broadcast multicast mtu:1500 metric:1 rx packets:0 errors:0 dropped:0 overruns:0 frame:0 tx packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 rx bytes:0 (0.0 b) tx bytes:0 (0.0 b) interrupt:28 base address:0x6000 eth1 link encap:ethernet hwaddr 00:22:68:d3:02:b5 inet addr:192.168.2.112 bcast:192.168.2.255 mask:255.255.255.0 inet6 addr: fe80::222:68ff:fed3:2b5/64 scope:link broadcast running multicast mtu:1500 metric:1 rx packets:23827 errors:0 dropped:0 overruns:0 frame:32515 tx packets:23200 errors:46 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 rx bytes:22027719 (22.0 mb) tx bytes:3778268 (3.7 mb) interrupt:19 lo link encap:local loopback inet addr:127.0.0.1 mask:255.0.0.0 inet6 addr: ::1/128 scope:host loopback running mtu:16436 metric:1 rx packets:402 errors:0 dropped:0 overruns:0 frame:0 tx packets:402 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 rx bytes:29197 (29.1 kb) tx bytes:29197 (29.1 kb)
i found 127.0.1.1 entry in host file (strange me, since never updated file)
root@dell:~# cat /etc/hosts 127.0.0.1 localhost 127.0.1.1 dell # following lines desirable ipv6 capable hosts ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters ff02::3 ip6-allhosts
how real ip of machine (not 127.0.0.1)? looking ipv4 address excluding 127.0.0.0/8 subnet
you'll need use networkinterface
enumerate network interfaces; inetaddress.getlocalhost()
returns loopback. doesn't explain why 127.0.1.1 instead of 127.0.0.1, since method doesn't you're trying do, doesn't seem pertinent. see: http://docs.oracle.com/javase/6/docs/api/java/net/networkinterface.html#getinetaddresses()
Comments
Post a Comment