#include <windows.h>
#include <stdio.h>
#include <iphlpapi.h>
#include <winsock.h>
#pragma comment(lib, "ws2_32")
#pragma comment(lib, "iphlpapi")
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
return -1;
}
HRESULT hr;
ULONG pulMac[2];
ULONG ulLen;
in_addr beginaddr, endaddr; //
unsigned long be, en;
char badd[16]={0};
char eadd[16]={0};
char add[16]={0};
char name[30]={0};
hostent *pht=NULL;
printf("输入起始ip地址:");
scanf("%16s",badd);
printf("输入终止ip地址:");
scanf("%16s",eadd);
beginaddr.S_un.S_addr = inet_addr(badd);
endaddr.S_un.S_addr = inet_addr(eadd);
be = ntohl(beginaddr.S_un.S_addr);
en = ntohl(endaddr.S_un.S_addr);
printf("%-18s %-20s %-20s/n", "IP", "NAME", "MAC");
while(be<=en)
{
ulLen = 6;
hr = SendARP(beginaddr.S_un.S_addr, 0, pulMac, &ulLen);
if(hr == NO_ERROR)
{
memcpy(add,inet_ntoa(beginaddr),16);
pht = gethostbyaddr((char*)&beginaddr, sizeof(beginaddr), AF_INET);
if (pht != NULL)
{
size_t i, j;
char * szMac = new char[ulLen*3];
PBYTE pbHexMac = (PBYTE) pulMac;
for (i = 0, j = 0; i < ulLen - 1; ++i) {
j += sprintf (szMac + j, "%02X:", pbHexMac[i]);
}
sprintf (szMac + j, "%02X", pbHexMac[i]);
memcpy(name, pht->h_name, 30);
printf("%-18s %-20s %-20sis online./n", add, name, szMac);
delete [] szMac;
}
be++;
beginaddr.S_un.S_addr = htonl(be);
}
else
{
be++;
beginaddr.S_un.S_addr = htonl(be);
continue;
}
}
WSACleanup( );
system("pause");
return 0;
}