PHP获取机器mac代码

废话不多话,直接上代码

<?php

class GetMac
{
    public $result = array();
    public $macAddrs = array(); //所有mac地址
    public $macAddr;            //第一个mac地址

    function __construct($OS)
    {
        $this->GetMac($OS);
    }

    function GetMac($OS)
    {
        switch (strtolower($OS)) {
            case "unix":
                break;
            case "solaris":
                break;
            case "aix":
                break;
            case "linux":
                $this->getLinux();
                break;
            default:
                $this->getWindows();
                break;
        }
        $tem = array();
        foreach ($this->result as $val) {
            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", $val, $tem)) {
                $this->macAddr = $tem[0];//多个网卡时,会返回第一个网卡的mac地址,一般够用。
                break;
                //$this->macAddrs[] = $temp_array[0];//返回所有的mac地址
            }
        }
        unset($temp_array);
        return $this->macAddr;
    }

    //Linux系统
    function getLinux()
    {
        @exec("ifconfig -a", $this->result);
        return $this->result;
    }

    //Windows系统
    function getWindows()
    {
        @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;
            }
        }
    }
}

$obj = new GetMac(PHP_OS);
print_r($obj->result);
echo $obj->macAddr;
//获取客户端
//$result=`arp -a $REMOTE_ADDR`;
//$result=`nbtstat -a $REMOTE_ADDR`;
//print_r($result);
exit;
  

 

代码到此为止,下面是验证.

接下来我们来验证下,windows上执行

 ipconfig/all  

查看下

 

执行php代码查看下:

 

 

注意,代码用的稍微老的语法,根据实际情况自行修改下.

果然,收工,睡觉了

 

转载于:https://www.cnblogs.com/wenhainan/p/10569017.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值