我想在Linux中使用C程序来查找MAC地址。怎么做?搜索谷歌通过量的如何使用C程序在Linux中获取接口的MAC地址?
6
A
回答
26
每分钟:(我还没有测试它自己,我的工作在Windows机器上的那一刻)
/*
* gethwaddr.c
*
* Demonstrates retrieving hardware address of adapter using ioctl()
*
* Author: Ben Menking <[email protected]>
*
*/
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
int main(int argc, char *argv[])
{
int s;
struct ifreq buffer;
s = socket(PF_INET, SOCK_DGRAM, 0);
memset(&buffer, 0x00, sizeof(buffer));
strcpy(buffer.ifr_name, "eth0");
ioctl(s, SIOCGIFHWADDR, &buffer);
close(s);
for(s = 0; s < 6; s++)
{
printf("%.2X ", (unsigned char)buffer.ifr_hwaddr.sa_data[s]);
}
printf("\n");
return 0;
}
2
有一个伟大的图书馆管理以太网。 如果你想要去低层次的东西,它肯定值得学习。 学习C API非常困难。
Lib PCAP。
一些示例代码:
#include <pcap.h>
#include <stdlib.h>
#include <netinet/ip.h>
#include <netinet/if_ether.h>
void find_eth_addr(struct in_addr *search_ip, const struct pcap_pkthdr* pkthdr, const u_char *packet) {
struct ether_header *eth_hdr = (struct ether_header *)packet;
if (ntohs(eth_hdr->ether_type) == ETHERTYPE_IP) {
struct ip *ip_hdr = (struct ip *)(packet + sizeof(struct ether_header));
if (ip_hdr->ip_dst.s_addr == search_ip->s_addr)
print_eth_addr(eth_hdr->ether_dhost);
if (ip_hdr->ip_src.s_addr == search_ip->s_addr)
print_eth_addr(eth_hdr->ether_shost);
}
}
还有不错的 “内核函数包装” 像库: DNET
它提供了强大的功能把它用在低级别的网络。 (也获取MAC地址)。
有两个库UNIX &胜利端口。
相关问题
- 1. 如何使用C程序获取机器的MAC地址?
- 2. 如何在android中获取WIFI接口的MAC地址?
- 3. 如何使用C#获取IP地址的物理(MAC)地址?
- 4. C#获取本地IP(多个接口)本地MAC地址
- 5. 使用pcap获取linux中的接口的ip地址
- 6. 如何在Linux下获取接口的IPv6地址
- 7. 如何使用findstr获取MAC地址?
- 8. 如何在C程序中从linux中的IPv6地址获取网络接口名称?
- 9. 如何获取WiFi网络接口的MAC地址?
- 10. 使用Python和Linux获取远程MAC地址
- 11. 获取MAC地址C#
- 12. 如何在Windows7中获取MAC地址?
- 13. 如何从驱动程序代码中提取接口的MAC地址
- 14. 如何在linux中使用java程序获取机器的ip地址
- 15. 在linux下使用单声道获取MAC地址
- 16. 使用Java小程序在网页上获取MAC地址
- 17. 如何在ios中获取接入点的mac地址?
- 18. 如何在java中获取接入点的MAC地址?
- 19. 在Windows 8应用程序中获取MAC地址
- 20. C#使用Mono获取MAC地址(适用于Mac平台)
- 21. 如何在Firefox中使用JavaScript获取MAC地址?
- 22. 获取MAC地址
- 23. 如何使用libnet获取接口的ipv6地址?
- 24. 如何在Android应用程序中使用mac地址
- 25. 如何获取C#中的外部IP的mac地址?
- 26. 如何获取phonegap的MAC地址?
- 27. Python:在Linux中获取本地接口/ IP地址的默认网关
- 28. 从FreeBSD上的GUID获取MAC地址/接口名称?
- 29. 使用C获取程序集的标签地址C
- 30. 使用VBScript获取Mac地址
无论如何得到它没有硬编码“eth0”?? – 2013-06-25 09:12:00
你必须分配一个网络适配器,否则没有MAC地址,你可以通过输入或参数来实现,但你需要一个适配器。 – 2014-12-03 23:22:19
#include for close() –
notlesh
2015-04-01 18:18:10