#include <net/if.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define ETH_NAME "enp2s0" //如果要获取其他网卡的地址,将这个换为其他网卡名称,比如eth0
void get_mac(char * mac_a,char *netname)
{
int sockfd;
struct ifreq ifr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == -1) {
perror("socket error");
exit(1);
}
strncpy(ifr.ifr_name, netname, IFNAMSIZ); //Interface name
if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) == 0)
{ //SIOCGIFHWADDR 获取hardware address
memcpy(mac_a, ifr.ifr_hwaddr.sa_data, 6);
}
}
#include <unistd.h>
#
Linux下遍历目录下的所有文件(获取mac)
最新推荐文章于 2023-08-20 16:24:43 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)