Centos7查看一下对外公网ip

avatar 2020年5月22日21:16:10 评论 2,193 次浏览

我们知道查看linux的内网ip使用ifconfig就可以查看到,服务器本身的内网ip,但是我们访问外网的ip确不知道的,因为从服务器出去经过路由器交换机或者dns,dhcp等服务,最终的出口ip是不知道的,所以,如果我们的服务需要做一些支付相关的业务,就需要把我们的外网IP提供给对方,但是业务场景网络比较复杂,找一个ip也不容易。下面就介绍几种查看出口外网ip的几种方法:

使用curl查看

curl是一个get请求命令,可以通过请求获取出口公网的IP,看下面的例子:

[root@www.wulaoer.org ~]# curl ifconfig.me
117.136.38.180[root@www.wulaoer.org ~]#
[root@www.wulaoer.org ~]# curl ifconfig.me/all
ip_addr: 117.136.38.180
remote_host: unavailable
user_agent: curl/7.29.0
port: 49176
language: 
referer: 
connection: 
keep_alive: 
method: GET
encoding: 
mime: */*
charset: 
via: 1.1 google
forwarded: 117.136.38.180, 216.239.38.21[root@www.wulaoer.org ~]#

这里使用了ifconfig.me和ifconfig.me/all两种方法,ifconfig.me/all返回的更详细一点。

通过请求url

请求url和上面的curl方法是一样的,只是把请求的过程中返回的一样,看下面的例子:

[root@www.wulaoer.org ~]# curl icanhazip.com
117.136.38.180

[root@CentOS4 ~]# curl ipinfo.io
{
  "ip": "117.136.38.180",
  "city": "Beijing",
  "region": "Beijing",
  "country": "CN",
  "loc": "39.9075,116.3972",
  "org": "AS56048 China Mobile Communicaitons Corporation",
  "timezone": "Asia/Shanghai",
  "readme": "https://ipinfo.io/missingauth"
}[root@CentOS4 ~]# 

[root@CentOS4 ~]# curl ipecho.net/plain
117.136.38.180[root@CentOS4 ~]# 

[root@CentOS4 ~]# curl www.trackip.net/i
 <!DOCTYPE html>
<html><head><title>2409:8900:1810:b154:d512:77ed:a282:3267</title>
<meta charset="utf-8" /><script src="/cdn-cgi/apps/head/3mylQIuozVhfFiJP1iGhloiI8eY.js"></script><link rel="external nofollow" target="_blank" href="https://www.wulaoer.org/wp-content/themes/begin/go.php?url=aHR0cHM6Ly9mb250cy5nb29nbGVhcGlzLmNvbS9jc3M/ZmFtaWx5PVZhcmVsYStSb3VuZA==" rel="stylesheet"><style>
    body { font-family: 'Varela Round', sans-serif; font-size: 48px; }
    div { height: 100px; text-align: center; } small { font-size: 12px; } #flag { font-size: 150%; }
    </style></head><body><div>
<span id="flag">/span>
<br>2409:8900:1810:b154:d512:77ed:a282:3267<br>
<small><a href="/ip">IP</a> &middot; <a href="/ip?json">json</a></small></div></body></html>[root@CentOS4 ~]# 


[root@CentOS4 ~]# curl ifconfig.me/all.xml
<info>
    <ip_addr>117.136.38.180</ip_addr>
    <remote_host>unavailable</remote_host>
    <user_agent>curl/7.29.0</user_agent>
    <port>41802</port>
    <language></language>
    <referer></referer>
    <connection></connection>
    <keep_alive></keep_alive>
    <method>GET</method>
    <encoding></encoding>
    <mime>*/*</mime>
    <charset></charset>
    <via>1.1 google</via>
    <forwarded>117.136.38.180, 216.239.38.21</forwarded>
</info>[root@CentOS4 ~]#

以上几种方法返回的结果不太一样,有的是返回xml,有的是返回的json,也有的是直接返回的是公网IP,所以可以根据自己的需求,使用不同的返回方法。

其它方法

还可以安装第三方的工具获取,我这里没有安装可以自己试一下。

host -t a dartsclink.com | sed 's/.*has address //'
dig +short myip.opendns.com @resolver1.opendns.com

可以直接使用上面的命令请求,先就介绍到这了,以后有什么其它的方法在继续更新

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: