蓝少个人博客

实迷途其未远,觉今是而昨非

PHP获取通过windows系统命令wmic获取MAC地址、CPU序列号、主板序列号

日期:2017-07-20 阅读:466 分类:学无止境 标签:DOS PHP 

在项目中,客户需要系统在win系统上获取MAC地址、CPU序列号和主板序列号等,在网上搜索下,通过windows系统命令wmic可以获取,测试基本可行,HardwareInfo.php源代码如下:

<?php
class HardwareInfo{
    //获取MAC地址
    function getMacAddress(){
        $return_arry = array();
        @exec("wmic nicconfig get macaddress", $return_arry);
        //原代码下面两行,但在某些机器上$return_arry[1]是空的,所以改进了一下
        /*
         $mac_addr = $return_arry[1];
         $mac_addr = str_replace(":", "", $mac_addr);//去除字符串中的字符“:”
         */
        $key=0;
        foreach ($return_arry as $k=>$val){
            if(!empty($val) && $k>0){
                $mac_addr[$key] = $val;
                //去除字符串中的字符“:”
                $mac_addr[$key] = str_replace(":", "", $mac_addr[$key]);
                $key++;
            }
        }
        return $mac_addr;
    }
    //获取CPU序列号
    function getCpuSN(){
        $return_arry = array();
        @exec("wmic cpu get processorid", $return_arry);
        $cpu_sn = $return_arry[1];
        return $cpu_sn;
    }
    //获取主板序列号
    function getBaseboardSN(){
        $return_arry = array();
        @exec("wmic baseboard get serialnumber", $return_arry);
        $baseboard_sn = $return_arry[1];
        //去除字符串中的字符“-”
        $baseboard_sn = str_replace("-", "", $baseboard_sn);
        return $baseboard_sn;
    }
}
?>

使用示例test.php如下:

<?php
include 'HardwareInfo.php';
$hw_addr = new HardwareInfo();
$mac_addr = $hw_addr->getMacAddress();
$cpu_sn = $hw_addr->getCpuSN();
$baseboard_sn = $hw_addr->getBaseboardSN();
foreach ($mac_addr as $val){
    echo 'MAC地址:'.$val.'<br>';
}
echo 'CPU序列号:'.$cpu_sn.'<br>';
echo '主板地址:'.$baseboard_sn.'<br>';
?>

Windows系统下使用wmic命令查看硬件信息

1、查询网卡信息:wmic nicconfig get macaddress
2、查询cpu序列号:wmic cpu get processorid
3、查询主板序列号:wmic baseboard get serialnumber
4、查询BIOS序列号:wmic bios get serialnumber
补充:在有些电脑上查询结果未正常显示,而是出现“To be filled by OEM”,可能是系统出问题了。