Linux服務(wù)器如何查看自己的公網(wǎng)出口IP地址
windows服務(wù)器我們都知道如何獲取公網(wǎng)IP,直接ip.cn或者ip138.com即可查到。linux就略有不同,我們可以通過(guò)如下方法獲取linux系統(tǒng)服務(wù)器的公網(wǎng)IP出口??梢愿鶕?jù)自己不同需求方式進(jìn)行調(diào)用。
網(wǎng)上搜索了一篇文章,方法如下:
curl 純文本格式輸出:
curl icanhazip.com
curl ifconfig.me
curl curlmyip.com
curl ip.appspot.com
curl ipinfo.io/ip
curl ipecho.net/plain
curl www.tjdsmy.cn/i
curl XML格式輸出:
curl ifconfig.me/all.xml
curl JSON格式輸出:
curl ipinfo.io/json
curl ifconfig.me/all.json
curl www.tjdsmy.cn/ip?json (有點(diǎn)丑陋)
curl 得到所有IP細(xì)節(jié) (挖掘機(jī)):
curl ifconfig.me/all
使用 DYDNS (當(dāng)你使用 DYDNS 服務(wù)時(shí)有用)
curl -s 'http://www.tjdsmy.cn' | sed 's/.*Current IP Address: ([0-9.]*).*/1/g'
curl -s http://www.tjdsmy.cn/ | grep -o "[[:digit:].]+"
使用 Wget 代替 Curl
wget http://www.tjdsmy.cn/plain -O - -q ; echo
wget http://www.tjdsmy.cn/ip -O - -q ; echo
使用 host 和 dig 命令
host -t a landui.com | sed 's/.*has address //'
dig +short myip.landui.com @resolver1.opendns.com<pr》
bash 腳本示例:
#!/bin/bash
PUBLIC_IP=`wget http://www.tjdsmy.cn/plain -O - -q ; echo`
echo $PUBLIC_IP
這里獲取到公網(wǎng)IP以后,自己還上傳到了一個(gè)線上服務(wù)器,畢竟公司這里申請(qǐng)的是公網(wǎng)IP,每隔幾天都要更換一下。家里或其他地方如果要連接公司內(nèi)部的虛擬機(jī)或管理路由器,就要獲取準(zhǔn)確公網(wǎng)IP才可以。
這里就簡(jiǎn)單寫了一個(gè)腳本定期執(zhí)行,獲取到公網(wǎng)IP以后上傳到一臺(tái)線上的阿里服務(wù)器,這樣每次可以直接通過(guò)登錄阿里服務(wù)器獲取到公司的公網(wǎng)出口IP了,公司內(nèi)部測(cè)試機(jī)器linux上部署的腳本如下:
#!/bin/bash
curl landui.com/ip > public_ip.txt
rsync -avz /home/yunwei/public_ip.txt 1.2.3.4:/home/yunwei/
以后每次到線上1.2.3.4的服務(wù)器cat /home/yunwei/public_ip.txt 就知道公司IP了。
會(huì)員登錄
賬號(hào)登錄還沒(méi)有賬號(hào)?立即注冊(cè)