天天看點

如何在 Linux 終端中知道你的公有 IP

公有位址由 internic 配置設定并由基于類的網絡 id 或基于 cidr 的位址塊構成(被稱為 cidr

塊),并保證了在全球網際網路中的唯一性。當公有位址被配置設定時,其路由将會被記錄到網際網路中的路由器中,這樣通路公有位址的流量就能順利到達。通路目标公有

位址的流量可經由網際網路抵達。比如,當一個 cidr 塊被以網絡 id 和子網路遮罩的形式配置設定給一個組織時,對應的 [網絡 id,子網路遮罩]

也會同時作為路由儲存在網際網路中的路由器中。目标是 cidr 塊中的位址的 ip 封包會被導向對應的位置。

如何在 Linux 終端中知道你的公有 IP

在本文中我将會介紹在幾種在 linux 終端中檢視你的公有 ip 位址的方法。這對普通使用者來說并無意義,但 linux

伺服器(無gui或者作為隻能使用基本工具的使用者登入時)會很有用。無論如何,從 linux 終端中擷取公有 ip

在各種方面都很意義,說不定某一天就能用得着。

以下是我們主要使用的兩個指令,curl 和 wget。你可以換着用。

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 json格式輸出:

curl ipinfo.io/json

curl ifconfig.me/all.json

curl www.trackip.net/ip?json (有點醜陋)

curl xml格式輸出:

curl ifconfig.me/all.xml

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 和 dig 指令。

host -t a dartsclink.com | sed 's/.*has address //'

dig +short myip.opendns.com @resolver1.opendns.com

bash 腳本示例:

#!/bin/bash

public_ip=`wget http://ipecho.net/plain -o - -q ; echo`

echo $public_ip

簡單易用。

我實際上是在寫一個用于記錄每日我的路由器中所有 ip 變化并儲存到一個檔案的腳本。我在搜尋過程中找到了這些很好用的指令。希望某天它能幫到其他人。

來源:51cto

繼續閱讀