C语言 Linux 获取所有网络接口名称



>我需要收集所有接口名称,甚至是目前尚未启动的接口名称。喜欢ifconfig -a.

getifaddrs()多次循环访问相同的接口名称。如何使用getifaddrs()一次收集所有接口名称?

你可以检查getifaddrs中的哪些条目属于AF_PACKET家族。在我的系统上,似乎列出了所有接口:

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

getifaddrs() 只会返回接口地址,而不是接口本身。

如果您的任何接口没有地址,或者没有所请求家庭的地址,如"AF_PACKET"接口所建议的那样怎么办?

在这里,我有一个隧道接口(带有OpenVPN连接),并且我列出了每个网络接口的getifaddrs()的所有条目:

[0] 1: lo                address family: 17 (AF_PACKET) b4:11:00:00:00:01
                         address family: 2 (AF_INET)    address: <127.0.0.1>
                         address family: 10 (AF_INET6)  address: <::1>
[...]
[5] 10: tun0             address family: 2 (AF_INET)    address: <172.16.0.14>
[EOF]

砰。"tun0"接口上没有AF_PACKET,但它确实存在于系统上。

相反,您应该使用 if_nameindex() syscall,它完全可以执行您想要的操作。换句话说,在没有参数的情况下,它会返回系统上所有接口的列表:

#include <net/if.h>
#include <stdio.h>
int main (void)
{
    struct if_nameindex *if_nidxs, *intf;
    if_nidxs = if_nameindex();
    if ( if_nidxs != NULL )
    {
        for (intf = if_nidxs; intf->if_index != 0 || intf->if_name != NULL; intf++)
        {
            printf("%sn", intf->if_name);
        }
        if_freenameindex(if_nidxs);
    }
    return 0;
}

瞧。

似乎ifconfig -a只列出了活动接口(至少在 Fedora 19 上)。 我知道我至少还有一个网卡,我没有看到。 无论如何,我得到的列表与:

ls -1 /sys/class/net

这可以很容易地以编程方式完成。

我想这显示了所有界面,至少对我来说是这样

IP链接显示

ls -1 /sys/class/net

仅显示接口名称

lo
p4p1

你走在正确的轨道上(它是getifaddrs)。它为每个系列返回每个接口一次,因此您可以获得 ipv4 的 eth0 和 ipv6 的一个。如果您只需要每个接口一次,则必须自己对输出进行单质化。

我需要系统正在使用的主设备(假设只有一个),例如 eth0 wlan0 或任何 RHEL7 试图做的事情......

我拼凑的最好的是:

#!/bin/bash
# -- Get me the interface for the main ip on system
for each in $(ls -1 /sys/class/net) ;do 
    result=$(ip addr show $each | awk '$1 == "inet" {gsub(//.*$/, "", $2); print $2}' | grep "$(hostname -I | cut -d' ' -f1)")
    if [ ! -z "${result// }" ] && [ -d /sys/class/net/${each// } ] ;then
            echo "Device: $each IP: $result"
            break;
    fi
done

示例输出:

    ./maineth.sh  
    Device: enp0s25 IP: 192.168.1.6

使用这个。为了便于理解,我添加了适当的注释。

void extract_network_interface_names()
{
    /* DEFINITION
     *  #include <net/if.h>
     *  struct if_nameindex * if_nameindex() :  function that returns a pointer.
     *  The if_nameindex()  function returns an array of if_nameindex structs. There is one struct for each network interface
     *  The if_nameindex struct contains at least the following members:
     *      unsigned int if_index : which is the index of each network interface {1,2,..n}
     *      char * if_name : which is the name of the interface=the C style string= an array of characters terminated in the NULL character 
     *
     *  The end of the array of struct is indicated by an entry with if_index=0 && if_name=NULL
     *  The if_nameindex() function returns an array of structs if successful, or NULL if some error occurred or not enough memory is available
     */
    puts("Extracting network interface names...");
    puts("Listing all available network interface names on this machine...");
    puts("********************************");
    int counter_network_interfaces=0;
    struct if_nameindex * interface_indexes;
    struct if_nameindex * interface;
    interface_indexes=if_nameindex();
    if(interface_indexes==NULL) //Some error occurred during the execution of if_nameindex() function or there is no enough memory available
    {
        perror("If_nameindex");
        printf("Ooops...error while extracting the network interface names.n");
        exit(EXIT_FAILURE);
    }
    //Loop through the elements of the array of if_nameindex structs
    for(interface=interface_indexes;interface->if_index!=0 && interface->if_name!=NULL;interface++)
        {
            counter_network_interfaces++;
            printf("There exists a network interface called "%s" with index %d. n",interface->if_name, interface->if_index);
        }
        puts("*******************************");
        printf("In total, there is a number of %d network interfaces on this machine.n",counter_network_interfaces);

}

最新更新