【转载】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 }