Skip to content

Instantly share code, notes, and snippets.

@j0inty
Forked from JunaidQadirB/get_mac.php
Last active September 8, 2016 14:00
Show Gist options
  • Save j0inty/2a355fb6dd143ab4d0d8 to your computer and use it in GitHub Desktop.
Save j0inty/2a355fb6dd143ab4d0d8 to your computer and use it in GitHub Desktop.
* added the php tag to activate the syntax highlight
<?php
/**
* Get the all interface mac addresses.
*
* @return array|integer Return the MAC adresses as an array OR if the exec() call failed the return will be the error code from "ipconfig /all".
*/
function get_ethernet_addresses()
{
$physicalAddresses = array();
$ipconfig_output = array();
$return = 0;
exec('ipconfig /all', $ipconfig_output, $return);
if( $return == 0 )
{
// https://regex101.com/r/uN9dF4/3
foreach( $ipconfig_output as &$line)
{
// Empty previous values
$matches = array();
// Let's find the MAC adresses
if( preg_match("/([[:blank:]][[:xdigit:]]{2}-[[:xdigit:]]{2}-[[:xdigit:]]{2}-[[:xdigit:]]{2}-[[:xdigit:]]{2}-[[:xdigit:]]{2})$/i", $line, $matches) )
{
$physicalAddresses[] = trim($matches[0]);
}
}
return $physicalAddresses;
}
return $return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment