获取服务器mac 复制
代码 代码如下: <?php /** 获取网卡的MAC
地址原码;目前支持WIN/LINUX系统 获取机器网卡的物理(MAC)地址 **/ class GetmacAddr{ var $result = array()// 返回带有MAC地址的字串
数组 var $macAddr/*构造*/ function __construct($osType){ switch ( strtolower($osType) ){ case "unix": breakcase "solaris": breakcase "aix": breakcase "linux": { $this->for_linux_os()}breakdefault: { $this->for_windows_os()}break} $temp_array = array()foreach($this->result as $value){ if(preg_match("/[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f]/i",$value, $temp_array ) ){ $this->macAddr = $temp_array[0]break} } unset($temp_array)return $this->macAddr} /*linux系统中获取方法*/ function for_linux_os(){ @exec("ifconfig -a", $this->result)return $this->result} /*win系统中的获取方法*/ function for_windows_os(){ @exec("ipconfig /all", $this->result)if ( $this->result ) { return $this->result} else { $ipconfig = $_SERVER["WINDIR"]."\system32\ipconfig.exe"if(is_file($ipconfig)) { @exec($ipconfig." /all", $this->result)} else { @exec($_SERVER["WINDIR"]."\system\ipconfig.exe /all", $this->result)return $this->result} } } } ?> 获取客户端mac地址: 复制代码 代码如下: @exec("arp -a",$array)//执行arp -a命令,结果放到数组$array中 foreach($array as $value){ //匹配结果放到数组$mac_array if(strpos($value,$_SERVER["REMOTE_ADDR"]) &&preg_match("/(:?[0-9A-F]{2}[:-]){5}[0-9A-F]{2}/i",$value,$mac_array)){ $mac = $mac_array[0]break} } echo $mac 注:客户端获取的mac不能在本机测试,只能用别的电脑访问才能输出复制代码 代码示例:private void ButtonIP_Click(object sender, System.EventArgs e)
{ System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList
if ( addressList.Length>1)
{ TextLIP.Text = addressList[0].ToString()
TextSIP.Text = addressList[1].ToString()}else{TextLIP.Text = addressList[0].ToString()
TextSIP.Text = "没有可用的连接"}}另一种获取服务器的IP地址与MAC地址的方法:
复制代码 代码示例:using System.Management
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()
TextMAC.Text = stringMAC.ToString()
string[] IPAddresses = (string[]) MO["IPAddress"]
if(IPAddresses.Length >0)
stringIP = IPAddresses[0]
TextIP.Text = stringIP.ToString()}}获取客户端本机的IP地址的方法:
复制代码 代码示例:using System.Net
TextIP.Text=Page.Request.UserHostAddress获取客户端本机的MAC地址刚复杂一些,需要调用两个API,用ARP协议获取,但也只能获取到同网段机器的MAC,对于跨网段的得利用IP扫描或cmd中nBTstat命令获取MAC地址。
评论列表(0条)