[英]How to get interface name from IPv4 ip address using only fs and bash in Linux?
How to get interface name from IPv4 ip address using only filesystem like /proc, /dev, /sys and bash? 如何使用/ proc,/ dev,/ sys和bash等文件系统从IPv4 IP地址获取接口名称? Is this possible without using the commands (such as ip, ifconfig, etc.)? 这可能不使用命令(如ip,ifconfig等)吗? I am also not able to use any packages or install any tools. 我也无法使用任何软件包或安装任何工具。
Edit: The reason for this is I am trying to get host's interface names and ip addresses from within a Docker container. 编辑:原因是我试图从Docker容器中获取主机的接口名称和IP地址。 The container is mounted with host's root and has access to host's filesystem. 容器安装有主机的根目录,并且可以访问主机的文件系统。 However, since the container is in a separate namespace, ip commands (and other similar command such as ifconfig) will only return the container's network. 但是,由于容器位于单独的命名空间中,因此ip命令(以及其他类似命令,如ifconfig)将仅返回容器的网络。 Hence, I believe the only way of getting the host's interface names and ip addresses is through the host's root (/hostroot/proc, /hostroot/sys, etc.). 因此,我认为获取主机的接口名称和IP地址的唯一方法是通过主机的根目录(/ hostroot / proc,/ hostroot / sys等)。 Note that I cannot have the --net=host flag when starting the container (which tells the container to use the host's net namespace). 请注意,启动容器时我不能使用--net = host标志(它告诉容器使用主机的net命名空间)。
I tried finding all my network interface IP addresses (IPv4) from the /proc/net/fib_trie file. 我尝试从/ proc / net / fib_trie文件中查找所有网络接口IP地址(IPv4)。 I read at https://regexit.com/3-ways-to-get-your-ip-on-a-linux-system/ which basically says that I can do cat /proc/net/fib_trie | 我在https://regexit.com/3-ways-to-get-your-ip-on-a-linux-system/上阅读,基本上我说我可以做cat / proc / net / fib_trie | grep -B 1 "32 host LOCAL" to find the IP addresses. grep -B 1“32 host LOCAL”查找IP地址。 I also see in https://stackoverflow.com/a/42082822 , that we can filter the fib_trie by pattern matching the string "32 host" to get the local network interface addresses. 我还在https://stackoverflow.com/a/42082822中看到,我们可以通过匹配字符串“32 host”的模式过滤fib_trie以获取本地网络接口地址。 After getting the IP addresses from the above method (using the /proc/net/fib_trie), I try to match those IP address with the "destination" IP address in /proc/net/route to get the network interface name. 从上面的方法获取IP地址(使用/ proc / net / fib_trie)后,我尝试将这些IP地址与/ proc / net / route中的“目标”IP地址进行匹配,以获取网络接口名称。 However, my /proc/net/route contains multiple entry for some interfaces (such as enp0s3). 但是,我的/ proc / net / route包含一些接口的多个条目(例如enp0s3)。 For example, there are two enp0s3 entries with different IP addresss ("Destination column). Moreover, the network interfaces in route file does have ip close to what is indicated by the ip command but not exactly. 例如,有两个具有不同IP地址的enp0s3条目(“目标列”)。此外,路由文件中的网络接口确实具有接近ip命令指示的ip但不完全正确。
Is there a better way of getting the network interface name after getting the IP address from fib_trie? 从fib_trie获取IP地址后,是否有更好的方法来获取网络接口名称?
Output I need will be all the host's network interface name and corresponding IPv4 IP addresses. 我需要的输出将是所有主机的网络接口名称和相应的IPv4 IP地址。
I think you have sufficient information there. 我认为你那里有足够的信息。 The /proc/net/route
file doesn't really list addresses. /proc/net/route
文件并不真正列出地址。 It's supposed to tell you how to route packets. 它应该告诉你如何路由数据包。 But you can glean information about the interfaces from it. 但是你可以从中收集有关接口的信息。 On my system (ubuntu 14 so different interface name format but otherwise should be similar), it shows this: 在我的系统上(ubuntu 14这么不同的接口名称格式,但在其他方面应该是类似的),它显示了这个:
Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
eth0 00000000 0100A8C0 0003 0 0 0 00000000 0 0 0
eth0 0000A8C0 00000000 0001 0 0 1 00FFFFFF 0 0 0
eth1 0028A8C0 00000000 0001 0 0 1 00FFFFFF 0 0 0
The destination and mask fields are in hexadecimal -- and byte-reversed on little-endian machine -- and give you the range of the network. 目标和掩码字段是十六进制的 - 在little-endian机器上进行字节反转 - 并为您提供网络范围。 Interpretation is: 解释是:
From this I can infer that my system has IP addresses in the 192.168.0.0/24 and 192.168.40.0/24 networks. 由此我可以推断出我的系统在192.168.0.0/24和192.168.40.0/24网络中具有IP地址。 The output from the grep command on /proc/net/fib_trie
shows on my system: /proc/net/fib_trie
上的grep命令输出显示在我的系统上:
|-- 127.0.0.1
/32 host LOCAL
|-- 192.168.0.104
/32 host LOCAL
|-- 192.168.40.129
/32 host LOCAL
So that makes it clear that my local IP addresses are 192.168.0.104 (eth0) and 192.168.40.129 (eth1). 这样就可以清楚地知道我的本地IP地址是192.168.0.104(eth0)和192.168.40.129(eth1)。 (We can ignore 127.0.0.1; it's always the loopback interface.) (我们可以忽略127.0.0.1;它始终是环回接口。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.