获取客户端的MAC地址

来源:互联网 发布:prtg交换机端口传感器 编辑:程序博客网 时间:2024/05/18 02:24
  using System.Text.RegularExpressions;
  using System.Diagnostics;
 

  /// 获取客户端本地MAC
  /// CODE CREATE BY BANLAO 
  /// </summary>
  /// <param name="strIP">客户端的IP</param>
  /// <returns></returns>
  public string _GetCustomerMac(string strIP)
  {
       string strResults = "";
       ProcessStartInfo psi = new ProcessStartInfo();
       Process proc = new Process();
       psi.FileName = "nbtstat";
       psi.RedirectStandardInput = false;
       psi.RedirectStandardOutput = true;
       psi.Arguments = "-a " + strIP;
       psi.UseShellExecute = false;
       proc = Process.Start(psi);
       strResults = proc.StandardOutput.ReadToEnd();
       proc.WaitForExit();

 

       //匹配mac地址
       Match m = Regex.Match(strResults, "//w+//-//w+//-//w+//-//w+//-//w+//-//w//w");

       //若匹配成功则返回mac,否则返回找不到主机信息
       if (m.ToString() != "")
       {
            return m.ToString();
       }
       else
       {
            return "";
       }
  }