我想在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