程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> VC >> vc教程 >> 得到遠程機器MAC地址

得到遠程機器MAC地址

編輯:vc教程

#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 ( [email protected] ) " );
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; i<6; i++ )
{
printf( "%.2x", MacAddress[i] );
if ( i<5 ) printf( "-" );
}
printf( " " );
}
else
printf( "SendARP Error:%d ", GetLastError());
nRemoteAddr = htonl( ntohl( nRemoteAddr ) + 1 );
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved