简体   繁体   English

如何在Linux中仅使用fs和bash从IPv4 ip地址获取接口名称?

[英]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: 解释是:

  • Anything destined for the 192.168.0.0/24 network (0000A8C0 + 00FFFFFF) goes to eth0 with no gateway needed (in other words, it's on the same subnet as this machine). 任何发往192.168.0.0/24网络(0000A8C0 + 00FFFFFF)的东西都转到eth0,不需要网关(换句话说,它与本机位于同一子网上)。
  • Anything destined for 192.168.40.0/24 (0028A8C0 + 00FFFFFF) goes to eth1 with no gateway needed. 任何发往192.168.40.0/24(0028A8C0 + 00FFFFFF)的东西都会进入eth1,不需要网关。
  • Packets destined for any other address (00000000 + 00000000) go to the default gateway 192.168.0.1. 发往任何其他地址(00000000 + 00000000)的数据包将转至默认网关192.168.0.1。
    (Not sure; it's possible that the extra flag bit or the "metric" value in this line marks this as a default gateway. But in any case, if it doesn't have a non-zero mask or is in the 0.0.0.0 address range, it's not going to be useful for routing in the real world.) (不确定;此行中的额外标志位或“metric”值可能将此标记为默认网关。但无论如何,如果它没有非零掩码或者在0.0.0.0中地址范围,它对现实世界中的路由没有用。)

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.

相关问题 如何在Linux上使用libnl3(netlink版本3)获取接口的ipv4地址? - How to get ipv4 address of an interface using libnl3 (netlink version 3) on linux? 使用netlink API从linux接口删除ipv4地址属于同一子网 - deleting ipv4 address belongs to the same subnet from linux interface using netlink API IP邻居获取IPV4地址 - IP Neighbour to get IPV4 address 如何在C程序中从Linux中的IPv6地址获取网络接口名称? - How to get network interface name from IPv6 address in linux in a C program? 如何从机器名称请求IPv4地址? - How to request IPv4 address from a machine name? 与C linux中的ipv6范围相比,如何查找ipv4地址的范围名称(链接,主机和全局) - how to find ipv4 address scope name (link, host and global) as compared with ipv6 scope in C linux 如何获取IP地址(IPv4为NULL,但存在IPv6),有关内容的详细信息 - How to get IP address (IPv4 is NULL, but IPv6 is exist), detail for content 如何在 Linux 中获取每个接口和协议(IPv6/IPv4)的统计信息? - How to get per interface and protocol (IPv6/IPv4) statistics in Linux? 接收未分配地址以与Linux / C / IPv4接口的最广泛的UDP - Receive UDP broadast with no address assigned to interface with Linux/C/IPv4 如何获取此内容中的所有ipv4地址 - how to get all the ipv4 address in this content
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM