如何从接口ip地址获取接口索引
谁能告诉我如何从接口IP地址获取接口索引? 例如,如果接口 IP 地址是 192.168.23.25,那么它的接口索引是多少。
我想补充一点,我需要在用 c 编写的代码中使用它,所以如果有任何函数 通过一些选项可以给我基于的接口索引号 接口IP地址。
Can anyone tell me how to get interface index from interface ip address?
e.g. If interface ip address is 192.168.23.25 then what is it's interface index.
I want to add on that i need to use it in one code written in c so if any function
with some option can give me the interface index number on the base of the
interface ip address.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您应该能够使用 getifaddrs() 来完成此操作。它应该考虑到 MarkR 对辅助地址的担忧。作为测试,
添加如下内容后:
在手册页上编译并运行示例程序应显示如下内容:
您应该能够在遍历列表时获取索引,但您还可以另外查看 if_nametoindex(), if_indextoname() 和 if_nameindex() 功能。由于您将能够将地址与接口名称关联起来,因此您可以根据需要调用它们。
You should be able to do this with getifaddrs(). It should account for MarkR's concern about secondary addresses. As a test,
After adding something like this:
compiling and running the example program on the man page should show something like:
You should be able to get the index as you traverse the list but you can also additionally look at the if_nametoindex(), if_indextoname(), and if_nameindex() functions. Since you will be able to associate an address with an interface name you could just call these as appropriate.
你不能这样做,你必须查看所有接口,然后循环所有 IP 地址,直到找到你想要的那个。我认为这段代码可以满足您的要求。
You can't do that, you have to look at all interfaces, then loop through all IP addresses until you find the one you want. I think this code does what you want.
以编程方式使用 if_nametoindex()。我已经在 Ubuntu 12.04(内核 3.11.0-15-generic)上验证了这一点。
下面是示例代码片段,
用法示例:
另外,非编程方法是读取 /proc/net/if_inet6 条目。第二列是对应的接口索引。
Programmatically, use if_nametoindex(). I have verified this on Ubuntu 12.04 (kernel 3.11.0-15-generic).
Here is the sample code snippet,
Usage example:
In addition, non-programming approach is to read /proc/net/if_inet6 entry. The second column is the corresponding interface index.
您可以简单地使用 ip 的 json-Output 并通过 jq: 扫描它
并提供正确的参数:
You can simply use the json-Output of ip and scan it via jq:
gives with the correct parameters:
你可以用这个:它将列出 Linux 机器上的网络接口。
我从这里得到它。
You could use this:. It will list the network interfaces on your Linux box.
I got it from here.
注意:我刚刚注意到OP要求使用
C
解决方案。我很抱歉。然而,由于 intergoogles 在我的搜索短语前面明确包含 bash 时将我发送到这里(并且因为我已经输入了所有这些内容),所以我希望人们不介意我将其留在这里。如果我有时间,我稍后会把它变成要点,看看是否可以在这里删除它。我有一个相对简单的解决方案,可以使用 shell 脚本。我喜欢@Oliver的建议,但它没有达到获取给定IP地址的接口索引的最初目标。我对他的答案做了一些调整。假设我想知道哪个接口索引当前绑定到192.168.1.96。此方法将起作用:
输出:
您可以通过删除
.ifindex
元素的选择来检索 NIC 的所有 JSON 来检查它是否正确:此方法将自动使用辅助地址和 IPv6只需更改命令中给出的 IP 地址即可地址。
此外,如果您只知道要查找的地址的一个子字符串,则只需进行细微调整即可找到该子字符串。它不如按子网过滤那么有用,但有时可能会有所帮助。
输出:
NOTE: I just noticed that the OP asked for a solution in
C
. My apologies. However, since the intergoogles sent me here when explicitly including bash at the front of my search phrase (and since I've already typed all of this up), I hope people don't mind me leaving it here. If I have time I will turn this into a gist later and see if I can delete it here.I have a relatively easy solution that works with shell scripts. I liked @Oliver's suggestion, but it did not meet the original goal of obtaining the interface index for a given IP address. I have tweaked his answer a bit. Suppose I want to know which interface index is currently bound to 192.168.1.96. This approach will work:
output:
You can check that it is correct by removing the selection of the
.ifindex
element to retrieve all of the JSON for the NIC:This method will work automatically with secondary addresses and IPv6 addresses by just changing the IP address given in the command.
Also, if you only know a substring of the address you are looking for, a minor adjustment allows you to find that as well. It's not as useful as being able to filter by subnet, but it may be helpful sometimes.
output:
SIOCGIFCONF 不会削减使用添加的辅助 IP 地址
如果您确实需要这样做,那么看看“ip addr sh”使用的任何内容 - 可能是一个 netlink 套接字操作(其中涉及非常奇怪的宏)
SIOCGIFCONF won't cut it for secondary IP addresses added using
If you really need to do this, then look at whatever "ip addr sh" uses - probably a netlink socket operation (which involves really strange macros)