取得本機的IP與連線網際網路的IP(NAT)
先熟悉以下幾個指令
sudo apt install wget html2text dnsutils
ip route get 8.8.8.8
ip r s default | cut -d " " -f 3
dig +short myip.opendns.com @resolver1.opendns.com
wget -U Mozilla/5.0 -qqO- \
'https://myip.com.tw' | html2text
當你的電腦在 NAT 主機的後面,你需要知道你使用的內部 private IP 與連線網際網路時所需的 external IP 時,這些指令很有用。
寫成 shell 程式
!/bin/bash
#author:kawsing
GW=$(ip r s default | cut -d " " -f3)
ext_IP=$(dig +short myip.opendns.com @resolver1.opendns.com)
IP=$(ip -o route get "8.8.8.8" 2>/dev/null | sed -e 's/^.* src \([^ ]*\) .*$/\1/')
echo "我的主機 IP 是 ${IP}"
echo "我的主機閘道 IP 是 ${GW}"
echo "我的WAN IP 是 ${ext_IP}"
Last updated
Was this helpful?