使用 BASH 查找内部 IP 地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5548032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:44:51  来源:igfitidea点击:

Find internal IP address with BASH

bashnetworkingscriptingiptcp

提问by wrangler

I am already aware of many ways of getting your internal IP (ifconfig, ip addr, /etc/hosts, etc), but I am trying to write a bash script that will always return the internal IP. The problem is, many one-liners (/sbin/ifconfig|grep inet|head -1|sed 's/\:/ /'|awk '{print $3}') can return multiple IPs, and I need to distinguish the internal one manually.

我已经知道多种获取内部 IP 的方法(ifconfig、ip addr、/etc/hosts 等),但我正在尝试编写一个始终返回内部 IP 的 bash 脚本。问题是,很多单行(/sbin/ifconfig|grep inet|head -1|sed 's/\:/ /'|awk '{print $3}')可以返回多个IP,我需要区分内部一个手动。

I suspect that to the computer, there is no difference between and an external IP and an internal IP, and thus no 100%, guaranteed way to get the right IP.

我怀疑对于计算机,外部 IP 和内部 IP 之间没有区别,因此没有 100% 保证获得正确 IP 的方法。

The end result is that this script will return the internal IP, no matter if its a 192 address or a 204 address, etc.

最终的结果是这个脚本将返回内部IP,无论是192地址还是204地址等。

Thanks in advance.

提前致谢。

回答by Keo Malope

"hostname -i" should hopefully give you the same result

hostname -i”应该希望给你同样的结果

回答by intuited

As others have mentioned, a machine is not really guaranteed, or even likely, to have a single IP address. I'm not sure exactly what you mean by "internal IP"; sometimes this can mean "IP address on the local network", i.e. the interface which connects to a NAT-enabled firewall.

正如其他人所提到的,一台机器并不能真正保证,甚至可能只有一个 IP 地址。我不确定您所说的“内部 IP”到底是什么意思;有时这可能意味着“本地网络上的 IP 地址”,即连接到启用 NAT 的防火墙的接口。

I'm thinking that the best way to do this is to connect to a host on the network you want and use the address from which that connection originates. This will be the interface which the machine normally uses to connect to that network. The user Unkwntech had the same idea on this thread. The code below is just taken from that answer.

我认为最好的方法是连接到您想要的网络上的主机并使用该连接的来源地址。这将是机器通常用于连接到该网络的接口。用户 Unkwntech 在这个线程上也有同样的想法。下面的代码只是取自那个答案。

I don't know if this really qualifies as a "bash" solution, since it's just an inline Python script, but anyway this will get you the local ip address used to reach google.com. So this will give you the IP address of whichever interface the machine uses to reach Internet hosts.

我不知道这是否真的符合“bash”解决方案的条件,因为它只是一个内联 Python 脚本,但无论如何这将为您提供用于访问google.com的本地 IP 地址。因此,这将为您提供机器用于访问 Internet 主机的任何接口的 IP 地址。

$ python -c 'import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("google.com", 80))
print s.getsockname()[0]'

A more bash-y solution might use tracepathor some similar utility.

一个更 bash-y 的解决方案可能会使用tracepath或一些类似的实用程序。

回答by mroach

Systems can have multiple private IPs too though. You would have to limit your searching on IPs to private IPs. 10.0.0.0/8, 172.16.0.0/12, and 192.168.0.0/16.

不过,系统也可以有多个私有 IP。您必须将对 IP 的搜索限制为私有 IP。10.0.0.0/8、172.16.0.0/12 和 192.168.0.0/16。

回答by sarnold

Within the RFC 1918private address spaces, a machine could conceivably have everyaddress in the 10/8range, the 172.16/12range, and the 192.168/16range, for a total of 17891328 IP addresses, and allof them would be legal "internal" IPs.

RFC 1918私有地址空间,一台机器可以想见,有每一个在地址10/8范围,在172.16/12范围,192.168/16范围,共计17891328个IP地址,以及所有他们的将是法律的“内部” IP地址。

Oh yes, don't forget IPv6 :) 2^64 possible addresses per networkfor a single machine, which might participate in multiple networks.

哦,是的,不要忘记 IPv6 :)单个机器每个网络有2^64 个可能的地址,这可能参与多个网络。

This isn't exactly academic, either: it is quite common for VMWare, VirtualBox, QEMU, etc. host systems to have multiple RFC 1918 addresses assigned; one for the 'usual use', and one that is used specifically to communicate with guest operating systems. Or routers / firewalls, they might have a dozen internal IPs specifically to subnet a network for access control reasons.

这也不完全是学术性的:VMWare、VirtualBox、QEMU 等主机系统分配多个 RFC 1918 地址是很常见的;一种用于“通常用途”,另一种专门用于与客户操作系统进行通信。或者路由器/防火墙,出于访问控制的原因,它们可能有十几个专门用于子网网络的内部 IP。