获取服务端mac地址

/**
     * 获取MacAddress
     * @var array
     */
    public $returnArray = [];
    public $macAddress;

    public function getMacAddress($os_type)
    {
        switch (strtolower($os_type)) {
            // MacOS MoJave系统(处理同linux)
            case "darwin":
            case "linux":
                $this->forLinux();
                break;
            case "solaris":
                break;
            case "unix":
                break;
            case "aix":
                break;
            default:
                $this->forWindows();
                break;

        }
        $tempArray = [];
        foreach ($this->returnArray 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, $tempArray)) {
                $this->macAddress = $tempArray[0];
                break;
            }

        }
        unset($tempArray);
        return $this->macAddress;
    }

    /**
     * 获取ipconfig /all 每行参数转化为数组
     * @return array
     */
    public function forWindows()
    {
        @exec("ipconfig /all", $this->returnArray);
        if ($this->returnArray) {
            return $this->returnArray;
        } else {
            $ipconfig = $_SERVER["WINDIR"] . "\system32\ipconfig.exe";
            if (is_file($ipconfig)) {
                @exec($ipconfig . " /all", $this->returnArray);
            } else {
                @exec($_SERVER["WINDIR"] . "\system\ipconfig.exe /all", $this->returnArray);
            }
            return $this->returnArray;
        }
    }

    /**
     * 获取ipconfig /all 每行参数转化为数组
     * @return array
     */
    public function forLinux()
    {
        @exec("ifconfig -a", $this->returnArray);
        return $this->returnArray;
    }

关注友儿不迷路

Last modification:September 20th, 2020 at 12:13 am
如果觉得我的文章对你有用,请随意赞赏