Discovering public IP programmatically

my ip
javascript get public ip
android get public ip address programmatically
python get public ip address
python get your public ip address
ip api
http ip port info json

I'm behind a router, I need a simple command to discover my public ip (instead of googling what's my ip and clicking one the results)

Are there any standard protocols for this? I've heard about STUN but I don't know how can I use it?

P.S. I'm planning on writing a short python script to do it

EDIT: is no longer available. (thanks maxywb)

Original Post:

As of writing this post, works. From the command line:


It's a third-party website, which may or may not be available a couple years down the road. But for the time being, it seems pretty simple and to the point.

ipify, ipify API is a simple public IP address API, easy enough to integrate into any application in C#. var httpClient = new HttpClient(); var ip = await httpClient. In a typical home network, a router has a public IP address on the Internet. The computers, smartphones, game consoles, and other devices behind the router each have a unique private IP address on the home network. The router acts as an intermediary, forwarding traffic to the local IP addresses that request it.

This may be the easiest way. Parse the output of the following commands:

  1. run a traceroute to find a router that is less than 3 hops out from your machine.
  2. run ping with the option to record the source route and parse the output. The first IP address in the recorded route is your public one.

For example, I am on a Windows machine, but the same idea should work from unix too.

> tracert -d

Tracing route to []
over a maximum of 30 hops:

  1    <1 ms    <1 ms    <1 ms
  2     *        *        *     Request timed out.
  3     8 ms     8 ms     9 ms
  4     8 ms     8 ms     9 ms
  5    10 ms     9 ms     9 ms
  6    11 ms    10 ms    10 ms

The is a Comcast (my provider) router. We can ping that:

> ping -r 9 -n 1

Pinging with 32 bytes of data:

Reply from bytes=32 time=10ms TTL=253
    Route: ->

Voila! The is my public IP.

Discovering public IP programmatically, Question. I'm behind a router, I need a simple command to discover my public ip (​instead of googling what's my ip and clicking one the results). Are there any  Why ipify? Ever needed to get your public IP address programmatically? Maybe you're provisioning new cloud servers and need to know your IP -- maybe you're behind a corporate firewall and need to tunnel information -- whatever the reason: sometimes having a public IP address API is useful! You should use ipify because:

I have made a program that connects to it is is written in D an getting someone else to tell you what they see your ip as is probably the most reliable way:

    Get my IP address


void main()
          auto page = new HttpGet ("");
      catch(Exception ex)
          Stdout("An exception occurred");

Edit python code should be like:

from urllib import urlopen
print urlopen('').read()

Discovering public IP programmatically, Im behind a router I need a simple command to discover my public ip instead of googling whats my ip and clicking one the resultsAre ther If you are trying to find out what your public external IP address is, you typically go to a web site specifically for this purpose, such as or Finding your public IP from the command line is just as easy. Simply enter the following command into a command prompt window: nslookup

Targeting is rude. They plea not to do that on the page.

Only a system on the same level of NAT as your target will see the same IP. For instance, your application may be behind multiple layers of NAT (this happens more as you move away from the US, where the glut of IPs are).

STUN is indeed the best method. In general, you should be planning to run a (STUN) server somewhere that you application can ask: do not hard code other people's servers. You have to code to send some specific messages as described in rfc5389.

I suggest a good read of, and related links.

You may prefer to look at IPv6, and Teredo to make sure that you always have IPv6 access. (Microsoft Vista makes this very easy, I'm told)

What Is My IP? - Shows your real public IP address - IPv4,® is the industry leader in providing REAL IP address information. Interface) gives you access to most of our tools programmatically. In order  IP Address Lookup or IP Locator is a tool that allows you to see where your IP address is located. It can also be used to lookup other IPs and find their IP Location. A WhoIs Lookup is a tool that will give you pertinent information about the owner of the IP address.

Java program to find IP address of your computer, InetAddress.getLocalHost() is used to find private IP address used in LAN or any other local network. To find public IP, we use http://bot. If . you only have one public IP address and; you are directly connected to the internet (no NAT / proxy / VPN etc) then you can just parse the output from ifconfig for the IP addresses of the interfaces (the "inet addr:" part) to get the list of IP addresses of all your interfaces.

Locating IP addresses for an instance, In the Google Cloud Console, go to the VM instances page. If the VM instance has an external IP address, it appears under the External IP column. If a VM does​  Unfortunately, there is no way to get programmatically the public/external IP of a computer without communicating with another computer over the internet. Thus, the GetMyPublicIP function sends a request to and returns the response text.

Use NAT for Public Access to Servers with Private IP Addresses on , In these configuration examples we use three different types of NAT to translate a public IP address to the private IP address of each server for both inbound and  In theory your router should be able to tell you the public IP address of the network, but the way of doing this will necessarily be inconsistent/non-straightforward, if even possible with some router devices. The easiest and still a very reliable method is to send a request to a web page that returns your IP address as the web server sees it.

Port forwarding, In computer networking, port forwarding or port mapping is an application of network address The NAT device's external interface is configured with a public IP address. is a network service by which an Internet gateway advertises its presence on a private network via the Simple Service Discovery Protocol (​SSDP). Find out what your public IPv4 and IPv6 address is revealing about you! My IP address information shows your location; city, region, country, ISP and location on a map. Many proxy servers, VPNs, and Tor exit nodes give themselves away.

  • yet another service: curl
  • +1: maybe not the easiest way, but it's neat to see a way that doesn't rely on some website.
  • But note that "Many hosts ignore or discard the RECORD_ROUTE option." See man ping.
  • This is the real answer. The other's all require some 3rd party website to stay alive.
  • This relies on ICMP messaging, which is often blocked. So this method is unreliable (and slow).
  • hehe I like ur name dude! (btw I love D)
  • The URL has been updated to; see for rules.
  • they also ask you to add a Mozilla user agent per the faq -- posted a verison that does so at… (wrapping with an except is a good idea if you're going to rely on this other code) --
  • It seems that whatsmyip don't mind automated lookups so long as you follow some rules: . If you break the rules than you can be banned.
  • the site ryeguy suggested is different .. whatis .. not whats ..
  • I see no such pleas on . What page are you speaking of?
  • It's working perfectly. If you suspect some web is not working, try some service like first.
  • nice! I didn't know about this site! usually google turns up a bunch of sites each of which is big and fat, probably with a note saying don't use automated tools! but this is awesome! thanks
  • It makes your program depend on an external site you do not control (same thing with STUN, of course, unless you run your own STUN server)...