在终端里获取外网 IP 的方法

你们都知道 ifconfig 能够得到本机的网络设备信息,包括本地的 IP 地址。可是,局域网用户只能得到局域网分配的 IP,那么如何得到外网 IP 呢?html

这里要告诉少年们一个 IP 检测网站,只须要在命令行里输入以下命令便可:json

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

.bash_profile 配置文件里添加:bash

alias myip='curl ifconfig.me'

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

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

全部命令

$ curl ifconfig.me 114.114.114.114
$ curl ifconfig.me/ip 114.114.114.114
$ curl ifconfig.me/host
$ curl ifconfig.me/ua Mozilla/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/port 2832
$ curl ifconfig.me/lang zh-CN,zh;q=0.8
$ curl ifconfig.me/keepalive
$ curl ifconfig.me/connection keep-alive
$ curl ifconfig.me/encoding gzip,deflate,sdch
$ curl ifconfig.me/mime text/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 格式返回所有结果
相关文章
相关标签/搜索