SoFunction
Updated on 2025-03-07

Use C# to write a method to obtain remote IP and MAC

If you want to obtain a remote address, you need to use the sendarp function to implement it. The specific code is as follows:
[DllImport("")] 
private static unsafe extern int SendARP(Int32 dest,Int32 host,ref IntPtr mac,ref IntPtr length); 
[DllImport("Ws2_32.dll")] 
private static extern Int32 inet_addr(string ip); 

Int32 ldest= inet_addr("157.60.68.163");//Destination ip
Int32 lhost= inet_addr("157.60.68.33");//Local ip

try 

Byte[] macinfo=new Byte[6]; 
Int32 length=6; 

IntPtr mac=new IntPtr(macinfo[0]); 
IntPtr len=new IntPtr(6); 
int ii=SendARP(ldest,lhost, ref mac, ref len); 

("Mac Add:"+mac); 
("length:"+len); 



catch(Exception err) 

(err);