如何在 Linux 终端中知道你的公有 IP
公有地址由 InterNIC 分配并由基于类的网络 ID 或基于 CIDR 的地址块构成(被称为 CIDR 块),并保证了在全球互联网中的唯一性。当公有地址被分配时,其路由将会被记录到互联网中的路由器中,这样访问公有地址的流量就能顺利到达。访问目标公有地址的流量可经由互联网抵达。比如,当一个 CIDR 块被以网络 ID 和子网掩码的形式分配给一个组织时,对应的 [网络 ID,子网掩码] 也会同时作为路由储存在互联网中的路由器中。目标是 CIDR 块中的地址的 IP 封包会被导向对应的位置。
在本文中我将会介绍在几种在 Linux 终端中查看你的公有 IP 地址的方法。这对普通用户来说并无意义,但 Linux 服务器(无GUI或者作为只能使用基本工具的用户登录时)会很有用。无论如何,从 Linux 终端中获取公有 IP 在各种方面都很意义,说不定某一天就能用得着。
以下是我们主要使用的两个命令,curl 和 wget。你可以换着用。
Curl 纯文本格式输出:
<span class="pln">curl icanhazip</span><span class="pun">.</span><span class="pln">com</span>
<span class="pln">curl ifconfig</span><span class="pun">.</span><span class="pln">me</span>
<span class="pln">curl curlmyip</span><span class="pun">.</span><span class="pln">com</span>
<span class="pln">curl ip</span><span class="pun">.</span><span class="pln">appspot</span><span class="pun">.</span><span class="pln">com</span>
<span class="pln">curl ipinfo</span><span class="pun">.</span><span class="pln">io</span><span class="pun">/</span><span class="pln">ip</span>
<span class="pln">curl ipecho</span><span class="pun">.</span><span class="pln">net</span><span class="pun">/</span><span class="pln">plain</span>
<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格式输出:
<span class="pln">curl ipinfo</span><span class="pun">.</span><span class="pln">io</span><span class="pun">/</span><span class="pln">json</span>
<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>
<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格式输出:
<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细节 (挖掘机)
<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 服务时有用)
<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>
<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
<span class="pln">wget http</span><span class="pun">:</span><span class="com">//ipecho.net/plain -O - -q ; echo</span>
<span class="pln">wget http</span><span class="pun">:</span><span class="com">//observebox.com/ip -O - -q ; echo</span>
使用 host 和 dig 命令
如果有的话,你也可以直接使用 host 和 dig 命令。
<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>
<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 脚本示例:
<span class="com">#!/bin/bash</span>
<span class="pln">PUBLIC_IP</span><span class="pun">=</span><span class="str">`wget http://ipecho.net/plain -O - -q ; echo`</span>
<span class="pln">echo $PUBLIC_IP</span>
简单易用。
我实际上是在写一个用于记录每日我的路由器中所有 IP 变化并保存到一个文件的脚本。我在搜索过程中找到了这些很好用的命令。希望某天它能帮到其他人。
相关推荐
83911535 2020-11-13
曾是土木人 2020-10-31
yegen00 2020-10-21
soralaro 2020-10-11
katanaFlower 2020-09-18
wytzsjzly 2020-08-17
88407710 2020-08-17
ChinaJoeEE 2020-08-16
CyborgLin 2020-08-15
Blueberry 2020-08-15
PinkBean 2020-08-11
katanaFlower 2020-08-03
hunningtu 2020-07-30
阿债的方寸天地 2020-06-28
pingyan 2020-06-25
wytzsjzly 2020-06-25
阳光岛主 2020-06-25
阿债的方寸天地 2020-06-16