public string GetMacAddress(string ipAddress)
{
string macAddress = string.Empty;
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
pProcess.StartInfo.FileName = "arp";
pProcess.StartInfo.Arguments = "-a " + ipAddress;
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.CreateNoWindow = true;
pProcess.Start();
string strOutput = pProcess.StandardOutput.ReadToEnd();
string[] substrings = strOutput.Split('-');
if (substrings.Length >= 8)
{
macAddress = substrings[3].Substring(Math.Max(0, substrings[3].Length - 2))
+ "-" + substrings[4] + "-" + substrings[5] + "-" + substrings[6]
+ "-" + substrings[7] + "-"
+ substrings[8].Substring(0, 2);
return macAddress;
}
else
{
return "not found";
}
}
Rất muộn chỉnh sửa: Trong dự án souce mở iSpy (https://github.com/ispysoftware/iSpy) họ sử dụng mã này, đó là một chút đẹp hơn
public static void RefreshARP()
{
_arpList = new Dictionary<string, string>();
_arpList.Clear();
try
{
var arpStream = ExecuteCommandLine("arp", "-a");
// Consume first three lines
for (int i = 0; i < 3; i++)
{
arpStream.ReadLine();
}
// Read entries
while (!arpStream.EndOfStream)
{
var line = arpStream.ReadLine();
if (line != null)
{
line = line.Trim();
while (line.Contains(" "))
{
line = line.Replace(" ", " ");
}
var parts = line.Trim().Split(' ');
if (parts.Length == 3)
{
string ip = parts[0];
string mac = parts[1];
if (!_arpList.ContainsKey(ip))
_arpList.Add(ip, mac);
}
}
}
}
catch (Exception ex)
{
Logger.LogExceptionToFile(ex, "ARP Table");
}
if (_arpList.Count > 0)
{
foreach (var nd in List)
{
string mac;
ARPList.TryGetValue(nd.IPAddress.ToString(), out mac);
nd.MAC = mac;
}
}
}
https://github.com/ispysoftware/iSpy/blob/master/Server/NetworkDeviceList.cs
Cập nhật 2 thậm chí nhiều hơn trễ, nhưng tôi nghĩ là tốt nhất vì nó sử dụng regex để kiểm tra tốt hơn cho các trận đấu chính xác.
public string getMacByIp(string ip)
{
var macIpPairs = GetAllMacAddressesAndIppairs();
int index = macIpPairs.FindIndex(x => x.IpAddress == ip);
if (index >= 0)
{
return macIpPairs[index].MacAddress.ToUpper();
}
else
{
return null;
}
}
public List<MacIpPair> GetAllMacAddressesAndIppairs()
{
List<MacIpPair> mip = new List<MacIpPair>();
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
pProcess.StartInfo.FileName = "arp";
pProcess.StartInfo.Arguments = "-a ";
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.CreateNoWindow = true;
pProcess.Start();
string cmdOutput = pProcess.StandardOutput.ReadToEnd();
string pattern = @"(?<ip>([0-9]{1,3}\.?){4})\s*(?<mac>([a-f0-9]{2}-?){6})";
foreach (Match m in Regex.Matches(cmdOutput, pattern, RegexOptions.IgnoreCase))
{
mip.Add(new MacIpPair()
{
MacAddress = m.Groups["mac"].Value,
IpAddress = m.Groups["ip"].Value
});
}
return mip;
}
public struct MacIpPair
{
public string MacAddress;
public string IpAddress;
}
Nguồn
2013-10-08 09:40:34
Không chắc chắn nếu nó hoạt động, nhưng với một tìm kiếm google nhanh chóng tôi tìm thấy thư viện này mà nên làm các trick: [http: // www. tamirgal.com/blog/post/ARP-Resolver-C-Class.aspx](http://www.tamirgal.com/blog/post/ARP-Resolver-C-Class.aspx) –
Cảm ơn bạn, tôi tin rằng tôi đã đọc bảng ARP không nhất quán và đã tự hỏi nếu có một cách để 'ping' cho địa chỉ MAC. –
Tôi ** nghĩ rằng nếu bạn thực hiện ping bình thường (hoặc cố gắng liên lạc) địa chỉ IP, nó sẽ làm cho các bảng ARP làm mới (nếu không ngăn xếp mạng sẽ không thể liên lạc với máy ngay từ đầu); tất nhiên điều này sẽ (nếu có) chỉ hoạt động nếu máy mong muốn trực tuyến. Tôi không nghĩ rằng bạn có thể nhận được kết quả đáng tin cậy cho các địa chỉ IP ngoại tuyến, đặc biệt nếu bạn có các IP được gán động. Tôi không phải là chuyên gia mạng, vì vậy tôi có thể sai (cố gắng suy nghĩ với bạn về vấn đề này). –