使用 SendARP 查询 MAC 地址
使用 SendARP 查询 MAC 地址
通过 IP Helper API 中的 SendARP 发送 ARP 请求来获取 MAC 地址,简单方便,缺点是不能跨越网关。
C++代码
#include
#include
#include
#pragma comment(lib , "IpHlpApi.lib")
#pragma comment(lib , "WS2_32.lib")
int main(int argc,char * argv[])
{
int iReturn;
DWORD dwIP;
BYTE byMAC[6];
DWORD dwLen;
WSADATA WsaData;
WSAStartup(MAKEWORD(2, 0), &WsaData);
__try
{
printf("IP2MAC Ver 1.0 -- Get MAC address from IP address though ARP request.
");
if (argc != 2)
{
printf("Usage: %s
", argv[0]);
__leave;
}
dwIP = inet_addr(argv[1]);
if (dwIP == INADDR_NONE)
{
printf("Invalid IP address: %s.
", argv[1]);
__leave;
}
// 发送ARP查询包获得 MAC 地址
dwLen = 6;
iReturn = SendARP(dwIP, 0, (PULONG) &byMAC, &dwLen);
if (iReturn != NO_ERROR)
{
printf("Error: Send ARP request.
Be sure that connectting to %s need no gateway.
", argv[1]);
__leave;
}
printf("%.2X-%.2X-%.2X-%.2X-%.2X-%.2X
",
byMAC[0],
byMAC[1],
byMAC[2],
byMAC[3],
byMAC[4],
byMAC[5]);
}
__finally
{
WSACleanup();
}
return 0;
}
补充:综合编程 , 安全编程 ,