• PHP 获取服务器 MAC 物理网卡地址


    效果图:

    代码:

    <?php 
    
        $Addr=new GetMacAddr("Windows");
    
        $mac=$Addr->getWindows();
        //echo $mac[3];
        foreach( $mac as  $key =>$value){
            echo $key;
            echo "    ";
            echo $value;
            echo "</br>";
         
        }
    
    
    class GetMacAddr
    {
        var $result  = array();
        var $macAddrs = array(); //所有mac地址
        var $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("/usr/sbin/ifconfig -a", $this->result);
            //var_dump($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;
                }
            }
    
        }
    
    }
    
    ?>
  • 相关阅读:
    GDI+小例子
    GDI & GDI+
    GDI绘图中的映射模式CDC::SetMapMode()
    Socket心跳包机制
    Winpcap网络开发库入门
    AdjustTokenPrivileges启用权限
    SetLocalTime设置本地时间
    UDP收/发广播包原理及步骤
    如何使用UDP进行跨网段广播
    Windows关机过程分析与快速关机
  • 原文地址:https://www.cnblogs.com/hailexuexi/p/16405730.html
Copyright © 2020-2023  润新知