西数超哥博客
运维经验教程分享

Linux服务器如何查看自己的公网出口IP地址

windows服务器我们都知道如何获取公网IP,直接ip.cn或者ip138.com即可查到。linux就略有不同,我们可以通过如下方法获取linux系统服务器的公网IP出口。可以根据自己不同需求方式进行调用。
网上搜索了一篇文章,方法如下:


curl 纯文本格式输出:

curl icanhazip.com
curl ifconfig.me
curl curlmyip.com
curl ip.appspot.com
curl ipinfo.io/ip
curl ipecho.net/plain
curl www.trackip.net/i

curl XML格式输出:

curl ifconfig.me/all.xml

curl JSON格式输出:

curl ipinfo.io/json
curl ifconfig.me/all.json
curl www.trackip.net/ip?json (有点丑陋)

curl 得到所有IP细节 (挖掘机):

curl ifconfig.me/all

使用 DYDNS (当你使用 DYDNS 服务时有用)

curl -s 'http://checkip.dyndns.org' | sed 's/.*Current IP Address: \([0-9\.]*\).*/\1/g'
curl -s http://checkip.dyndns.org/ | grep -o "[[:digit:].]\+"

使用 Wget 代替 Curl

wget http://ipecho.net/plain -O - -q ; echo
wget http://observebox.com/ip -O - -q ; echo

使用 host 和 dig 命令

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

bash 脚本示例:

#!/bin/bash
PUBLIC_IP=`wget http://ipecho.net/plain -O - -q ; echo`
echo $PUBLIC_IP


这里获取到公网IP以后,自己还上传到了一个线上服务器,毕竟公司这里申请的是公网IP,每隔几天都要更换一下。家里或其他地方如果要连接公司内部的虚拟机或管理路由器,就要获取准确公网IP才可以。
这里就简单写了一个脚本定期执行,获取到公网IP以后上传到一台线上的阿里服务器,这样每次可以直接通过登录阿里服务器获取到公司的公网出口IP了,公司内部测试机器linux上部署的脚本如下:

#!/bin/bash
curl ipinfo.io/ip > public_ip.txt
rsync  -avz  /home/yunwei/public_ip.txt  1.2.3.4:/home/yunwei/

 以后每次到线上1.2.3.4的服务器cat  /home/yunwei/public_ip.txt 就知道公司IP了。

转载请注明:西数超哥博客www.ysidc.top» Linux服务器如何查看自己的公网出口IP地址

https://www.ysidc.top 西数超哥博客,数据库,西数超哥,虚拟主机,域名注册,域名,云服务器,云主机,云建站,ysidc.top

赞(0)
声明:本站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,若涉及侵权请及时告知,将会在第一时间删除。本站原创内容未经允许不得转载:西数超哥博客 » Linux服务器如何查看自己的公网出口IP地址