TL;DR - 2015 年最快的方法

TL;DR - 2015 年最快的方法

我需要从 shell 脚本中找到我的外部 IP 地址。目前我使用这个功能:

myip () {
    lwp-request -o text checkip.dyndns.org | awk '{ print $NF }'
}

但它依赖于perl-libwwwperl-html-format、 和perl-html-tree正在安装。

我还可以通过哪些方式获取外部 IP?

答案1

我建议直接从 DNS 服务器获取它。

下面的大多数其他答案都涉及通过 HTTP 到远程服务器。其中一些需要解析输出,或者依赖 User-Agent 标头使服务器以纯文本方式响应。这些更改非常频繁(删除、更改名称、发布广告、可能更改输出格式等)。

  1. DNS 响应协议已标准化(格式将保持兼容)。
  2. 从历史上看,DNS 服务(Akamai、Google Public DNS、OpenDNS 等)往往比当今热门的任何新潮 Whatismyip dot-com HTTP 服务生存时间更长、更稳定、更具可扩展性,并且通常更受关注。
  3. 这种方法本质上更快(仅仅几毫秒!)。

使用dig使用 OpenDNS 解析器:

$ dig @resolver4.opendns.com myip.opendns.com +short

也许可以在您的名称中添加别名,bashrc这样很容易记住

# https://unix.stackexchange.com/a/81699/37512
alias wanip='dig @resolver4.opendns.com myip.opendns.com +short' 
alias wanip4='dig @resolver4.opendns.com myip.opendns.com +short -4'
alias wanip6='dig @resolver1.ipv6-sandbox.opendns.com AAAA myip.opendns.com +short -6'

使用普通 IP 地址进行响应:

$ wanip # wanip4, or wanip6
80.100.192.168 # or, 2606:4700:4700::1111

句法

(缩写自https://ss64.com/bash/dig.html:

usage:  dig [@global-dnsserver] [q-type] <hostname> <d-opt> [q-opt]

    q-type   one of (A, ANY, AAAA, TXT, MX, ...). Default: A.

    d-opt    ...
             +[no]short          (Display nothing except short form of answer)
             ...

    q-opt    one of:
             -4                  (use IPv4 query transport only)
             -6                  (use IPv6 query transport only)
             ...

查询ANY类型返回 AAAA 或 A 记录。要特别选择 IPv4 或 IPv6 连接,请相应地使用-4-6选项。

要要求响应是 IPv4 地址,请将 ANY 替换为A;对于 IPv6,将其替换为AAAA.请注意,它只能返回用于连接的地址。例如,通过 IPv6 连接时,它无法返回 A 地址。

替代服务器

许多 DNS 提供商都提供此服务,包括开放DNS,阿卡迈, 和谷歌公共 DNS:

# OpenDNS (since 2009)
$ dig @resolver3.opendns.com myip.opendns.com +short
$ dig @resolver4.opendns.com myip.opendns.com +short
80.100.192.168

# OpenDNS IPv6
$ dig @resolver1.ipv6-sandbox.opendns.com AAAA myip.opendns.com +short -6
2606:4700:4700::1111

# Akamai (since 2009)
$ dig @ns1-1.akamaitech.net ANY whoami.akamai.net +short
80.100.192.168

# Akamai approximate
# NOTE: This returns only an approximate IP from your block,
# but has the benefit of working with private DNS proxies.
$ dig +short TXT whoami.ds.akahelp.net
"ip" "80.100.192.160"

# Google (since 2010)
# Supports IPv6 + IPv4, use -4 or -6 to force one.
$ dig @ns1.google.com TXT o-o.myaddr.l.google.com +short
"80.100.192.168"

专门请求 IPv4 地址的别名示例:

# https://unix.stackexchange.com/a/81699/37512
alias wanip4='dig @resolver4.opendns.com myip.opendns.com +short -4'

$ wanip4
80.100.192.168

对于您的 IPv6 地址:

# https://unix.stackexchange.com/a/81699/37512
alias wanip6='dig @ns1.google.com TXT o-o.myaddr.l.google.com +short -6'

$ wanip6
"2606:4700:4700::1111"

故障排除

如果该命令由于某种原因不起作用,则可能存在网络问题。首先尝试上述替代方案之一。

如果您怀疑存在其他问题(与上游提供商、命令行工具或其他工具有关),请运行该命令,而不选择+short显示 DNS 查询的详细信息。例如:

$ dig @resolver4.opendns.com myip.opendns.com

;; Got answer: ->>HEADER<<- opcode: QUERY, status: NOERROR

;; QUESTION SECTION:
;myip.opendns.com.      IN  A

;; ANSWER SECTION:
myip.opendns.com.   0   IN  A   80.100.192.168

;; Query time: 4 msec

答案2

注意:这大约是外部的IP 地址(当您连接到 Internet 上的服务器时看到的地址)- 如果您愿意内部的IP 地址(您自己的计算机用于连接的 IP 地址,可能不同)请参阅这个答案

TL;DR - 2015 年最快的方法

使用 DNS 最快的方法:

dig +short myip.opendns.com @resolver4.opendns.com

或使用外部IP:

externalip dns

使用 HTTP 最快:

curl -s http://whatismyip.akamai.com/

或使用externalip:

externalip http

使用具有有效证书的 HTTPS 最快:

curl -s https://4.ifcfg.me/

或使用externalip:

externalip https

使用远程登录:

使用nc命令:

nc 4.ifcfg.me 23 | grep IPv4 | cut -d' ' -f4

或使用externalip:

externalip telnet

使用telnet命令:

telnet 4.ifcfg.me 2>&1 | grep IPv4 | cut -d' ' -f4

使用FTP:

echo close | ftp 4.ifcfg.me | awk '{print $4; exit}'

或使用externalip:

externalip ftp

以上所有内容都可以使用我的来运行外部IP脚本为:

externalip dns
externalip http
externalip https
externalip telnet
externalip ftp

现在说来话长……

提供外部 IP 的不同服务器有很多选项,特别是通过此处或其他地方发布的 HTTP。

我做了一个基准测试,看看它们中是否有一个比其他的更好,我对结果感到惊讶。例如,最广泛推荐的 ifconfig.me 之一对我来说几乎总是最慢的,有时需要很多秒才能响应。许多无法通过 HTTPS 运行,或者可以运行但证书无效。有些响应时间非常不一致。

基准测试

HTTP 和 HTTPS

这是我使用的 externalip-benchmark 脚本的来源:

您可以自己运行一下,看看这里提到的哪些服务值得使用:

wget https://raw.githubusercontent.com/rsp/scripts/master/externalip-benchmark
chmod a+x externalip-benchmark
./externalip-benchmark

我于 2015 年 4 月 3 日从华沙得到的结果 - 地址已更改以保护无辜者:

最佳 http 响应时间:

0.086s http://ip.tyk.nu/ - answer='172.31.133.7'
0.089s http://whatismyip.akamai.com/ - answer='172.31.133.7'
0.091s http://tnx.nl/ip - answer='172.31.133.7'
0.117s http://ifcfg.me/ - answer='172.31.133.7'
0.156s http://l2.io/ip - answer='172.31.133.7'
0.317s http://ip.appspot.com/ - answer='172.31.133.7'
0.336s http://ident.me/ - answer='172.31.133.7'
0.338s http://ipof.in/txt - answer='172.31.133.7'
0.347s http://icanhazip.com/ - answer='172.31.133.7'
0.496s http://curlmyip.com/ - answer='172.31.133.7'
0.527s http://wgetip.com/ - answer='172.31.133.7'
0.548s http://curlmyip.com/ - answer='172.31.133.7'
0.665s http://bot.whatismyipaddress.com/ - answer='172.31.133.7'
0.665s http://eth0.me/ - answer='172.31.133.7'
1.041s http://ifconfig.me/ - answer='172.31.133.7'
1.049s http://corz.org/ip - answer='172.31.133.7'
1.598s http://ipecho.net/plain - answer='172.31.133.7'

最佳 https 响应时间:

0.028s https://curlmyip.com/ - answer=''
0.028s https://curlmyip.com/ - answer=''
0.029s https://l2.io/ip - answer=''
0.029s https://tnx.nl/ip - answer=''
0.072s https://whatismyip.akamai.com/ - answer=''
0.113s https://ipecho.net/plain - answer=''
0.117s https://ident.me/ - answer=''
0.207s https://ip.tyk.nu/ - answer='172.31.133.7'
0.214s https://ipof.in/txt - answer='172.31.133.7'
0.259s https://ifcfg.me/ - answer='172.31.133.7'
0.289s https://corz.org/ip - answer=''
0.436s https://ip.appspot.com/ - answer='172.31.133.7'
0.448s https://bot.whatismyipaddress.com/ - answer=''
0.454s https://eth0.me/ - answer=''
0.673s https://icanhazip.com/ - answer='172.31.133.7'
5.255s https://ifconfig.me/ - answer=''
10.000s https://wgetip.com/ - answer=''

(注意:有一些内容为空的快速响应 - 这些是无效的。)

最佳平均 ping 时间:

10.210 //whatismyip.akamai.com/
36.820 //tnx.nl/ip
37.169 //ip.tyk.nu/
39.412 //ipof.in/txt
40.967 //ident.me/
41.257 //ipecho.net/plain
43.918 //ifcfg.me/
45.720 //l2.io/ip
64.749 //ip.appspot.com/
123.412 //corz.org/ip
134.245 //wgetip.com/
157.997 //icanhazip.com/
161.613 //curlmyip.com/
162.100 //curlmyip.com/
268.734 //ifconfig.me/
999999 //bot.whatismyipaddress.com/
999999 //eth0.me/

以下是我于 2015 年 4 月 3 日从阿姆斯特丹得到的结果:

最佳 http 响应时间:

0.021s http://ipecho.net/plain - answer='172.31.13.37'
0.027s http://tnx.nl/ip - answer='172.31.13.37'
0.035s http://whatismyip.akamai.com/ - answer='172.31.13.37'
0.039s http://ifcfg.me/ - answer='172.31.13.37'
0.045s http://l2.io/ip - answer='172.31.13.37'
0.142s http://ident.me/ - answer='172.31.13.37'
0.144s http://ipof.in/txt - answer='172.31.13.37'
0.150s http://ip.appspot.com/ - answer='172.31.13.37'
0.150s http://ip.tyk.nu/ - answer='172.31.13.37'
0.170s http://icanhazip.com/ - answer='172.31.13.37'
0.190s http://eth0.me/ - answer='172.31.13.37'
0.191s http://wgetip.com/ - answer='172.31.13.37'
0.301s http://curlmyip.com/ - answer='172.31.13.37'
0.330s http://bot.whatismyipaddress.com/ - answer='172.31.13.37'
0.343s http://curlmyip.com/ - answer='172.31.13.37'
0.485s http://corz.org/ip - answer='172.31.13.37'
3.549s http://ifconfig.me/ - answer='172.31.13.37'

最佳 https 响应时间:

0.004s https://curlmyip.com/ - answer=''
0.012s https://curlmyip.com/ - answer=''
0.012s https://tnx.nl/ip - answer=''
0.016s https://ipecho.net/plain - answer=''
0.071s https://whatismyip.akamai.com/ - answer=''
0.096s https://ifcfg.me/ - answer='172.31.13.37'
0.097s https://ident.me/ - answer=''
0.187s https://corz.org/ip - answer=''
0.187s https://ip.appspot.com/ - answer='172.31.13.37'
0.189s https://ip.tyk.nu/ - answer='172.31.13.37'
0.195s https://eth0.me/ - answer=''
0.253s https://l2.io/ip - answer=''
0.300s https://ipof.in/txt - answer='172.31.13.37'
0.324s https://bot.whatismyipaddress.com/ - answer=''
0.512s https://icanhazip.com/ - answer='172.31.13.37'
1.272s https://ifconfig.me/ - answer=''
10.002s https://wgetip.com/ - answer=''

最佳平均 ping 时间:

1.020 //ipecho.net/plain
1.087 //whatismyip.akamai.com/
5.011 //ip.appspot.com/
6.942 //ident.me/
7.017 //ipof.in/txt
8.209 //tnx.nl/ip
11.343 //ip.tyk.nu/
12.647 //ifcfg.me/
13.828 //l2.io/ip
81.642 //icanhazip.com/
85.447 //wgetip.com/
91.473 //corz.org/ip
102.569 //curlmyip.com/
102.627 //curlmyip.com/
247.052 //ifconfig.me/
999999 //bot.whatismyipaddress.com/
999999 //eth0.me/

(999999 ping 意味着 100% 丢包。)

域名系统

为了进行比较,这里列出了其他方法所花费的时间 - 于 2015 年 6 月 16 日在华沙和阿姆斯特丹进行了测试。

使用:

time dig +short myip.opendns.com @resolver1.opendns.com

通常需要(实际挂钟时间)大约:

  • 距离华沙 0.035 秒
  • 距阿姆斯特丹 0.015 秒

其实有可以这样使用的解析器:

  • 解析器1.opendns.com
  • 解析器2.opendns.com
  • 解析器3.opendns.com
  • 解析器4.opendns.com

他们在华沙和阿姆斯特丹的响应时间都相同,但在其他地方可能并非如此。

使用 208.67.222.222 -resolver1.opendns.com 的 IP 而不是其域名更快:

  • 距离华沙 0.023 秒
  • 距阿姆斯特丹 0.009 秒

但如果 IP 发生变化,将来可能无法工作(尽管对于知名的 DNS 解析器来说这可能不太可能 - 也许我应该在我的 IP 中使用该 IP)外部IP脚本 - 请评论)。

远程登录

nc使用或telnet命令(见上文)进行 Telnet通常需要:

  • 距华沙 0.103 秒
  • 距阿姆斯特丹 0.035 秒

nc(和telnet命令之间没有明显区别。)

文件传输协议

  • 距离华沙 0.104 秒
  • 距阿姆斯特丹 0.036 秒

域名

当使用 IP 地址而不是给定服务的域名时,所有方法都会更快(尤其是第一次运行时)(HTTP 除外,它可以使用基于主机的虚拟服务器并且不能使用裸 IP -未测试),但当服务更改 IP 地址时将停止工作,因此它可能会更快,但不太适合未来。

评论

如果您在您的位置看到一些有趣的结果,或者您认为应该推荐其他一些主机而不是我选择的主机,请发表评论。如果有什么重要的服务缺失,请评论或发布问题在 GitHub 上。我希望通过当前选择的性能最佳的服务来更新这篇文章。

答案3

 curl -s http://whatismijnip.nl |cut -d " " -f 5

将网站替换为荷兰工作网站。

答案4

亚马逊AWS

curl https://checkip.amazonaws.com

示例输出:

123.123.123.123

也适用于浏览器:http://checkip.amazonaws.com

我喜欢它因为:

  • 它只返回回复正文中的明文 IP,没有其他内容
  • 它来自一家知名提供商,不太可能很快下线

相关内容