2

Is there a way to find mapping between MAC address to IP address in C#. i think RARP should be able to do that, is there an API available in C# for that

Kazoom
  • 5,659
  • 16
  • 56
  • 69
  • Duplicate question: http://stackoverflow.com/questions/1148778/how-do-i-access-arp-protocol-information-through-c-net – mjv Feb 05 '10 at 00:53
  • That is _not_ RARP, which is for a host to request its own IP address, but it is an obsolete protocol. – Ron Maupin Mar 15 '18 at 17:10
  • Possible duplicate of [How do I access ARP-protocol information through .NET?](https://stackoverflow.com/questions/1148778/how-do-i-access-arp-protocol-information-through-net) – Ron Maupin Mar 15 '18 at 17:11

3 Answers3

2

Why not spawn a process to invoke rarp and read in the input stream from the process's output? That's a real cheap simple and cheerful way of doing it...top-of-my-head, it goes something like this:

System.Diagnostics.ProcessStartInfo ps = new System.Diagnostics.ProcessStartInfo("arp", "-a");
ps.CreateNoWindow = false;
ps.RedirectStandardOutput = true;
using (System.Diagnostics.Process proc = new System.Diagnostics.Process())
{
    proc.StartInfo = ps;
    proc.Start();
    System.IO.StreamReader sr = proc.StandardOutput;
    while (!proc.HasExited) ;
    string sResults = sr.ReadToEnd();
}

Then it's a matter of parsing the sResults to get the MAC address.

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
t0mm13b
  • 34,087
  • 8
  • 78
  • 110
1

You can use this class

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

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();
        return list.SingleOrDefault(x => x.MAC.ToLower() == macAddress.ToLower()).IP;
    }

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

and use it as

var ipAddress = IPMacMapper.FindIPFromMacAddress("mac-address");
var macAddress = IPMacMapper.FindMacFromIPAddress("ip-address");
staafl
  • 3,147
  • 1
  • 28
  • 23
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
-1

In case you are looking for an API based approach and are not able to do a Process.Start() take a look at this:

http://www.codeproject.com/KB/IP/host_info_within_network.aspx

It allows mapping of hostname, IP address and MAC address.

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228