Wes*_*ler 5 c c++ linux mac-address busybox
如何在我的(小型)本地网络上使用C或C++ PROGRAM(无命令行)来获取MAC地址(如果它们"免费",我也将获取IP地址).它是一个嵌入式Busybox Linux,所以我需要一个极简主义的答案,希望不需要移植一些库.我没有libnet或libpcap.如果DHCP主机,arp缓存似乎永远不会包含MAC.
打开/proc/net/arp
,然后读取每行如下:
char line[500]; // Read with fgets().
char ip_address[500]; // Obviously more space than necessary, just illustrating here.
int hw_type;
int flags;
char mac_address[500];
char mask[500];
char device[500];
FILE *fp = xfopen("/proc/net/arp", "r");
fgets(line, sizeof(line), fp); // Skip the first line (column headers).
while(fgets(line, sizeof(line), fp))
{
// Read the data.
sscanf(line, "%s 0x%x 0x%x %s %s %s\n",
ip_address,
&hw_type,
&flags,
mac_address,
mask,
device);
// Do stuff with it.
}
fclose(fp);
Run Code Online (Sandbox Code Playgroud)
这是直接从BusyBox的arp实现,在BusyBox 1.21.0 tarball的busybox-1_21_0/networking/arp.c
目录中.特别是看功能.arp_show()
如果你害怕C:
该命令arp -a
应该为您提供所需的MAC地址和IP地址.
要获取子网上的所有MAC地址,您可以尝试
nmap -n -sP <subnet>
arp -a | grep -v incomplete
Run Code Online (Sandbox Code Playgroud)