【问题标题】:C# get local MAC address by local IP (multiple interfaces)C#通过本地IP获取本地MAC地址(多个接口)
【发布时间】:2017-10-28 18:45:35
【问题描述】:

我的电脑有 4 个以太网接口(通过 ipconfig /all 获取信息)

例如:
1) IP:192.168.15.161,MAC:00-E2-4C-98-18-89
2)IP:172.168.11.126,MAC:00-FF-4C-98-18-44
3) IP:10.0.13.136,MAC:00-EE-89-98-13-44
4)IP:195.22.18.146,MAC:00-12-89-98-13-33

我需要一个在给定 IP 时返回 MAC 地址的函数。

例如getMacByIP("192.168.15.161") 将返回"00-E2-4C-98-18-89"

使用arp 不适用于本地!

例如arp -a 不会给出带有 MAC 的本地地址
所以像here 这样的所有问题/答案都没有帮助我。

在问这个问题之前,我在互联网上搜索了很多。

编辑:

这个答案:(link) 没有帮助我

public static string GetMacAddressUsedByIp(string ipAddress)
    {
        var ips = new List<string>();
        string output;

        try
        {
            // Start the child process.
            Process p = new Process();
            // Redirect the output stream of the child process.
            p.StartInfo.UseShellExecute = false;

            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.FileName = "ipconfig";
            p.StartInfo.Arguments = "/all";
            p.Start();
            // Do not wait for the child process to exit before
            // reading to the end of its redirected stream.
            // p.WaitForExit();
            // Read the output stream first and then wait.
            output = p.StandardOutput.ReadToEnd();
            p.WaitForExit();

        }
        catch
        {
            return null;
        }

        // pattern to get all connections
        var pattern = @"(?xis) 
(?<Header>
     (\r|\n) [^\r]+ :  \r\n\r\n
)
(?<content>
    .+? (?= ( (\r\n\r\n)|($)) )
)";

        List<Match> matches = new List<Match>();

        foreach (Match m in Regex.Matches(output, pattern))
            matches.Add(m);

        var connection = matches.Select(m => new
        {
            containsIp = m.Value.Contains(ipAddress),
            containsPhysicalAddress = Regex.Match(m.Value, @"(?ix)Physical \s Address").Success,
            content = m.Value
        }).Where(x => x.containsIp && x.containsPhysicalAddress)
        .Select(m => Regex.Match(m.content, @"(?ix)  Physical \s address [^:]+ : \s* (?<Mac>[^\s]+)").Groups["Mac"].Value).FirstOrDefault();

        return connection;
    }

因为如果我使用例如中文版的 Windows,Regex 将不起作用!

【问题讨论】:

  • 不!这是不正确的!所有的答案都没有解决我的问题。他们返回第一个/活动。我想要通过 IP 指定的 MAC。一个答案给出了这个功能,但这不是一个好的答案!因为如果我的 windows 不是英文的,它就无法工作!
  • 您没有阅读所有答案,也没有尝试了解此类问题的复杂性。还有很多其他事情需要考虑,例如分配给接口的地址数量,以及您尝试匹配的地址类型等。
  • 我已经一一阅读了所有答案并进行了测试!!他们都不是我的好解决方案!请删除重复的
  • 我刚刚更新了我的答案以匹配您的问题

标签: c# ip localhost mac-address arp


【解决方案1】:

这是一个可以满足您要求的建议解决方案:

void Main()
{
    Console.WriteLine(GetMacByIP("192.168.15.161")); // will return "00-E2-4C-98-18-89"
}

public string GetMacByIP(string ipAddress)
{
    // grab all online interfaces
    var query = NetworkInterface.GetAllNetworkInterfaces()
        .Where(n =>
            n.OperationalStatus == OperationalStatus.Up && // only grabbing what's online
            n.NetworkInterfaceType != NetworkInterfaceType.Loopback)
        .Select(_ => new
        {
            PhysicalAddress = _.GetPhysicalAddress(),
            IPProperties = _.GetIPProperties(),
        });

    // grab the first interface that has a unicast address that matches your search string
    var mac = query
        .Where(q => q.IPProperties.UnicastAddresses
            .Any(ua => ua.Address.ToString() == ipAddress))
        .FirstOrDefault()
        .PhysicalAddress;

    // return the mac address with formatting (eg "00-00-00-00-00-00")
    return String.Join("-", mac.GetAddressBytes().Select(b => b.ToString("X2")));
}

【讨论】:

  • 你为什么使用 q.IPProperties.UnicastAddresses?为什么需要具有单播地址的接口?
  • @cheziHoyzer - 这是我之前试图警告你的。这并不总是那么简单。您有AnycastUnicastMulticast,了解更多信息...msdn.microsoft.com/en-us/library/…
猜你喜欢
  • 1970-01-01
  • 2022-09-29
  • 1970-01-01
  • 2011-10-09
  • 2011-10-11
  • 1970-01-01
  • 2016-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多