全球音谷vocc
回答数:113 | 被采纳数:69
跨网段获取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 "";
}
}
}
第一种获取远程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地址,还能获取到远程电脑的电脑名称和所属工作组。