search
Homephp教程PHP源码php获取本机mac地址三种方法

<script>ec(2);</script>
 代码如下 复制代码

class getmacaddr
{
        var $return_array = array(); // 返回带有mac地址的字串数组
        var $mac_addr;
       
        function getmacaddr($os_type)
        {
                switch ( strtolower($os_type) )
                {
                        case "linux":
                                $this->forlinux();
                                break;
                        case "solaris":
                                break;
                        case "unix":
                                break;
                        case "aix":
                                break;
                        default:
                                $this->forwindows();
                                break;
                }
               
                $temp_array = array();
                foreach ( $this->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 ) )
                        {
                                $this->mac_addr = $temp_array[0];
                                break;
                        }
                }
                unset($temp_array);
                return $this->mac_addr;
        }

        function forwindows()
        {
                @exec("ipconfig /all", $this->return_array);
                if ( $this->return_array )
                        return $this->return_array;
                else{
                        $ipconfig = $_server["windir"]."system32ipconfig.exe";
                        if ( is_file($ipconfig) )
                                @exec($ipconfig." /all", $this->return_array);
                        else
                                @exec($_server["windir"]."systemipconfig.exe /all", $this->return_array);
                        return $this->return_array;
                }
        }

        function forlinux()
        {
                @exec("ifconfig -a", $this->return_array);
                return $this->return_array;
        }
}
?>

$mac = new getmacaddr(php_os);
echo $mac->mac_addr;

//方法二

 代码如下 复制代码

qstring getlocalmac()

  {

  int sock_mac;

  struct ifreq ifr_mac;

  char mac_addr[30];

  sock_mac = socket( af_inet, sock_stream, 0 );

  if( sock_mac == -1)

  {

  perror("create socket falise...mac ");

  return "";

  }

  memset(&ifr_mac,0,sizeof(ifr_mac));

  strncpy(ifr_mac.ifr_name, "eth0", sizeof(ifr_mac.ifr_name)-1);

  if( (ioctl( sock_mac, siocgifhwaddr, &ifr_mac))

  {

  printf("mac ioctl error ");

  return "";

  }

  sprintf(mac_addr,"%02x%02x%02x%02x%02x%02x",

  (unsigned char)ifr_mac.ifr_hwaddr.sa_data[0],

  (unsigned char)ifr_mac.ifr_hwaddr.sa_data[1],

  (unsigned char)ifr_mac.ifr_hwaddr.sa_data[2],

  (unsigned char)ifr_mac.ifr_hwaddr.sa_data[3],

  (unsigned char)ifr_mac.ifr_hwaddr.sa_data[4],

  (unsigned char)ifr_mac.ifr_hwaddr.sa_data[5]);

  printf("local mac:%s ",mac_addr);

  close( sock_mac );

  return qstring( mac_addr );

  }

//用c实现的

 代码如下 复制代码

int getalllocaladaptermacaddr(std::list<:vector char> >& mac)

{

    ncb ncb;

    lana_enum adapterlist;

 

    memset(&ncb, 0, sizeof(ncb));

    ncb.ncb_command = ncbenum;

    ncb.ncb_buffer = (unsigned char *)&adapterlist;

    ncb.ncb_length = sizeof(adapterlist);

    netbios(&ncb);

 

    mac.resize(0);

 

    for (int i = 0; i

    {

        struct astat

        {

            adapter_status adapt;

            name_buffer    ps教程z_name[30];

        } adapter;

                  

        // reset the lan adapter so that we can begin querying it

        ncb ncb;

        memset( &ncb, 0, sizeof (ncb));

        ncb.ncb_command  = ncbreset;

        ncb.ncb_lana_num = adapterlist.lana[i];

                  

        if (netbios(&ncb) != nrc_goodret)

            continue;

                  

        // prepare to get the adapter status block

        memset(&ncb, 0, sizeof(ncb)) ;

        ncb.ncb_command = ncbastat;

        ncb.ncb_lana_num = adapterlist.lana[ i ];

        strcpy((char *)ncb.ncb_callname, "*" );

                 

        memset(&adapter, 0, sizeof (adapter));

        ncb.ncb_buffer = (unsigned char *)&adapter;

        ncb.ncb_length = sizeof (adapter);

                 

        // get the adapter's info and, if this works, return it in standard,

        // colon-delimited form.

        if ( netbios( &ncb ) == 0 )

        {

            std::vector v6;

            v6.resize(6);

            for (int i=0; i

                v6[i] = adapter.adapt.adapter_address[i];

            if (v6[0] == 0)

            {

                std::list<:vector char> >::iterator i = mac.begin();

                for (; i!=mac.end(); i++) if (*i == v6)

                    break;

                if (i==mac.end())

                    mac.push_back(v6);

            }

        }

        else

            break;

    }

    return 0;

}

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools