获取远程机器名与Mac地址的代码

#include "stdio.h" 
#include "stdlib.h" 
#include "Winsock2.h" 
#include "iphlpapi.h" 
  
#pragma comment ( lib, "ws2_32.lib" ) 
#pragma comment ( lib, "Iphlpapi.lib" ) 
  
void main( int argc, char ** argv ) 

 //处理命令行参数 
 if ( argc != 2 ) 
 { 
 printf( "RmtHost v0.1 - Get remote HostName /MacAddress/n" ); 
 printf( "by ShotgunLabs ( 
Shotgun@xici.net  )/n/n" ); 
 printf( "Usage :/n/tRmtHost.exe [RemoteIP]/n/n"); 
 printf( "Example:/n/tRmtHost.exe 192.168.0.3/n/n"); 
 exit( 0 ); 
 } 
  
 printf( "/nIpAddress : %s/n", argv[1] ); 
 //初始化SOCKET 
 WSADATA wsaData; 
 int iRet = WSAStartup(MAKEWORD(2,1), &wsaData); 
 if ( iRet != 0 ) 
 { 
 printf( "WSAStartup Error:%d/n", GetLastError() ); 
 exit( 0 ); 
 } 
 //获取远程机器名 
 hostent * remoteHostent = (hostent*)malloc( sizeof( hostent )); 
 int nRemoteAddr = inet_addr( argv[1] ); 
 remoteHostent = gethostbyaddr( (char*)&nRemoteAddr,sizeof( in_addr ), AF_INET ); 
 if ( remoteHostent ) 
 printf( "HostName : %s/n",remoteHostent->h_name ); 
 else  
 printf( "gethostbyaddr Error:%d/n", GetLastError()); 
  
 //发送ARP查询包获得远程MAC地址 
 unsigned char macAddress[6];  
 ULONG macAddLen = 6;  
 iRet = SendARP( nRemoteAddr, NULL, (PULONG)&macAddress,&macAddLen ); 
 if ( iRet == NO_ERROR ) 
 { 
 printf( "MacAddress: " ); 
 for( int i =0; i<6; i++ )  
 { 
  printf( "%.2x", macAddress ); 
  if ( i<5 ) printf( "-" ); 
 } 
 } 
 else  
 printf( "SendARP Error:%d/n", GetLastError() ); 
 printf( "/n" ); 
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值