歡迎您光臨本站 註冊首頁

Linux下獲取eth網卡MAC地址的代碼

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

因開發需要獲取有線網卡的MAC地址,發現網上獲取的方法多數只能獲取聯網網卡的MAC地址,因此重寫了下Ubuntu10下測試通過.

下面代碼無論網卡是否連線,都可以獲取MAC地址,稍作修改,可以輸出系統所有的網卡硬體MAC地址,無論是否已經聯網.

  1. /*
  2. * getmac.c
  3. *
  4. * Created on: 2010-11-4
  5. * Author: carl
  6. */
  7. #include <stdio.h>
  8. #include <fcntl.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/types.h>
  14. #include <sys/socket.h>
  15. #include <netinet/in.h>
  16. #include <linux/if.h>
  17. #define IFNAMSIZ 16

  18. // data structs to store interface name list
  19. char ifname_buf[2048];
  20. char *ifnames = ifname_buf;
  21. int count = 0;
  22. void add_interface_name(const char * name)
  23. {
  24. int i;
  25. for (i=0;i<count;i )
  26. {
  27. if (!strcmp(ifnames i*IFNAMSIZ, name))
  28. return;
  29. }
  30. strncpy(ifnames (count )*IFNAMSIZ, name, IFNAMSIZ-1);
  31. }
  32. char * get_name(char *name, char *p)
  33. {
  34. while (isspace(*p))
  35. p ;
  36. while (*p) {
  37. if (isspace(*p))
  38. break;
  39. if (*p == ':') { /* could be an alias */
  40. char *dot = p, *dotname = name;
  41. *name = *p ;
  42. while (isdigit(*p))
  43. *name = *p ;
  44. if (*p != ':') { /* it wasn't, backup */
  45. p = dot;
  46. name = dotname;
  47. }
  48. if (*p == '')
  49. return NULL;
  50. p ;
  51. break;
  52. }
  53. *name = *p ;
  54. }
  55. *name = '';
  56. return p;
  57. }
  58. // get /proc/net/dev interface name list into buffer
  59. // return 0 if success
  60. int get_procnet_list()
  61. {
  62. FILE

    *fh;
  63. char buf[512];
  64. fh = fopen("/proc/net/dev", "r");
  65. if (!fh)
  66. return -1;
  67. fgets(buf, sizeof buf, fh); /* eat title lines */
  68. fgets(buf, sizeof buf, fh);
  69. while (fgets(buf, sizeof buf, fh))
  70. {
  71. char name[IFNAMSIZ];
  72. get_name(name, buf);
  73. add_interface_name(name);
  74. }
  75. fclose(fh);
  76. return 0;
  77. }
  78. long mac_addr_sys ( u_char *addr)
  79. {
  80. /* implementation for Linux */
  81. struct ifreq ifr;
  82. struct ifreq *IFR;
  83. struct ifconf ifc;
  84. char

    buf[1024];
  85. int s, i;
  86. int ok = 0;
  87. // clear buffer
  88. memset(ifname_buf, 0, sizeof(ifname_buf));
  89. s = socket(AF_INET, SOCK_DGRAM, 0);
  90. if (s==-1) {
  91. return -1;
  92. }
  93. ifc.ifc_len = sizeof(buf);
  94. ifc.ifc_buf = buf;
  95. ioctl(s, SIOCGIFCONF, &ifc);
  96. IFR = ifc.ifc_req;
  97. // put the ioctl interface names in the list
  98. for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; IFR ) {
  99. add_interface_name(IFR->ifr_name);
  100. }
  101. // put the /proc/net/dev interface names in the list
  102. if (get_procnet_list())
  103. return -1;
  104. // get the first mac address of eth* device hardware address

  105. for (i = 0; i < count; i ) {
  106. strcpy(ifr.ifr_name, ifnames i*IFNAMSIZ);
  107. if (!strncmp(ifr.ifr_name, "eth", 3))
  108. if (ioctl(s, SIOCGIFFLAGS, &ifr) == 0) {
  109. if (! (ifr.ifr_flags & IFF_LOOPBACK)) {
  110. if (ioctl(s, SIOCGIFHWADDR, &ifr) == 0) {
  111. char *p = (char *)ifr.ifr_hwaddr.sa_data;
  112. if (!*((int *)p) && !*((int *)(p 2)) )
  113. continue;
  114. // if not 00:00:00:00:00:00, yes, we get the real mac addr
  115. ok = 1;
  116. break;
  117. }
  118. }
  119. }
  120. }
  121. close(s);
  122. if (ok) {
  123. bcopy( ifr.ifr_hwaddr.sa_data, addr, 6);

  124. }
  125. else {
  126. return -1;
  127. }
  128. return 0;
  129. }
  130. /***********************************************************************/
  131. /*
  132. * Main (only for testing)
  133. */
  134. int main( int argc, char **argv)
  135. {
  136. long stat;
  137. int i;
  138. u_char addr[6];
  139. stat = mac_addr_sys( addr);
  140. if (0 == stat) {
  141. printf( "MAC address = ");
  142. for (i=0; i<6; i) {
  143. printf("%2.2x", addr[i]);
  144. if (i<5)
  145. printf(":");
  146. }
  147. printf( "n");
  148. }
  149. else {
  150. fprintf( stderr, "can't get MAC addressn");
  151. exit( 1);
  152. }
  153. return 0;
  154. }

本文出自 「快樂的技術員」 博客,請務必保留此出處http://carltao.blog.51cto.com/856514/415119


[火星人 ] Linux下獲取eth網卡MAC地址的代碼已經有843次圍觀

http://coctec.com/docs/linux/show-post-49599.html