在bash中检测公共IP地址的方法

Jer*_*oen 26 linux bash shell ubuntu centos

作为安装脚本的一部分,我想对机器的公共 IP地址进行"有根据的猜测" .它应该尽可能健壮,适用于各种主机和平台.这是一个例子:

https://gist.github.com/4669037

此脚本使用3种方法,然后默认为值/etc/hostname.到目前为止我只在Ubuntu上测试了这个.这可能适用于其他发行版吗?还有其他方法可以添加吗?

Ken*_*ent 10

curl ifconfig.me 如果你没有卷曲,那将是最好的选择:

wget -qO- ifconfig.me/ip

  • 该网站很糟糕,比较 [ifconfig.me status](http://wheresitup.com/results/5497718d8c3dcfc65cc8c06d) 与 [ipinfo.io status](http://wheresitup.com/results/549771c48c3dcfdd5cc8c06a) (2认同)

Eri*_*nda 6

如果在 AWS EC2 上,您可以使用:

curl checkip.amazonaws.com
Run Code Online (Sandbox Code Playgroud)

注意:可以从任何 Web 客户端使用此服务。


小智 6

我在这里找到了我个人喜欢的方法\xef\xbc\x9a

\n
    \n
  • dig +short myip.opendns.com @resolver1.opendns.com
  • \n
  • dig +short txt ch whoami.cloudflare @1.0.0.1
  • \n
  • dig +short txt o-o.myaddr.l.google.com @ns1.google.com\n
      \n
    • dig +short txt o-o.myaddr.l.google.com @ns1.google.com | awk -F'"' '{print $2}'
    • \n
    \n
  • \n
\n