2017-05-28 266 views
1

我有4個以太網接口,在我的電腦(獲得由ipconfig信息/全部)C#獲取本地IP(多個接口)本地MAC地址

例如:
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,正則表達式將無法正常工作!

+1

[可靠的方法,在C#中獲取機器的MAC地址(的可能的複製https://stackoverflow.com/questions/850650/可靠的方法來獲取機器mac地址在c-sharp) – Svek

+0

不!這是不正確的!所有的答案都沒有解決我的問題。他們返回第一個/主動。我想通過IP來獲得特定的MAC。一個答案給這個功能,但它不是很好的答案!因爲如果我的窗戶不是英文的,它不會起作用! – cheziHoyzer

+0

您沒有閱讀所有答案,或試圖瞭解這類問題的複雜性。還有很多其他的事情需要考慮,比如分配給一個接口的地址的數量,以及你試圖匹配的地址的類型等。 – Svek

回答

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"))); 
} 
+0

你爲什麼使用q.IPProperties.UnicastAddresses?爲什麼你需要具有單播地址的接口? – cheziHoyzer

+0

@cheziHoyzer - 這正是我之前試圖提醒你的。這並不總是很簡單。你有'Anycast','Unicast','Multicast',更多信息... https://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipinterfaceproperties(v=vs.110)的.aspx – Svek