PHP thinkphp 在 linux windows 下 獲取 MAC地址

 PHP thinkphp 在 linux windows 下 獲取 MAC地址

public static function getMacAddress(){
        if(strtolower(PHP_OS) == 'linux'){
            return self::forLinux();
        }else{
            $return_array = self::forWindows();
            $temp_array = array();
            foreach ( $return_array 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 ) ) {
                    $mac_addr = $temp_array[0];
                    break;
                }
            }
            unset($temp_array);
            //return strtoupper(md5($mac_addr));
            return $mac_addr;
        }
}

//windows下
public static function forWindows(){
    	$return_array = array();
        @exec("ipconfig /all", $return_array);
        if ( $return_array ) {

            return $return_array;
        }else {
            $ipconfig = $_SERVER["WINDIR"]."\system32\ipconfig.exe";
            if ( is_file($ipconfig) ) {
                @exec($ipconfig." /all", $return_array);
            } else {
                @exec($_SERVER["WINDIR"]."\system\ipconfig.exe /all", $return_array);
            }
            return $return_array;
        }
    }
//Linux
public static function forLinux(){
        // @exec("ifconfig -a", $this->return_array);
        // return $this->return_array;
        //exec被禁用時可使用下面的方式進行獲取MAC地址
        $path = '/etc/sysconfig/network-scripts/ifcfg-eth*';
        $res = glob($path);
        if (isset($res[0])  && is_file($res[0]) && is_readable($res[0])) {
            $arr = parse_ini_file($res[0]);
            $pathArr = explode("/", $res[0]);
            //$salt = array_pop($pathArr);
            $macAddr = isset($arr['HWADDR']) && !empty($arr['HWADDR']) ? $arr['HWADDR'] : '1234abcd' ;
            //$macAddr = isset($arr['HWADDR']) && !empty($arr['HWADDR']) ? $salt.$arr['HWADDR'] : '1234abcd' ;
            //$hashVal = strtoupper(md5($macAddr));
            return $hashVal;
        } else {
            // echo 'The file does not exists or can not read';
            // die;
            return false;
        }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章