大家都知道 ifconfig 可以获得本机的网络设备信息,包括本地的 IP 地址。但是,局域网用户只能获得局域网分配的 IP,那么如何获得外网 IP 呢?

这里要告诉少年们一个 IP 检测网站,只需要在命令行里输入如下命令即可:

$ curl ifconfig.me  # 返回 IP,如:114.114.114.114

在 .bash_profile 配置文件里添加:

alias myip='curl ifconfig.me'

然后就能用 myip 获得外网 IP 了。这是 ifconfig.me 网站提供的 API 服务。

除了返回 IP,他们还提供返回 http 链接的几乎所有参数,还能选择返回是 xml 和 json 格式。

所有命令

$ curl ifconfig.me114.114.114.114
$ curl ifconfig.me/ip114.114.114.114
$ curl ifconfig.me/host 
$ curl ifconfig.me/uaMozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36
$ curl ifconfig.me/port2832
$ curl ifconfig.me/langzh-CN,zh;q=0.8
$ curl ifconfig.me/keepalive 
$ curl ifconfig.me/connectionkeep-alive
$ curl ifconfig.me/encodinggzip,deflate,sdch
$ curl ifconfig.me/mimetext/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
$ curl ifconfig.me/charset 
$ curl ifconfig.me/via 
$ curl ifconfig.me/forwarded 
$ curl ifconfig.me/all返回全部结果
$ curl ifconfig.me/all.xml以 XML 格式返回全部结果
$ curl ifconfig.me/all.json以 JSON 格式返回全部结果