C# 네트워크 상 맥어드레스 가져오기
public class MacAddressHelper
{
private static List<MacIpPair> _macIpPairs;
public static string GetMacByIp(string ip)
{
if (_macIpPairs == null)
{
GetAllMacAddressesAndIppairs();
}
int index = _macIpPairs.FindIndex(x => x.IpAddress == ip);
if (index >= 0)
{
return _macIpPairs[index].MacAddress.ToUpper();
}
else
{
return string.Empty;
}
}
private static void 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
});
}
_macIpPairs = mip;
}
private struct MacIpPair
{
public string MacAddress;
public string IpAddress;
}
}
'C# > Network' 카테고리의 다른 글
C# WOL 컴퓨터 원격 온오프 (0) | 2020.08.07 |
---|---|
C# Ping 테스트, 해당 IP 장치 이름 가져오기 (0) | 2020.08.07 |
C# http post header body (0) | 2020.08.07 |
C# HttpClient RestAPI Post (0) | 2020.08.05 |
C# HttpClient RestAPI Patch (0) | 2020.08.04 |