如何跨网段取MAC地址
发布网友
发布时间:2022-04-20 04:36
我来回答
共3个回答
热心网友
时间:2023-07-02 09:39
跨网段获取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 "";
}
}
}
热心网友
时间:2023-07-02 09:39
不同网段是直接获取不到mac的,只有通过与目标在同网段的设备(主机)才能取到mac。
热心网友
时间:2023-07-02 09:39
不同网段获取主机MAC地址是可以得,但是需要对方开启netbios可以探测的服务,我们在使用工具的时候扫描的时候,netbios回复报文带有MAC地址信息