歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 如何在 Linux 終端中知道你的公有 IP

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

日期:2017/2/28 13:58:35   编辑:Linux教程

公有地址由 InterNIC 分配並由基於類的網絡 ID 或基於 CIDR 的地址塊構成(被稱為 CIDR 塊),並保證了在全球互聯網中的唯一性。當公有地址被分配時,其路由將會被記錄到互聯網中的路由器中,這樣訪問公有地址的流量就能順利到達。訪問目標公有地址的流量可經由互聯網抵達。比如,當一個 CIDR 塊被以網絡 ID 和子網掩碼的形式分配給一個組織時,對應的 [網絡 ID,子網掩碼] 也會同時作為路由儲存在互聯網中的路由器中。目標是 CIDR 塊中的地址的 IP 封包會被導向對應的位置。

在本文中我將會介紹在幾種在 Linux 終端中查看你的公有 IP 地址的方法。這對普通用戶來說並無意義,但 Linux 服務器(無GUI或者作為只能使用基本工具的用戶登錄時)會很有用。無論如何,從 Linux 終端中獲取公有 IP 在各種方面都很意義,說不定某一天就能用得著。

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

Curl 純文本格式輸出:

  1. curl icanhazip.com
  2. curl ifconfig.me
  3. curl curlmyip.com
  4. curl ip.appspot.com
  5. curl ipinfo.io/ip
  6. curl ipecho.net/plain
  7. curl www.trackip.net/i

curl JSON格式輸出:

  1. curl ipinfo.io/json
  2. curl ifconfig.me/all.json
  3. curl www.trackip.net/ip?json (有點丑陋)

curl XML格式輸出:

  1. curl ifconfig.me/all.xml

curl 得到所有IP細節 (挖掘機)

  1. curl ifconfig.me/all

使用 DYDNS (當你使用 DYDNS 服務時有用)

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

使用 Wget 代替 Curl

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

使用 host 和 dig 命令

如果有的話,你也可以直接使用 host 和 dig 命令。

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

bash 腳本示例:

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

簡單易用。

我實際上是在寫一個用於記錄每日我的路由器中所有 IP 變化並保存到一個文件的腳本。我在搜索過程中找到了這些很好用的命令。希望某天它能幫到其他人。


via: How to get Public IP from Linux Terminal?

譯者:KevinSJ 校對:wxy

本文由 LCTT 原創翻譯,Linux中國 榮譽推出

Copyright © Linux教程網 All Rights Reserved