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

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

公有地址由 InterNIC 分配并由基于类的网络 ID 或基于 CIDR 的地址块构成(被称为 CIDR 块),并保证了在全球互联网中的唯一性。当公有地址被分配时,其路由将会被记录到互联网中的路由器中,这样访问公有地址的流量就能顺利到达。访问目标公有地址的流量可经由互联网抵达。比如,当一个 CIDR 块被以网络 ID 和子网掩码的形式分配给一个组织时,对应的 [网络 ID,子网掩码] 也会同时作为路由储存在互联网中的路由器中。目标是 CIDR 块中的地址的 IP 封包会被导向对应的位置。

在本文中我将会介绍在几种在 Linux 终端中查看你的公有 IP 地址的方法。这对普通用户来说并无意义,但 Linux 服务器(无GUI或者作为只能使用基本工具的用户登录时)会很有用。无论如何,从 Linux 终端中获取公有 IP 在各种方面都很意义,说不定某一天就能用得着。

以下是我们主要使用的两个命令,curl 和 wget。你可以换着用。

 

Curl 纯文本格式输出:

  1. <span class="pln">curl icanhazip</span><span class="pun">.</span><span class="pln">com</span>
  2. <span class="pln">curl ifconfig</span><span class="pun">.</span><span class="pln">me</span>
  3. <span class="pln">curl curlmyip</span><span class="pun">.</span><span class="pln">com</span>
  4. <span class="pln">curl ip</span><span class="pun">.</span><span class="pln">appspot</span><span class="pun">.</span><span class="pln">com</span>
  5. <span class="pln">curl ipinfo</span><span class="pun">.</span><span class="pln">io</span><span class="pun">/</span><span class="pln">ip</span>
  6. <span class="pln">curl ipecho</span><span class="pun">.</span><span class="pln">net</span><span class="pun">/</span><span class="pln">plain</span>
  7. <span class="pln">curl www</span><span class="pun">.</span><span class="pln">trackip</span><span class="pun">.</span><span class="pln">net</span><span class="pun">/</span><span class="pln">i</span>

 

curl JSON格式输出:

  1. <span class="pln">curl ipinfo</span><span class="pun">.</span><span class="pln">io</span><span class="pun">/</span><span class="pln">json</span>
  2. <span class="pln">curl ifconfig</span><span class="pun">.</span><span class="pln">me</span><span class="pun">/</span><span class="pln">all</span><span class="pun">.</span><span class="pln">json</span>
  3. <span class="pln">curl www</span><span class="pun">.</span><span class="pln">trackip</span><span class="pun">.</span><span class="pln">net</span><span class="pun">/</span><span class="pln">ip</span><span class="pun">?</span><span class="pln">json </span><span class="pun">(有点丑陋)</span>

 

curl XML格式输出:

  1. <span class="pln">curl ifconfig</span><span class="pun">.</span><span class="pln">me</span><span class="pun">/</span><span class="pln">all</span><span class="pun">.</span><span class="pln">xml</span>

 

curl 得到所有IP细节 (挖掘机)

  1. <span class="pln">curl ifconfig</span><span class="pun">.</span><span class="pln">me</span><span class="pun">/</span><span class="pln">all</span>

 

使用 DYDNS (当你使用 DYDNS 服务时有用)

  1. <span class="pln">curl </span><span class="pun">-</span><span class="pln">s </span><span class="str">'http://checkip.dyndns.org'</span><span class="pun">|</span><span class="pln"> sed </span><span class="str">'s/.*Current IP Address: \([0-9\.]*\).*/\1/g'</span>
  2. <span class="pln">curl </span><span class="pun">-</span><span class="pln">s http</span><span class="pun">:</span><span class="com">//checkip.dyndns.org/ | grep -o "[[:digit:].]\+"</span>

 

使用 Wget 代替 Curl

  1. <span class="pln">wget http</span><span class="pun">:</span><span class="com">//ipecho.net/plain -O - -q ; echo</span>
  2. <span class="pln">wget http</span><span class="pun">:</span><span class="com">//observebox.com/ip -O - -q ; echo</span>

 

使用 host 和 dig 命令

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

  1. <span class="pln">host </span><span class="pun">-</span><span class="pln">t a dartsclink</span><span class="pun">.</span><span class="pln">com </span><span class="pun">|</span><span class="pln"> sed </span><span class="str">'s/.*has address //'</span>
  2. <span class="pln">dig </span><span class="pun">+</span><span class="kwd">short</span><span class="pln"> myip</span><span class="pun">.</span><span class="pln">opendns</span><span class="pun">.</span><span class="pln">com </span><span class="lit">@resolver1</span><span class="pun">.</span><span class="pln">opendns</span><span class="pun">.</span><span class="pln">com</span>

 

bash 脚本示例:

  1. <span class="com">#!/bin/bash</span>
  2. <span class="pln">PUBLIC_IP</span><span class="pun">=</span><span class="str">`wget http://ipecho.net/plain -O - -q ; echo`</span>
  3. <span class="pln">echo $PUBLIC_IP</span>

简单易用。

我实际上是在写一个用于记录每日我的路由器中所有 IP 变化并保存到一个文件的脚本。我在搜索过程中找到了这些很好用的命令。希望某天它能帮到其他人。


via: How to get Public IP from Linux Terminal?

译者:KevinSJ 校对:wxy

本文由 LCTT 原创翻译,Linux中国 荣誉推出

相关推荐