Sprache: C#
Die MAC-Adresse (Media Access Control, ist die Hardware-Adresse jedes einzelnen Netzwerkadapters, die zur eindeutigen Identifizierung des Geräts im Netzwerk dient.
(Quelle Wikipedia )
Dieser Snippet nützt von der "iphlpapi.dll" die Funktion SendARP um die MAC Adresse als byte-array zu erhalten.
[DllImport("iphlpapi.dll", ExactSpelling = true)]
public static extern int SendARP(int DestIP, int SrcIP, [Out] byte[] pMacAddr, ref int PhyAddrLen);
/// <summary>
/// Ermittelt die MAC Adresse eines im Netzwerk befindlichen Computers
/// </summary>
/// <param name="ip">Die IP des Netzwerkcomputers</param>
/// <returns>Die MAC Adresse im string format</returns>
private string GetMACAdress(IPAddress ip)
{
try
{
byte[] ab = new byte[6];
int len = ab.Length;
int r = SendARP((int)ip.Address, 0, ab, ref len);
string mac = BitConverter.ToString(ab, 0, 6);
return mac;
}
catch (Exception)
{
return "MAC not available";
}
}
[DllImport("iphlpapi.dll", ExactSpelling = true)]
public static extern int SendARP(int DestIP, int SrcIP, [Out] byte[] pMacAddr, ref int PhyAddrLen);
/// <summary>
/// Ermittelt die MAC Adresse eines im Netzwerk befindlichen Computers
/// </summary>
/// <param name="ip">Die IP des Netzwerkcomputers</param>
/// <returns>Die MAC Adresse im string format</returns>
private string GetMACAdress(IPAddress ip)
{
try
{
byte[] ab = new byte[6];
int len = ab.Length;
int r = SendARP((int)ip.Address, 0, ab, ref len);
string mac = BitConverter.ToString(ab, 0, 6);
return mac;
}
catch (Exception)
{
return "MAC not available";
}
}
Alte URL:
/snippet/mac-adresse-eines-netzwerkcomputers-mithilfe-der-ip-adresse/905
Erst als ich es veröffentlicht habe, habe ich gesehen das der völlig gleiche Snippet schon hier veröffentlicht wurde.
Sorry !
Macht nichts 🙂
Als Anmerkung:
Dies funktioniert nur im eigenen Netz. Das ARP-Protokoll ist nicht routingsfähig!