最新消息:天气越来越冷,记得加一件厚衣裳

curl获取公网(外网)IP

Linux w3sun 2132浏览 0评论

IDC机房中的服务器如果没有开通外网权限无法访问外部资源,当然也就无法获取公网IP。需要查询出口公网IP的时候需要向运维申请在Gateway上开通外网权限,然后结合curl命令查询出口公网IP信息。

ifconfig.io获取公网IP

ifconfig.io是一个灵感来自于ifconfig.me的开源项目,纯粹是为速度而设计。单台服务器每秒钟可以支撑18000个请求,而只消耗50M内存。作者使用了gin框架,gin框架可以确保针对每个请求而不单独分配内存以防止不必要的内存分配。除了支持web访问外,ifconfig.io还支持curl获取相关信息。

web访问ifconfig.io

如果是通过浏览器访问ifconfig.io可以很直观的看到公网IP、端口、国家编码、MIME Type、User Agent等信息。

curl访问ifconfig.io

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[username@hostname ~]$ curl ifconfig.io/all
{
"country_code":"CN",
"encoding":"gzip",
"forwarded":"222.1.151.14",
"host":"222.1.151.14",
"ifconfig_hostname":"ifconfig.io",
"ip":"222.1.151.14",
"lang":"",
"method":"GET",
"mime":"*/*",
"port":17760,
"referer":"",
"ua":"curl/7.64.1"
}
[username@hostname ~]$ curl ifconfig.io/all { "country_code":"CN", "encoding":"gzip", "forwarded":"222.1.151.14", "host":"222.1.151.14", "ifconfig_hostname":"ifconfig.io", "ip":"222.1.151.14", "lang":"", "method":"GET", "mime":"*/*", "port":17760, "referer":"", "ua":"curl/7.64.1" }
[username@hostname ~]$ curl ifconfig.io/all
{
    "country_code":"CN",
    "encoding":"gzip",
    "forwarded":"222.1.151.14",
    "host":"222.1.151.14",
    "ifconfig_hostname":"ifconfig.io",
    "ip":"222.1.151.14",
    "lang":"",
    "method":"GET",
    "mime":"*/*",
    "port":17760,
    "referer":"",
    "ua":"curl/7.64.1"
}

除了json格式外ifconfig.io还支持xml和plain格式,如果只需要获取ip而不需要展示其他信息时直接执行curl ifconfig.io/ip即可。

通过其他方式获取公网IP

cip.cc

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[username@hostname ~]$ curl cip.cc
IP : 222.1.151.14
地址 : 中国 北京
运营商 : 联通
数据二 : 北京市 | 联通
数据三 : URL : http://www.cip.cc/222.1.151.14
[username@hostname ~]$ curl cip.cc IP : 222.1.151.14 地址 : 中国 北京 运营商 : 联通 数据二 : 北京市 | 联通 数据三 : URL : http://www.cip.cc/222.1.151.14
[username@hostname ~]$ curl cip.cc 
IP : 222.1.151.14 
地址 : 中国 北京 
运营商 : 联通 
数据二 : 北京市 | 联通 
数据三 : URL : http://www.cip.cc/222.1.151.14

ident.me

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[username@hostname ~]$ curl ident.me
222.1.151.14%
[username@hostname ~]$ curl ident.me 222.1.151.14%
[username@hostname ~]$ curl ident.me 
222.1.151.14%

ipinfo.io

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[username@hostname ~]$ curl ipinfo.io
{
"ip":"222.1.151.14",
"city":"Beijing",
"region":"Beijing",
"country":"CN",
"loc":"37.9075,143.3972",
"org":"AS4808 China Unicom Beijing Province Network",
"timezone":"Asia/Shanghai",
"readme":"https://ipinfo.io/missingauth"
}
[username@hostname ~]$ curl ipinfo.io { "ip":"222.1.151.14", "city":"Beijing", "region":"Beijing", "country":"CN", "loc":"37.9075,143.3972", "org":"AS4808 China Unicom Beijing Province Network", "timezone":"Asia/Shanghai", "readme":"https://ipinfo.io/missingauth" }
[username@hostname ~]$ curl ipinfo.io 
{
  "ip":"222.1.151.14",
  "city":"Beijing",
  "region":"Beijing",
  "country":"CN",
  "loc":"37.9075,143.3972",
  "org":"AS4808 China Unicom Beijing Province Network",
  "timezone":"Asia/Shanghai",
  "readme":"https://ipinfo.io/missingauth"
}

ifconfig.co

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[username@hostname ~]$ curl ifconfig.co
222.1.151.14%
[username@hostname ~]$ curl ifconfig.co 222.1.151.14%
[username@hostname ~]$ curl ifconfig.co
222.1.151.14%

ifconfig.me

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[username@hostname ~]$ curl ifconfig.me
222.1.151.14%
[username@hostname ~]$ curl ifconfig.me 222.1.151.14%
[username@hostname ~]$ curl ifconfig.me
222.1.151.14%

inet-ip.info

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[username@hostname ~]$ curl inet-ip.info
222.1.151.14%
[username@hostname ~]$ curl inet-ip.info 222.1.151.14%
[username@hostname ~]$ curl inet-ip.info
222.1.151.14%

myip.ipip.net

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[username@hostname ~]$ curl myip.ipip.net
当前 IP:222.1.151.14 来自于:中国 北京 北京 联通
[username@hostname ~]$ curl myip.ipip.net 当前 IP:222.1.151.14 来自于:中国 北京 北京 联通
[username@hostname ~]$ curl myip.ipip.net
当前 IP:222.1.151.14 来自于:中国 北京 北京 联通

icanhazip.com

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[username@hostname ~]$ curl icanhazip.com
222.1.151.14
[username@hostname ~]$ curl icanhazip.com 222.1.151.14
[username@hostname ~]$ curl icanhazip.com
222.1.151.14

ipecho.net/plain

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[username@hostname ~]$ curl ipecho.net/plain
222.1.151.14%
[username@hostname ~]$ curl ipecho.net/plain 222.1.151.14%
[username@hostname ~]$ curl ipecho.net/plain
222.1.151.14%

members.3322.org/dyndns/getip

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[username@hostname ~]$ curl members.3322.org/dyndns/getip
222.1.151.14
[username@hostname ~]$ curl members.3322.org/dyndns/getip 222.1.151.14
[username@hostname ~]$ curl members.3322.org/dyndns/getip
222.1.151.14

参考资料

https://github.com/georgyo/ifconfig.io

转载请注明:雪后西塘 » curl获取公网(外网)IP

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址