Linux getting all network interface names

前端 未结 6 1946
傲寒
傲寒 2020-12-01 08:30

I need to collect all the interface names, even the ones that aren\'t up at the moment. Like ifconfig -a.

getifaddrs() is iterating through

6条回答
  •  盖世英雄少女心
    2020-12-01 08:53

    You could check which entries from getifaddrs belong to the AF_PACKET family. On my system that seems to list all interfaces:

    struct ifaddrs *addrs,*tmp;
    
    getifaddrs(&addrs);
    tmp = addrs;
    
    while (tmp)
    {
        if (tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_PACKET)
            printf("%s\n", tmp->ifa_name);
    
        tmp = tmp->ifa_next;
    }
    
    freeifaddrs(addrs);
    

提交回复
热议问题