本文提供了两个简单的脚本showipaddr
(在Bash和Python中),以快速检索Linux中的私人(本地)和公共IP地址。尽管Linux提供了ip
和nmcli
之类的内置命令,但这些脚本提供了一种简化的方法来仅获取基本IP信息。
这些脚本允许您:
这两个脚本都在GitHub上可用。下载后,您需要使它们可执行,并可选地将其移至系统路径以轻松访问。
使用bash脚本( showipaddr.sh
):
此脚本需要Bash, curl
和jq
。如果需要,将提供有关这些的安装说明。
git clone https://gist.github.com/09259a3da0e7190775d16aadef14580b.git showipaddr
cd showipaddr/
chmod x showipaddr.sh
sudo mv showipaddr.sh /usr/local/bin/showipaddr
showipaddr
该脚本提示您选择是否显示本地IP,公共IP或两者兼而有之。
示例输出(两者显示):
<code>Select the information to display: 1. Local IP 2. Public IP 3. Both Local and Public IP Enter your choice (1/2/3): 3 ------------------------------------------------------ Local Network Information: Hostname : [hostname] Local IPv4 Address : [IPv4 address] Local IPv6 Address : [IPv6 address] Router IP Address : [router IP] DNS Server : [DNS server] ------------------------------------------------------ ------------------------------------------------------ Public IP Information: IP Address : [public IP address] ISP : [ISP] City : [City] Region : [Region] Country : [Country] Coordinates : [Latitude], [Longitude] ------------------------------------------------------</code>
使用Python脚本( showipaddr.py
):
此脚本需要Python 3和requests
库( pip install requests
)。它还检查jq
,但不直接使用它。
git clone https://gist.github.com/27276f43e980aa3a468c4b39680a68ee.git showipaddr
cd showipaddr/
python3 showipaddr.py
该脚本提供与Bash脚本相同的选择选项。
这两个脚本都提供了一种访问重要的IP地址信息的方便方法。选择最适合您的系统和偏好的脚本。
以上是如何使用ShowipAddr脚本在Linux中显示本地和公共IP地址的详细内容。更多信息请关注PHP中文网其他相关文章!