Linux 获取所有网络接口名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19227781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:01:50  来源:igfitidea点击:

Linux getting all network interface names

clinuxnetwork-interface

提问by tez

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

我需要收集所有的接口名称,即使是那些目前没有启动的名称。喜欢ifconfig -a

getifaddrs()is iterating through same interface name multiple times. How can I collect all the interface names just once using getifaddrs()?

getifaddrs()多次迭代相同的接口名称。如何仅使用一次就收集所有接口名称getifaddrs()

采纳答案by brm

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

您可以检查 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("%s\n", tmp->ifa_name);

    tmp = tmp->ifa_next;
}

freeifaddrs(addrs);

回答by aet

You are on the right track (it is getifaddrs). It returns each interface once per family, so you get eth0 for ipv4 and one for ipv6. If you just want each interface once you will have to uniq the output yourself.

你在正确的轨道上(它是 getifaddrs)。它为每个系列返回每个接口一次,因此您将获得用于 ipv4 的 eth0 和用于 ipv6 的 eth0。如果您只想要每个接口一次,您将不得不自己 uniq 输出。

回答by ldav1s

It seems that ifconfig -aonly lists active interfaces (at least on Fedora 19). I know I have at least one more network card that I'm not seeing. Anyway, I get the same list as:

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

ls -1 /sys/class/net

Which could easily be done programatically.

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

回答by Makaro

I thing this show you all interface, at least for me

我认为这会向您展示所有界面,至少对我而言

ip link show

ip链接显示

ls -1 /sys/class/net

only show interface name

只显示接口名称

lo
p4p1

回答by Mike Q

I need the main device that is being used by the system (assuming there is only one) such as eth0 wlan0 or whatever RHEL7 is trying to do...

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

The best I hacked together was this:

我一起破解的最好的是:

#!/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 ' == "inet" {gsub(/\/.*$/, "", ); print }' | grep "$(hostname -I | cut -d' ' -f1)")

    if [ ! -z "${result// }" ] && [ -d /sys/class/net/${each// } ] ;then
            echo "Device: $each IP: $result"
            break;
    fi

done

Sample output:

示例输出:

    ./maineth.sh  
    Device: enp0s25 IP: 192.168.1.6

回答by Hugues

getifaddrs() will only return your interfaces addresses, not the interfaces themselves.

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

What if any of your interface has no address, or no address of the requested family, as suggested with the 'AF_PACKET' one ?

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

Here, an example where I've got a tunnel interface (with an OpenVPN connexion), and where I'm listing all entries from getifaddrs() for each of my network interfaces:

在这里,我有一个隧道接口(带有 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]

Bam. No AF_PACKET on the "tun0" interface, but it DOES exist on the system.

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

You should, instead, use if_nameindex() syscall, which does exactly what you want. In other words, with no arguments, it returns a list of all interfaces on your system:

相反,您应该使用 if_nameindex() 系统调用,它完全符合您的要求。换句话说,没有参数,它返回系统上所有接口的列表:

#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("%s\n", intf->if_name);
        }

        if_freenameindex(if_nidxs);
    }

    return 0;
}

And voilà.

瞧。