Skip to content
This repository has been archived by the owner on Aug 19, 2024. It is now read-only.

Commit

Permalink
add option to be able to specify the ipconfig path. (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
vsc55 authored Jan 3, 2022
1 parent b1a4f00 commit aee0fea
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ var_dump(MacAddress::setFakeMacAddress('eth0'));

// set a specific MAC address on the eth0 interface
var_dump(MacAddress::setFakeMacAddress('eth0', '00:E4:01:2C:79:DA'));

// get the mac address of the eth0 interface using the ifconfig path that we define
var_dump(MacAddress::getCurrentMacAddress('eth0', '/usr/local/sbin/ifconfig'));
echo "\n";
```

For more see the example.php file. You can run the example on the command line
Expand Down
5 changes: 5 additions & 0 deletions example.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@
// set a specific MAC address on the eth0 interface
echo "Setting a specific MAC address: ";
var_dump(MacAddress::setFakeMacAddress('eth0', '00:E4:01:2C:79:DA'));
echo "\n";

// get the mac address of the eth0 interface using the ifconfig path that we define
echo "Current MAC address: ";
var_dump(MacAddress::getCurrentMacAddress('eth0', '/usr/local/sbin/ifconfig'));
echo "\n";
40 changes: 34 additions & 6 deletions src/BlakeGardner/MacAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,47 @@ class MacAddress
"8", "9", "A", "B", "C", "D", "E", "F"
);

/**
* Path where ifconfig will be searched by default
*/
public static function getIfconfig() {
$paths = array(
"/bin/ifconfig",
"/sbin/ifconfig",
"/usr/bin/ifconfig",
"/usr/sbin/ifconfig"
);
foreach ($paths as $path) {
if (file_exists($path)) {
return $path;
}
}
return "ifconfig";
}

/**
* Change the MAC address of the network interface specified
* @param string $interface Name of the interface e.g. eth0
* @param string $mac The new MAC address to be set to the interface
* @return bool Returns true on success else returns false
*/
public static function setFakeMacAddress($interface, $mac = null)
public static function setFakeMacAddress($interface, $mac = null, $ifconfig = null)
{

// if a valid mac address was not passed then generate one
if (!self::validateMacAddress($mac)) {
$mac = self::generateMacAddress();
}

// if ifconfig is not defined, the default value is used.
if (is_null($ifconfig)) {
$ifconfig = self::getIfconfig();
}

// bring the interface down, set the new mac, bring it back up
self::runCommand("ifconfig {$interface} down");
self::runCommand("ifconfig {$interface} hw ether {$mac}");
self::runCommand("ifconfig {$interface} up");
self::runCommand($ifconfig. " {$interface} down");
self::runCommand($ifconfig. " {$interface} hw ether {$mac}");
self::runCommand($ifconfig. " {$interface} up");

// TODO: figure out if there is a better method of doing this
// run DHCP client to grab a new IP address
Expand Down Expand Up @@ -102,9 +125,14 @@ protected static function runCommand($command)
* @param string $interface The name of the interface e.g. eth0
* @return string|bool Systems current MAC address; otherwise false on error
*/
public static function getCurrentMacAddress($interface)
public static function getCurrentMacAddress($interface, $ifconfig = null)
{
$ifconfig = self::runCommand("ifconfig {$interface}");
// if ifconfig is not defined, the default value is used.
if (is_null($ifconfig)) {
$ifconfig = self::getIfconfig();
}

$ifconfig = self::runCommand($ifconfig . " {$interface}");
preg_match("/" . self::$valid_mac . "/i", $ifconfig, $ifconfig);
if (isset($ifconfig[0])) {
return trim(strtoupper($ifconfig[0]));
Expand Down

0 comments on commit aee0fea

Please sign in to comment.