using System.Text.RegularExpressions;
using System.Diagnostics;
public class test
...{
public test
...{}
public static string GetCustomerMac(string IP) //para IP is the client's IP 
...{
string dirResults="";
ProcessStartInfo psi = new ProcessStartInfo();
Process proc = new Process();
psi.FileName = "nbtstat";
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.Arguments = "-A " + IP;
psi.UseShellExecute = false;
proc = Process.Start(psi);
dirResults = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
dirResults=dirResults.Replace(" ","").Replace(" ","").Replace(" ",""); 
Regex reg=new Regex("Mac[ ]{0,}Address[ ]{0,}=[ ]{0,}(?<key>((.)*?)) __MAC",RegexOptions.IgnoreCase|RegexOptions.Compiled);
Match mc=reg.Match(dirResults+"__MAC"); 
if(mc.Success) 
...{
return mc.Groups["key"].Value;
}
else 
...{
reg=new Regex("Host not found",RegexOptions.IgnoreCase|RegexOptions.Compiled);
mc=reg.Match(dirResults);
if(mc.Success) 
...{
return "Host not found!";
}
else 
...{
return "";
}
}
}
}
本文介绍了一种使用C#编程语言通过nbtstat命令获取客户端MAC地址的方法。该方法利用了System.Diagnostics名称空间下的Process类来启动外部进程并读取输出结果。此外,还使用了正则表达式从输出中精确提取MAC地址。
197

被折叠的 条评论
为什么被折叠?



