如何跨网段取MAC地址

如何跨网段取MAC地址,第1张

跨网段获取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() } //获取硬盘IDString 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 "" }}}

两种方法设置:

1、编辑配置文件。

vi  /etc/sysconfig/network-scripts/ifcfg-eth0

如图所示:将BOOTPROTO=none改成BOOTPROTO=DHCP即可从网络设备自动获取IP地址等网络信息。

2、netconfig命令。

输入netconfig后将会出现下图所示,单击yes按钮。

选中”use dynamic ip configuration“点击OK即可。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8676509.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-19
下一篇 2023-04-19

发表评论

登录后才能评论

评论列表(0条)

保存