繁体   English   中英

如何以编程方式检索 eth0 id

[英]How to retrieve eth0 id programmatically

当我在 Linux 机器上/sbin/ip addr show 我得到如下输出

3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 00:00:21:02:16:6b brd ff:ff:ff:ff:ff:ff .. so on

如何在 C 或 CPP 中以编程方式为eth0检索上述 id 3。

使用if_nametoindex () 函数。

unsigned int idx = if_nametoindex("eth0");
if (idx == 0) {
    perror("if_nametoindex");
 }

用于处理网络接口的标准 linux c 库是getifaddrs

您可以从 getifaddrs 获取现有接口的链接列表并对其进行计数,直到找到您要查找的接口。

除此之外,尚不完全清楚您为什么需要该数字(我有XY 问题的预感)。 我这么说是因为操作系统按名称不同接口。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM