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条回答
混吃等死
2楼-- · 2020-01-25 06:26

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:

exports.requestIP = (req, res) => {
    res.status(200).send(req.ip)
}

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.

查看更多
The star\"
3楼-- · 2020-01-25 06:31

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.

查看更多
男人必须洒脱
4楼-- · 2020-01-25 06:31

I like the ipify.org:

  • it's free to use (even programmatically and even heavy traffic)
  • response contains only the IP address without any garbage (no need for parsing)
  • you can also request response in JSON
  • works for both IPv4 and IPv6
  • it's hosted in cloud
  • it's open source
$ curl api.ipify.org
167.220.196.42

$ curl "api.ipify.org?format=json"
{"ip":"167.220.196.42"}
查看更多
冷血范
5楼-- · 2020-01-25 06:33

EDIT: curlmyip.com is no longer available. (thanks maxywb)

Original Post:

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

curl curlmyip.com

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.

查看更多
何必那么认真
6楼-- · 2020-01-25 06:33

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)

INTERFACE=`ip route get 8.8.8.8 | grep 8.8.8.8 | cut -d' ' -f5`
HOSTIP=`ifconfig $INTERFACE | grep "inet " | awk -F'[: ]+' '{ print $4 }'`

Or in python if you prefer:

import subprocess
public_ip = subprocess.check_output(["ifconfig `ip route get 8.8.8.8 | grep 8.8.8.8 | cut -d' ' -f5` | grep \'inet \' | awk -F'[: ]+' '{ print $4 }'"], shell=True)

It works like this:

  • determines the best interface to reach the google dns server
  • greps public IP from the ifconfig entry for that interface
查看更多
做个烂人
7楼-- · 2020-01-25 06:38

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)

查看更多
登录 后发表回答