1

Is there any way to get the IP (or the hostname) that corresponds to a specific MAC address?

What I have found so far is to get the hostname from an IP and vice versa using the System.Net.Dns.

Community
  • 1
  • 1
Chris
  • 1,610
  • 3
  • 18
  • 37

2 Answers2

4

You can use this class

public class IPMacMapper
{
    private static List<IPAndMac> list;

    private static StreamReader ExecuteCommandLine(String file, String arguments = "")
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.CreateNoWindow = true;
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardOutput = true;
        startInfo.FileName = file;
        startInfo.Arguments = arguments;

        Process process = Process.Start(startInfo);

        return process.StandardOutput;
    }

    private static void InitializeGetIPsAndMac()
    {
        if (list != null)
            return;

        var arpStream = ExecuteCommandLine("arp", "-a");
        List<string> result = new List<string>();
        while (!arpStream.EndOfStream)
        {
            var line = arpStream.ReadLine().Trim();
            result.Add(line);
        }

        list = result.Where(x => !string.IsNullOrEmpty(x) && (x.Contains("dynamic") || x.Contains("static")))
            .Select(x =>
            {
                string[] parts = x.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                return new IPAndMac { IP = parts[0].Trim(), MAC = parts[1].Trim() };
            }).ToList();
    }

    public static string FindIPFromMacAddress(string macAddress)
    {
        InitializeGetIPsAndMac();
        IPAndMac item = list.SingleOrDefault(x => x.MAC == macAddress);
        if (item == null)
            return null;
        return item.IP;
    }

    public static string FindMacFromIPAddress(string ip)
    {
        InitializeGetIPsAndMac();
        IPAndMac item = list.SingleOrDefault(x => x.IP == ip);
        if (item == null)
            return null;
        return item.MAC;
    }

    private class IPAndMac
    {
        public string IP { get; set; }
        public string MAC { get; set; }
    }
}

and use it as

string ipAddress = IPMacMapper.FindIPFromMacAddress("some-mac-address");
string macAddress = IPMacMapper.FindMacFromIPAddress("some-ip-address");
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
0

What you're looking for is reading the computer's ARP list.
Then parse it to find the requested MAC address... Notice that arp -a has both the IP and MAC so you just need to match the requested one.

Here is an example of how it is done in C#

Blachshma
  • 17,097
  • 4
  • 58
  • 72
  • Nope, strictly speaking ARP is getting MAC by given IP, @Chris wants to have IP by given MAC. – om-nom-nom Mar 12 '13 at 10:14
  • 3
    Reading the ARP cache to retrieve an IP address by MAC address will only work if the PC has already communicated with the IP address and resolve it to a MAC address. If it hasn't the cache won't have either address. –  May 06 '15 at 09:25