<?php
/*
* 使用实例
*/
$mac = new MacClass();
echo $mac->GetMac();
/*
* 获取服务器网卡的MAC地址
* 支持Windows/Linux系统
*/
class MacClass {
var $os;
var $bug = false;
/*
* 构造函数
*/
function MacClass(){
if(function_exists('exec')){
$this->os = strtolower(PHP_OS);
}else{
$this->bug = '服务器不支持 Exec 函数!';;
}
}
/*
* 获取Mac
*/
function GetMac(){
$info = array();
if($this->bug !== false){
return $this->bug;
}elseif($this->os == 'solaris' || $this->os == 'unix' || $this->os == 'aix'){
return '暂不支持!';
}elseif($this->os == 'linux'){
$info = $this->Linux();
}else{
$info = $this->Windows();
}
$temp = array();
foreach($info as $value){
$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";
if(preg_match($match, $value, $temp)){
return $temp[0];
}
}
}
/*
* Windows
*/
function Windows(){
@exec("ipconfig /all", $info);
if(is_array($info)){
return $info;
}else{
$sys = $_SERVER['WINDIR'];
if(is_file($sys."\system32\ipconfig.exe")){
@exec($sys."\system32\ipconfig.exe /all", $info);
}elseif(is_file($sys."\system\ipconfig.exe")){
@exec($sys."\system\ipconfig.exe /all", $info);
}
return $info;
}
}
/*
* Linux
*/
function Linux(){
@exec("ifconfig -a", $info);
return $info;
}
}
?>
转载于:https://my.oschina.net/slywl/blog/89040