Discovering public IP programmatically

2020-01-25 06:06发布

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

15条回答
ら.Afraid
2楼-- · 2020-01-25 06:21

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.

查看更多
Melony?
3楼-- · 2020-01-25 06:21

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

查看更多
疯言疯语
4楼-- · 2020-01-25 06:23

I have made a program that connects to http://automation.whatismyip.com/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://automation.whatismyip.com/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://automation.whatismyip.com/n09230945.asp').read()
查看更多
别忘想泡老子
5楼-- · 2020-01-25 06:24

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.

查看更多
萌系小妹纸
6楼-- · 2020-01-25 06:26

another cheeky way: if your router has got the option to update it's web IP on DynDNS, you can get your own IP with something like:

IP=`resolveip -s myalias.dyndns-home.com`
查看更多
Animai°情兽
7楼-- · 2020-01-25 06:26

curl api.infoip.io - full details

curl api.infoip.io/ip - just the ip address

curl api.infoip.io/country - just the country name

... and more of the same

you can view the docs at http://docs.ciokan.apiary.io/

查看更多
登录 后发表回答