0

I am writing a Windows desktop app that communicates with my DSLR camera via Wireless network (WLAN). I need a way to scan the network and find the camera by its MAC address or other identifying information. Nirsoft Wireless Network Watcher displays information about each device on my network. (Specifically: 'IP Address', 'Device Name', 'MAC Address', 'Network Adapter Company', etc.) Does .NET provide a way to retrieve this information from C#?

JNygren
  • 179
  • 1
  • 2
  • 20

1 Answers1

1

You can check out the following blog where someone already wrote a code for something like this. https://www.maniuk.net/2016/08/get-ip-address-by-mac-address-in-csharp.html

This uses the command line arp command as an external process. I have tested it and seems to be working ok. As the code stands the mac address has to be in the format aa-bb-cc-dd-ee-ff

Alternatively, there is a Nuget Package called ArpLookup. https://www.nuget.org/packages/ArpLookup/

With this one, you have to provide the IP Address and it will return the mac address. So you would have to enumerate through your IP range and return the one that matches the mac address.

Something like this:

private static async Task<string> GetIPFromMAC(string macAddress)
{
    var ping = new Ping();

    var start = BitConverter.ToInt32(new byte[] { 1, 0, 168, 192 }, 0);
    var end = BitConverter.ToInt32(new byte[] { 254, 0, 168, 192 }, 0);

    for (var i = start; i <= end; i++)
    {
        var bytes = BitConverter.GetBytes(i);
        var ipAddress = new IPAddress(new[] {bytes[3], bytes[2], bytes[1], bytes[0]});
        var reply = ping.Send(ipAddress, 200);

        if (reply == null || reply.Status == IPStatus.TimedOut) continue;
        
        var mac = await Arp.LookupAsync(ipAddress);

        if (macAddress == mac.ToString()) return ipAddress.ToString();
    }

    return string.Empty;
}

You need to replace the values in start and end with your IP range and then you can call the code like this.

var ip = GetIPFromMAC("aabbccddeeff").Result;
George
  • 32
  • 5