使用 "arp -a" 检索相应 IP 地址的 MAC 地址
Use "arp -a" to retreive MAC address of corresponding IP Adress
我试图将 phone 的 MAC 地址转换为它的 IP 地址。
var arpStream = ExecuteCommandLine("arp", "-a");
List<string> result = new List<string>();
while (!arpStream.EndOfStream)
{
var line = arpStream.ReadLine().Trim();
result.Add(line);
}
使用上面的代码,我将其存储在以下形式的列表中:
192.168.137.1 2e-bb-58-0a-2f-34 dynamic
192.168.137.44 a8-3e-0e-61-3f-db dynamic
192.168.137.91 d4-63-c6-b2-ac-38 dynamic
224.0.0.22 01-00-5e-00-00-16 static
224.0.0.252 01-00-5e-00-00-fc static
我想不通的是,如何为给定的 MAC 检索特定的 IP。
假设我的 phone 是具有物理地址的设备:a8-3e-0e-61-3f-db,我如何将它的 IP 作为字符串存储在某处?
我假设您已经以某种方式获得了字符串列表(ExecuteCommandLine
方法)并且希望能够根据 arp 值对其进行过滤。正则表达式可以是一个选项:
void Main()
{
// just setting it up for testing
List<string> result = new List<string>();
result.Add("192.168.137.1 2e-bb-58-0a-2f-34 dynamic");
result.Add("192.168.137.44 a8-3e-0e-61-3f-db dynamic");
result.Add("224.0.0.22 01-00-5e-00-00-16 static");
result.Add("224.0.0.22 01-00-5e-00-00-16 static");
result.Add("192.168.137.91 d4-63-c6-b2-ac-38 dynamic");
result.Add("224.0.0.22 01-00-5e-00-00-16 static");
result.Add("224.0.0.252 01-00-5e-00-00-fc static");
// this is the part you want
ConcurrentDictionary<string,string> arps = new ConcurrentDictionary<string, string>();
foreach (var s in result)
{
var matches = Regex.Match(s, @"((?:\d+\.?){4})\s+((?:[0-9a-f]{2}-?){6}).*");
arps.TryAdd(matches.Groups[2].Value, matches.Groups[1].Value);
}
Console.WriteLine(arps["01-00-5e-00-00-16"]);
}
注意:在这里选择字典有好处也有坏处。
您将获得 元素访问时间,但那里不能有重复的 MAC 地址。在不知道您的具体用例的情况下,很难说这种权衡是否适用于您,我只是指出这是一个选项。
您可以在 C# 中使用 Dictionary
数据结构 来保存您案例中的 key-value
配对信息这是Ipaddress-Macaddress
个信息
用法
var macofIpaddress = dict["192.168.137.1"];
代码
string output = "";
var proc = new ProcessStartInfo("cmd.exe", "/c arp -a")
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
WorkingDirectory = @"C:\Windows\System32\"
};
Process p = Process.Start(proc);
p.OutputDataReceived += (sender, args1) => { output += args1.Data + Environment.NewLine; };
p.BeginOutputReadLine();
p.WaitForExit();
var dict = new Dictionary<string, string>();
var lines = output.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach(var line in lines)
{
if (line.Contains("static") || line.Contains("dynamic"))
{
var elements = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
var ipAdd = elements[0].Trim();
var macAdd = elements[1].Trim();
dict.Add(ipAdd,macAdd);
}
}
输出:
您可以创建一个模型 class 来存储 IP 信息,例如:
public class DeviceIPAddress
{
public string IPv4 { get; set; }
public string MAC { get; set; }
public string IPType { get; set; }
}
现在,我们可以使用这个模型来解析提供的列表,这将使我们更容易处理:
var ips = new List<DeviceIPAddress>();
foreach (var ip in result)
{
var info = ip.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
ips.Add(new DeviceIPAddress { IPv4 = info[0].Trim(), MAC = info[1].Trim(), IPType = info[2].Trim() });
}
现在使用 ips
列表,我们可以轻松管理接收到的数据:
var getIP = ips.First(x => x.MAC.Equals("a8-3e-0e-61-3f-db", StringComparison.InvariantCultureIgnoreCase));
// call getIP.IPv4 will to get the IP 192.168.137.44
String s = "";
for (int i = 3; i < result.Count(); i++)
{
s = Convert.ToString(result[i]);
if (s.Contains(macAddress))
{
break;
}
}
char[] ip = new char[15];
StringBuilder ipaddr = new StringBuilder();
for (int i = 0; s[i].CompareTo(' ') != 0; i++)
{
ipaddr.Append(s[i]);
}
return ipaddr;
我将结果列表中的每个条目都用作字符串,并在所有条目中寻找我的 MAC 作为子字符串。
我试图将 phone 的 MAC 地址转换为它的 IP 地址。
var arpStream = ExecuteCommandLine("arp", "-a");
List<string> result = new List<string>();
while (!arpStream.EndOfStream)
{
var line = arpStream.ReadLine().Trim();
result.Add(line);
}
使用上面的代码,我将其存储在以下形式的列表中:
192.168.137.1 2e-bb-58-0a-2f-34 dynamic
192.168.137.44 a8-3e-0e-61-3f-db dynamic
192.168.137.91 d4-63-c6-b2-ac-38 dynamic
224.0.0.22 01-00-5e-00-00-16 static
224.0.0.252 01-00-5e-00-00-fc static
我想不通的是,如何为给定的 MAC 检索特定的 IP。 假设我的 phone 是具有物理地址的设备:a8-3e-0e-61-3f-db,我如何将它的 IP 作为字符串存储在某处?
我假设您已经以某种方式获得了字符串列表(ExecuteCommandLine
方法)并且希望能够根据 arp 值对其进行过滤。正则表达式可以是一个选项:
void Main()
{
// just setting it up for testing
List<string> result = new List<string>();
result.Add("192.168.137.1 2e-bb-58-0a-2f-34 dynamic");
result.Add("192.168.137.44 a8-3e-0e-61-3f-db dynamic");
result.Add("224.0.0.22 01-00-5e-00-00-16 static");
result.Add("224.0.0.22 01-00-5e-00-00-16 static");
result.Add("192.168.137.91 d4-63-c6-b2-ac-38 dynamic");
result.Add("224.0.0.22 01-00-5e-00-00-16 static");
result.Add("224.0.0.252 01-00-5e-00-00-fc static");
// this is the part you want
ConcurrentDictionary<string,string> arps = new ConcurrentDictionary<string, string>();
foreach (var s in result)
{
var matches = Regex.Match(s, @"((?:\d+\.?){4})\s+((?:[0-9a-f]{2}-?){6}).*");
arps.TryAdd(matches.Groups[2].Value, matches.Groups[1].Value);
}
Console.WriteLine(arps["01-00-5e-00-00-16"]);
}
注意:在这里选择字典有好处也有坏处。
您将获得
您可以在 C# 中使用 Dictionary
数据结构 来保存您案例中的 key-value
配对信息这是Ipaddress-Macaddress
个信息
用法
var macofIpaddress = dict["192.168.137.1"];
代码
string output = "";
var proc = new ProcessStartInfo("cmd.exe", "/c arp -a")
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
WorkingDirectory = @"C:\Windows\System32\"
};
Process p = Process.Start(proc);
p.OutputDataReceived += (sender, args1) => { output += args1.Data + Environment.NewLine; };
p.BeginOutputReadLine();
p.WaitForExit();
var dict = new Dictionary<string, string>();
var lines = output.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach(var line in lines)
{
if (line.Contains("static") || line.Contains("dynamic"))
{
var elements = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
var ipAdd = elements[0].Trim();
var macAdd = elements[1].Trim();
dict.Add(ipAdd,macAdd);
}
}
输出:
您可以创建一个模型 class 来存储 IP 信息,例如:
public class DeviceIPAddress
{
public string IPv4 { get; set; }
public string MAC { get; set; }
public string IPType { get; set; }
}
现在,我们可以使用这个模型来解析提供的列表,这将使我们更容易处理:
var ips = new List<DeviceIPAddress>();
foreach (var ip in result)
{
var info = ip.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
ips.Add(new DeviceIPAddress { IPv4 = info[0].Trim(), MAC = info[1].Trim(), IPType = info[2].Trim() });
}
现在使用 ips
列表,我们可以轻松管理接收到的数据:
var getIP = ips.First(x => x.MAC.Equals("a8-3e-0e-61-3f-db", StringComparison.InvariantCultureIgnoreCase));
// call getIP.IPv4 will to get the IP 192.168.137.44
String s = "";
for (int i = 3; i < result.Count(); i++)
{
s = Convert.ToString(result[i]);
if (s.Contains(macAddress))
{
break;
}
}
char[] ip = new char[15];
StringBuilder ipaddr = new StringBuilder();
for (int i = 0; s[i].CompareTo(' ') != 0; i++)
{
ipaddr.Append(s[i]);
}
return ipaddr;
我将结果列表中的每个条目都用作字符串,并在所有条目中寻找我的 MAC 作为子字符串。