读书人

根据IP获取局域网客户机跨网段MAC地址

发布时间: 2013-07-20 11:07:48 作者: rapoo

根据IP获取局域网客户机跨网段MAC地址,为什么返回的是空值,请教有方法获取吗?
本帖最后由 cherish588 于 2013-07-17 17:30:48 编辑 现在用的方法如下(网上都说这个可以获取跨网段MAC,但是得到的是空值)
private string GetMac(string 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("\r", "").Replace("\n", "").Replace("\t", "");
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 ""; }
}


为什么这个方法得到的是空值,各位大侠有什么方法吗?
[解决办法]


private string GetMac(string 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();

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

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


这个方法正确,就是有一个问题,若客户端通过路由器再分出去就无法获得。

读书人网 >asp.net

热点推荐