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

蜗牛vpsvps教程2021-03-169380

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了。


评论

有免费节点资源,我们会通知你!加入纸飞机订阅群

×
天气预报查看日历分享网页手机扫码留言评论电报频道链接