linux获取mac地址
1 #include <stdio.h> 2 #include <string.h> 3 4 #include <sys/types.h> 5 #include <sys/socket.h> 6 #include <sys/ioctl.h> 7 #include <netinet/in.h> 8 #include <net/if.h> 9 10 int 11 main() 12 { 13 int fd; 14 struct ifreq ifr; 15 16 fd = socket(AF_INET, SOCK_DGRAM, 0); 17 18 ifr.ifr_addr.sa_family = AF_INET; 19 strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1); 20 21 ioctl(fd, SIOCGIFHWADDR, &ifr); 22 23 close(fd); 24 25 printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n", 26 (unsigned char)ifr.ifr_hwaddr.sa_data[0], 27 (unsigned char)ifr.ifr_hwaddr.sa_data[1], 28 (unsigned char)ifr.ifr_hwaddr.sa_data[2], 29 (unsigned char)ifr.ifr_hwaddr.sa_data[3], 30 (unsigned char)ifr.ifr_hwaddr.sa_data[4], 31 (unsigned char)ifr.ifr_hwaddr.sa_data[5]); 32 33 return 0; 34 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。