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

 

 

引用http://blog.csdn.net/nbda1121440/article/details/8963352

posted @ 2015-03-04 11:17  Hiker天下  阅读(1937)  评论(0编辑  收藏  举报