views:

1402

answers:

7

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

+9  A: 

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.

ryeguy
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
hasen j
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)...
bortzmeyer
Iwhatismyip.org seems completely broken: I get the answer 192.168.2.1 which is clearly not my public IP...
bortzmeyer
If you're developing an application and it somehow needs to know the local IP, then you're likely doing something wrong. Don't make dependencies on local IP addrs, as they can change at any time (i.e. DHCP). What's wrong with just using the NAT'd address?
slacy
STUN is the right way to do this. Scraping whatismyip.org makes your application dependent on that service it isn't playing nice.
Troy J. Farrell
+2  A: 

Your simplest way may be to ask some server on the outside of your network.

One thing to keep in mind is that different destinations may see a different address for you. The router may be multihomed. And really that's just where problems begin.

John Fricker
Ditto if your ISP is doing any proxying
Dana the Sane
Completely false: there is a standard protocol, STUN, RFC 5389.
bortzmeyer
Interesting - wasn't familiar with that RFC. Is it widely implemented yet?
John Fricker
Good question, but I am not aware of any survey. Anyway, you do not need every machine on the Internet to implement it. If you develop a network program, just put STUN code in *your* program and set up a few public STUN servers (or see if the existing ones can be used).
bortzmeyer
+12  A: 

I have made a program that connects to http://whatismyip.com/automation/n09230945.asp 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
*/


import tango.net.http.HttpGet;
import tango.io.Stdout;

void main()
{
      try
      {
          auto page = new HttpGet ("http://whatismyip.com/automation/n09230945.asp");
          Stdout(cast(char[])page.read);
      }
      catch(Exception ex)
      {
          Stdout("An exception occurred");
      }
}

Edit python code should be like:

from urllib import urlopen
print urlopen('http://whatismyip.com/automation/n09230945.asp').read()
Tim Matthews
hehe I like ur name dude! (btw I love D)
hasen j
+2  A: 

If the network has an UpNp server running on the gateway you are able to talk to the gateway and ask it for your outside IP address.

X-Istence
+9  A: 

Targeting 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)

mcr
It seems that whatsmyip don't mind automated lookups so long as you follow some rules: http://forum.whatismyip.com/f14/ . If you break the rules than you can be banned.
dangph
the site ryeguy suggested is different .. what*is* .. not what*s* .. mykp.org
hasen j
I see no such pleas on whatismyip.org . What page are you speaking of?
Brian
+8  A: 

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 www.yahoo.com

Tracing route to www-real.wa1.b.yahoo.com [69.147.76.15]
over a maximum of 30 hops:

  1    <1 ms    <1 ms    <1 ms  192.168.14.203
  2     *        *        *     Request timed out.
  3     8 ms     8 ms     9 ms  68.85.228.121
  4     8 ms     8 ms     9 ms  68.86.165.234
  5    10 ms     9 ms     9 ms  68.86.165.237
  6    11 ms    10 ms    10 ms  68.86.165.242

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

> ping -r 9 68.85.228.121 -n 1

Pinging 68.85.228.121 with 32 bytes of data:

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

Voila! The 66.176.38.51 is my public IP.

Carlos A. Ibarra
+1: maybe not the easiest way, but it's neat to see a way that doesn't rely on some website.
David Zaslavsky
+2. good answer indeed
Sujoy
+2  A: 

As mentioned by several people, STUN is indeed the proper solution.

bortzmeyer