php获取服务器端mac和客户端mac的地址支持WIN/LINUX

yipeiwu_com5年前服务器
获取服务器mac
复制代码 代码如下:

<?php
/**
获取网卡的MAC地址原码;目前支持WIN/LINUX系统
获取机器网卡的物理(MAC)地址
**/
class GetmacAddr{
var $result = array(); // 返回带有MAC地址的字串数组
var $macAddr;
/*构造*/
function __construct($osType){
switch ( strtolower($osType) ){
case "unix": break;
case "solaris": break;
case "aix": break;
case "linux": {
$this->for_linux_os();
}break;
default: {
$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不能在本机测试,只能用别的电脑访问才能输出

相关文章

浅析PHP程序防止ddos,dns,集群服务器攻击的解决办法

废话不多说,上代码复制代码 代码如下:<?php//查询禁止IP$ip =$_SERVER['REMOTE_ADDR'];$fileht=".htaccess2";if(!file...

PHP 服务器配置(使用Apache及IIS两种方法)

一、使用Apache≡ PHP 5.2.5 的安装 ≡1、到其官方站点下载 php-5.2.5-Win32.zip 并解压(据说:不要下载及使用它的Installer,这种方式虽然很自动...

开启CURL扩展,让服务器支持PHP curl函数(远程采集)

curl()、file_get_contents()、snoopy.class.php这三个远程页面抓取或采集中用到的工具,默迹还是侵向于用snoopy.class.php,因为他效率比...

Python写的Socks5协议代理服务器

直接上代码: #!/usr/bin/python # Filename s5.py # Python Dynamic Socks5 Proxy # Usage: python...

在Windows服务器下用Apache和mod_wsgi配置Python应用的教程

最近开发了一个 Google Analytics 相关的应用,但需要在 Windows 下部署,结合网上的相关经验,最终选择了 apache+mod_wsgi 这样的配置。 修改pyth...