免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3377 | 回复: 4
打印 上一主题 下一主题

linux下如何用c取得指定网卡的mac地址? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-10-05 18:25 |只看该作者 |倒序浏览
linux下如何用c取得指定网卡的mac地址?
就是这个问题!谢谢

论坛徽章:
0
2 [报告]
发表于 2005-10-06 03:10 |只看该作者

linux下如何用c取得指定网卡的mac地址?

  1. #include <stdio.h>;
  2. #include <unistd.h>;
  3. #include <string.h>;
  4. #include <sys/ioctl.h>;
  5. #include <sys/socket.h>;
  6. #include <net/if.h>;

  7. int main(){
  8.         int sockfd, i;
  9.         struct ifreq buffer;
  10.         
  11.         sockfd = socket(PF_INET,SOCK_DGRAM,0);
  12.         if (sockfd <= 0) {
  13.                 fputs("Can not create socket!\n", stderr);
  14.                 return 1;
  15.         }      

  16.         memset(&buffer, 0, sizeof(struct ifreq));
  17.         strcpy(buffer.ifr_name, "eth0");
  18.         ioctl(sockfd, SIOCGIFHWADDR, &buffer);
  19.         close(sockfd);

  20.         fputs("Network MAC address: ", stdout);
  21.         for(i = 0; i < 6; i++)
  22.                 printf("%.2x:", (unsigned char)buffer.ifr_hwaddr.sa_data[i]);
  23.         putchar('\n');

  24.         return 0;
  25. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2005-10-08 09:26 |只看该作者

linux下如何用c取得指定网卡的mac地址?

十分感谢,如果是专为这个问题写的,那么致以十万份的感谢

论坛徽章:
0
4 [报告]
发表于 2005-10-08 13:13 |只看该作者

linux下如何用c取得指定网卡的mac地址?

libnet有个libnet_get_hwaddr()函数

论坛徽章:
0
5 [报告]
发表于 2005-10-09 10:27 |只看该作者

linux下如何用c取得指定网卡的mac地址?

通过楼上兄弟的提示找到了不少好东西,谢了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP