首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检测网卡是否是Linux / bsd中的虚拟网卡(docker/veth/etc)

检测网卡是否是Linux / bsd中的虚拟网卡(docker/veth/etc)
EN

Server Fault用户
提问于 2017-05-30 07:10:51
回答 1查看 1.4K关注 0票数 5

我在页面的ubuntu中看到了获取网卡及其统计信息的一些很好的解释。正如页面中提到的那样,这提供了一个很好的输出。我也试过阅读其他文档,但是找不到一个标志或类似的东西,在那里我可以区分系统上的真实网卡和虚拟网卡。

有什么方法可以区分吗?谢谢。

EN

回答 1

Server Fault用户

回答已采纳

发布于 2017-05-30 07:38:51

检查/sys/class/net/<device_name>符号链接。如果它指向/sys/devices/virtual/,那么它就是一个虚拟接口。如果它指向一个“真实”的设备(例如进入/sys/devices/pci0000:00/),那么它就不是。

编辑:

从代码中,您可以使用readlink检查设备是否是虚拟的。下面是一个非常简单的示例代码:

代码语言:javascript
复制
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

int main(int argc, char **argv) {
    char theLink[128];
    char thePath[128];

    strcpy(thePath,"/sys/class/net/");
    memset(theLink,0,128);

    if (argc>1) {
        strcat(thePath,argv[1]);
    } else {
        printf("Gimme device\n");
        return 1;
    }

    if (readlink(thePath, theLink, 127)==-1) {
        perror(argv[1]);
    } else {
        if (strstr(theLink,"/virtual")) {
            printf("%s is a virtual device\n",argv[1]);
        } else {
            printf("%s is a physical device\n",argv[1]);
        }
    }
}
票数 6
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/852947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档