Php: three methods for obtaining the mac address of a local machine-PHP source code

Source: Internet
Author: User
Ec (2); & nbsp; copy the code classgetmacaddr {& nbsp; var $ return_arrayarray (); returns a string array with a mac address & nbsp; v script ec (2); script

The Code is as follows:

Class getmacaddr
{
Var $ return_array = array (); // returns a string array with a mac address.
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;

// Method 2

The Code is as follows:

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) <0)

{

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 );

}

// Implemented in c

The Code is as follows:

Int getalllocaladaptermacaddr (std: list > & Mac)

{

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 <adapterlist. length; ++ I)

{

Struct astat

{

Adapter_status adapt;

Name_buffer pstutorial z_name [30];

} Adapter;

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

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 <6; I ++)

V6 [I] = adapter. adapt. adapter_address [I];

If (v6 [0] = 0)

{

Std: list >:: 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;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.