下面图老师小编要向大家介绍下得到远程机器MAC地址源代码,看起来复杂实则是简单的,掌握好技巧就OK,喜欢就赶紧收藏起来吧!
【 tulaoshi.com - 编程语言 】
#include
#include
#include
#include "iphlpapi.h"
#pragma comment ( lib, "ws2_32.lib" )
#pragma comment ( lib, "Iphlpapi.lib" )
void main( int argc, char ** argv )
{
int numberOfHost = 1;
struct hostent *remoteHostent;
//处理命令行参数
if ( argc == 3 )
numberOfHost = atoi( argv[2] );
if ( ( argc 3 ) || ( argc 2 ) )
{
printf( "RmtHost v0.2 - Get remote HostName /MacAddress" );
printf( "by ShotgunLabs ( Shotgun@xici.net )" );
printf( "Usage :RmtHost.exe [RemoteIP]" );
printf( "Example:RmtHost.exe 192.168.0.3" );
printf( "RmtHost.exe 192.168.0.3 255" );
exit( 0 );
}
//初始化SOCKET
WSADATA wsaData;
int iRet = WSAStartup(MAKEWORD(2,1), &wsaData);
if ( iRet != 0 )
{
printf( "WSAStartup Error:%d", GetLastError() );
exit( 0 );
}
int nRemoteAddr = inet_addr( argv[1] );
remoteHostent= (struct hostent*)malloc( sizeof(struct hostent ));
struct in_addr sa;
for ( int i = 0; i numberOfHost; i ++ )
{
//获取远程机器名
sa.s_addr = nRemoteAddr;
printf( "IpAddress : %s", inet_ntoa( sa ) );
remoteHostent = gethostbyaddr( (char*)&nRemoteAddr,4, AF_INET );
if ( remoteHostent )
printf( "HostName : %s",remoteHostent-h_name );
else
printf( "gethostbyaddr Error:%d",GetLastError() );
//发送ARP查询包获得远程MAC地址
unsigned char macAddress[6];
ULONG macAddLen = 6;
iRet=SendARP(nRemoteAddr, (unsigned long)NULL,(PULONG)&macAddress, &macAddLen);
if ( iRet == NO_ERROR )
{
printf( "MacAddress: " );
for( int i =0; i6; i++ )
{
printf( "%.2x", macAddress[i] );
if ( i5 ) printf( "-" );
}
printf( "" );
}
else
printf( "SendARP Error:%d", GetLastError());
nRemoteAddr = htonl( ntohl( nRemoteAddr ) + 1 );
}
}
来源:http://www.tulaoshi.com/n/20160219/1605868.html
看过《得到远程机器MAC地址源代码》的人还看了以下文章 更多>>