当前位置:系统粉 > 电脑问答 > 其他问答 > 在内网中如何跨网段根据IP地址查找到mac地址

在内网中如何跨网段根据IP地址查找到mac地址

提问者:bbnini75  |  浏览 次  |  提问时间:2017-02-23  |  回答数量:2

在内网中如何跨网段根据IP地址查找到mac地址例如本机IP是192.168.1.100,知道另一个IP是192.168.20.36,相互能ping通,如何根据IP查找到对方的mac地址

已有2条答案
全球音谷vocc

全球音谷vocc

回答数:113  |  被采纳数:69

2017-03-02 20:18:00
跨网段获取MAC地址方法如下:
protected void Page_Load(object sender, EventArgs e)
{
//ReadMSG();

if (!IsPostBack)
{
Label1.Text =
"//客户端ip:" +

Request.ServerVariables.Get("Remote_Addr").ToString() + "<br>" +
" //客户端主机名:" +
Request.ServerVariables.Get("Remote_Host").ToString() + "<br>" +
"//客户端浏览器IE:" +
Request.Browser.Browser + "<br>" +
"//客户端浏览器 版本号:" +
Request.Browser.MajorVersion + "<br>" +
"//客户端操作系统:" +
Request.Browser.Platform + "<br>" +
"//服务器ip:" +
Request.ServerVariables.Get("Local_Addr").ToString() + "<br>" +
"//服务器名:" +
Request.ServerVariables.Get("Server_Name").ToString() + "<br>";

string stringMAC = "";
string stringIP = "";
ManagementClass MC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection MOC = MC.GetInstances();

foreach (ManagementObject MO in MOC)
{
if ((bool)MO["IPEnabled"] == true)
{
stringMAC += MO["MACAddress"].ToString(); //获取网卡的地址
string[] IPAddresses = (string[])MO["IPAddress"]; //获取本地的IP地址
if (IPAddresses.Length > 0)
stringIP = IPAddresses[0];
Label1.Text += "<br>MAC:" + stringMAC + "/ IP:" + stringIP;
if (stringIP == "192.168.1.45")
Label1.Text += "<br>I get you :192.168.1.45<br>";
}
}
//在页面上打印出客户端的网卡物理地址(MAC)
//Response.Write(this.GetMac(Request.UserHostAddress.ToString())+"<br>");
GetInfo();
}
}
//获取cpu序列号,硬盘ID,网卡MAC地址
private void GetInfo()
{
string cpuInfo = "";//cpu序列号
ManagementClass cimobject = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach (ManagementObject mo in moc)
{
cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
Label1.Text += "cpu序列号:" + cpuInfo.ToString();
}

//获取硬盘ID
String HDid;
ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc1 = cimobject1.GetInstances();
foreach (ManagementObject mo in moc1)
{
HDid = (string)mo.Properties["Model"].Value;
Label1.Text += "硬盘序列号:" + HDid.ToString();
}

}

//protected void Button1_Click(object sender, EventArgs e)
//{

// try
// {
// File.Delete(@"D:\html.html");
// }
// catch (Exception e2)
// {
// Response.Write(e2.Message);
// }
// Response.Write("Delete Successed!");

//}
//获取远程客户端的网卡物理地址(MAC)
public string GetMac(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("\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 "";
}
}
}
赞 11
文字旅者

文字旅者

回答数:33  |  被采纳数:36

2017-03-02 16:14:24
第一种获取远程MAC的方法是使用命令arp -a(示例:arp -a 192.168.20.36),在命令行输入 “arp -a 192.168.20.36”,按回车,获取到的物理地址就是所谓的MAC地址;第二种获取远程MAC的方法是使用命令nbtstat -a(示例:nbtstat -a 192.168.20.36),在命令行输入"nbtstat -a 192.168.20.36",按回车,使用这个命令的好处就是不仅能获取到MAC地址,还能获取到远程电脑的电脑名称和所属工作组。
赞 3
解决方法
版权信息

Copyright @ 2011 系统粉 版权声明 最新发布内容 网站导航