From aee0feabb05e4c0b2e972b6fb3d3aedff224e772 Mon Sep 17 00:00:00 2001 From: Javier Pastor Date: Mon, 3 Jan 2022 21:07:18 +0100 Subject: [PATCH] add option to be able to specify the ipconfig path. (#3) --- README.md | 4 ++++ example.php | 5 +++++ src/BlakeGardner/MacAddress.php | 40 ++++++++++++++++++++++++++++----- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ee76254..3191d06 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/example.php b/example.php index c0a3263..08eba5f 100644 --- a/example.php +++ b/example.php @@ -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"; \ No newline at end of file diff --git a/src/BlakeGardner/MacAddress.php b/src/BlakeGardner/MacAddress.php index 4f9099b..ff4a61c 100644 --- a/src/BlakeGardner/MacAddress.php +++ b/src/BlakeGardner/MacAddress.php @@ -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 @@ -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]));