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
It's now quite cheap to host your own "serverless" API. All the major cloud providers have a service for this. For example, using Google Cloud Functions all it takes is:
This approach is probably more reliable than using the public services above and you can add a long random string to the function's name to keep it private.
Whenever I wanted to do this, I would just scrape whatismyip.org. When you go to the site, it gives you your plain text public IP. Plain and simple.
Just have your script access that site and read the IP.
I don't know if you were implying this in your post or not, but it isn't possible to get your public IP from your own computer. It has to come from an external source.
2013 edit: This site returns an image now instead of text, so it's useless for this purpose.
I like the ipify.org:
EDIT: curlmyip.com is no longer available. (thanks maxywb)
Original Post:
As of writing this post, curlmyip.com 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.
I'm sharing you my method of retrieving a server's public IP address without having to use external APIs (which is a security risk of course)
Or in python if you prefer:
It works like this:
ifconfig
entry for that interfaceTargeting www.whatsmyip.org 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. http://www.ietf.org/html.charters/behave-charter.html
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)