1

I am developing an android application which needs to get internet IP. Is it possible to get internet IP without calling an external server?

If possible, how can I fetch internet IP without making an additional network request?

VivekRajendran
  • 676
  • 1
  • 6
  • 21
  • You can check this link, and find ur answer https://stackoverflow.com/questions/3097589/getting-my-public-ip-via-api – timmyB Jul 03 '18 at 08:10
  • I think u can find answer from this url below https://stackoverflow.com/questions/3097589/getting-my-public-ip-via-api – timmyB Jul 03 '18 at 08:11
  • I think u can find answer from this url below https://stackoverflow.com/questions/3097589/getting-my-public-ip-via-api – timmyB Jul 03 '18 at 08:12

3 Answers3

3

You will probably not be able to grab your public IP Address without making a request to an external server, as your device is inside a LAN, it doesn't care about the public IP address of the router to Internet because it doesn't need it !

I would suggest you to use web-services like http://checkip.amazonaws.com/ to meet your needs.

    URL getIP = new URL("http://checkip.amazonaws.com/");
    BufferedReader getIPReader = new BufferedReader(new InputStreamReader(getIP.openStream()));

    System.out.println(getIPReader.readLine()); // prints the IP
Dampen59
  • 513
  • 2
  • 10
0

Old question, but after not finding any reasonable answer anywhere else...

Getting your public IP only using local resources

This method tries to be as generic as possible and will work even if you are at multiple hops before the NAT host. This example It's in bash and linux, but the method can be used on any OS that has some sort of traceroute and ping utilities.

#!/bin/bash
# Get the first non-local IP, that will be our ISP gateway
EXTERNAL_GW=`LANG=c traceroute  8.8.8.8 -n |grep -v -E '(192\.168|10\.|172\.1[6789]\.|172\.2[0-9]\.|172\.3[01]\.|8\.8\.8\.8)'|sed -n '1 s/ *[^ ]\+ \+\([^ ]*\) .*/\1/p'`
echo "External gateway: $EXTERNAL_GW"
echo -n "External IP     : "
#Ping the gateway with the record source option. Also get the first non-local and not our EXTERNAL_GW
LANG=c ping -4 -R $EXTERNAL_GW -c 1|sed -n '/^RR:/,${p;/^$/q}'|grep -v -E '(192\.168|10\.|172\.1[6789]\.|172\.2[0-9]\.|172\.3[01]\.|'$EXTERNAL_GW')'|head -n 1|cut -f 2

It depends only on traceroute and ping (Appart of the usual sed/grep/head/cut...)

  • First, uses traceroute to get the first host on a non-internal range network. That will be the first public gateway after your NAT host.

  • Then we ping that gateway with the option to record the sources, also extracting all the local network ranges.

  • The resulting IP is the one your NAT host is using on the public network.

Can be argued that it really uses an external resource. (In this case, 8.8.8.8) but any ICMP returning public host will do and there will always be some around.

FrameGrace
  • 585
  • 4
  • 4
0

Using Open.NAT:

var discoverer = new NatDiscoverer();
var device = await discoverer.DiscoverDeviceAsync();
var ip = await device.GetExternalIPAsync();

This only works for IPv4 though

blenderfreaky
  • 738
  • 7
  • 26