打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Linux下C语言获取所有网卡信息的代码
http://www.open-open.com/code/view/1432215294567
2015.05
#include <sys/ioctl.h>#include <net/if.h>#include <unistd.h>#include <netinet/in.h>#include <string.h> int main(){    struct ifreq ifr;    struct ifconf ifc;    char buf[2048];    int success = 0;     int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);    if (sock == -1) {        printf("socket error\n");        return -1;    }     ifc.ifc_len = sizeof(buf);    ifc.ifc_buf = buf;    if (ioctl(sock, SIOCGIFCONF, &ifc) == -1) {        printf("ioctl error\n");        return -1;    }     struct ifreq* it = ifc.ifc_req;    const struct ifreq* const end = it + (ifc.ifc_len / sizeof(struct ifreq));    char szMac[64];    int count = 0;    for (; it != end; ++it) {        strcpy(ifr.ifr_name, it->ifr_name);        if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0) {            if (! (ifr.ifr_flags & IFF_LOOPBACK)) { // don't count loopback                if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) {                    count ++ ;                    unsigned char * ptr ;                    ptr = (unsigned char  *)&ifr.ifr_ifru.ifru_hwaddr.sa_data[0];                    snprintf(szMac,64,"%02X:%02X:%02X:%02X:%02X:%02X",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5));                    printf("%d,Interface name : %s , Mac address : %s \n",count,ifr.ifr_name,szMac);                }            }        }else{            printf("get mac info error\n");            return -1;        }    }} 

执行结果如下
1,Interface name : eth0 , Mac address : 90:B1:1D:87:13:752,Interface name : virtac , Mac address : 52:54:00:96:E0:BF3,Interface name : map , Mac address : 52:54:00:F2:FE:9C4,Interface name : poi , Mac address : 52:54:00:1B:97:825,Interface name : vnet1 , Mac address : 52:54:00:05:14:E3

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
《Linux网络接口》---------struct ifreq struct ifconf
ioctl函数详解
linux下ioctl操作网络接口
ioctl()函数
用ioctl获得本地ip地址
几个网络编程常用的数据结构sockaddr_in
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服