windows C#中同一局域网上的MAC地址到IP地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2204318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:53:25  来源:igfitidea点击:

MAC address to IP Address on Same LAN in C#

c#windowsnetworkingtcp

提问by Kazoom

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

有没有办法在 C# 中找到 MAC 地址到 IP 地址之间的映射。我认为 RARP 应该能够做到这一点,C# 中是否有可用的 API

回答by t0mm13b

Why not spawn a process to invoke rarpand 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:

为什么不产生一个进程来调用rarp和从进程的输出中读取输入流?这是一种真正便宜的简单而愉快的方法……在我的脑海中,它是这样的:

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 sResultsto get the MAC address.

然后就是解析sResults得到MAC地址的问题。

回答by Hakan F?st?k

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 == macAddress).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");

回答by Rahul Singh

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

如果您正在寻找基于 API 的方法并且无法执行 Process.Start() ,请查看以下内容:

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

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

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

它允许映射主机名、IP 地址和 MAC 地址。