1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| [DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip);
[DllImport("Iphlpapi.dll")]
private static extern int SendARP (Int32 dest,Int32 host,ref Int64 mac,ref Int32 len);
static void Main(string[] args)
{
return;
}
public static Int64 getRemoteMAC(string localIP,string remoteIP)
{
Int32 ldest = inet_addr(remoteIP);
Int32 lhost = inet_addr(localIP);
try
{
Int64 macinfo = 0;
Int32 len = 6;
int res = SendARP(ldest, 0, ref macinfo, ref len);
return macinfo;
}
catch(Exception e)
{
return 0;
}
}
public static string formatMac(Int64 mac, char separator)
{
if (mac <= 0)
return "-";
char[] oldmac = Convert.ToString(mac, 16).PadLeft(12, '0').ToCharArray();
System.Text.StringBuilder newMac = new System.Text.StringBuilder(17);
if (oldmac.Length < 12)
return "00-00-00-00-00-00";
newMac.Append(oldmac[10]);
newMac.Append(oldmac[11]);
newMac.Append(separator);
newMac.Append(oldmac[8]);
newMac.Append(oldmac[9]);
newMac.Append(separator);
newMac.Append(oldmac[6]);
newMac.Append(oldmac[7]);
newMac.Append(separator);
newMac.Append(oldmac[4]);
newMac.Append(oldmac[5]);
newMac.Append(separator);
newMac.Append(oldmac[2]);
newMac.Append(oldmac[3]);
newMac.Append(separator);
newMac.Append(oldmac[0]);
newMac.Append(oldmac[1]);
return newMac.ToString();
} |
Partager