Three ways to get native MAC addresses from PHP

Source: Internet
Author: User
The code is as follows Copy Code

Class Getmacaddr
{
var $return _array = Array (); Returns an array of strings 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 Two

The code is as follows Copy Code

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 Copy Code

int getalllocaladaptermacaddr (std::list<std::vector<unsigned 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 < adapterlist.length; ++i)

{

struct ASTAT

{

Adapter_status adapt;

Name_buffer PS Tutorial z_name[30];

} adapter;

Reset the LAN adapter so 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 to it in standard,

colon-delimited form.

if (NetBIOS (&NCB) = = 0)

{

std::vector<unsigned char> V6;

V6.resize (6);

for (int i=0; i<6; i++)

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

if (v6[0] = = 0)

{

std::list<std::vector<unsigned 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;

}

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.