From c01728273b0718bff86817e62a26fd358eb39996 Mon Sep 17 00:00:00 2001 From: Michael Heap Date: Fri, 9 Aug 2013 18:13:00 +0100 Subject: [PATCH] Initial commit --- .gitignore | 2 + README.md | 36 +++++ composer.json | 12 ++ example/example.php | 58 ++++++++ src/Datasift/netifaces.php | 136 ++++++++++++++++++ src/Datasift/netifaces/NetifacesException.php | 52 +++++++ 6 files changed, 296 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 composer.json create mode 100644 example/example.php create mode 100644 src/Datasift/netifaces.php create mode 100644 src/Datasift/netifaces/NetifacesException.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7579f74 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +vendor +composer.lock diff --git a/README.md b/README.md new file mode 100644 index 0000000..d8d7b8c --- /dev/null +++ b/README.md @@ -0,0 +1,36 @@ +# Netifaces + +### Installation + +Add the following to your composer.json file + +```json +{ + "requires": { + "datasift/netifaces": "*" + } +} +``` + +### Usage + +We currently support two methods, `listAdapters()` and `getIpAddress()`. + +```php +require __DIR__.'/../vendor/autoload.php'; + +// To get information about our network adapters, we need to know about two things +// 1. The OS that we're on +$os = Datasift\Os::getOs(); +// 2. We need a parser for the ifconfig output +$parser = Datasift\IfconfigParser::fromDistributions($os->getPossibleClassNames()); + +// Next, we create a new netifaces instance, passing in our OS and Parser +$netifaces = new Datasift\netifaces($os, $parser); + +// Then we can list the available adapters +var_dump($netifaces->listAdapters()); + +// Or get the IP address if a specific adapter +var_dump($netifaces->getIpAddress("eth0")); +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..62f5c7d --- /dev/null +++ b/composer.json @@ -0,0 +1,12 @@ +{ + "name": "datasift/netifaces", + "require": { + "datasift/os": "*", + "datasift/ifconfig-parser": "*" + }, + "autoload": { + "psr-0": { + "Datasift": "src/" + } + } +} diff --git a/example/example.php b/example/example.php new file mode 100644 index 0000000..4380f00 --- /dev/null +++ b/example/example.php @@ -0,0 +1,58 @@ + + * @copyright 2013-present Mediasift Ltd www.datasift.com + * @license http://www.opensource.org/licenses/bsd-license.php BSD License + * @link http://github.com/datasift/netifaces + */ + +require __DIR__.'/../vendor/autoload.php'; + +// To get information about our network adapters, we need to know about two things +// 1. The OS that we're on +$os = Datasift\Os::getOs(); +// 2. We need a parser for the ifconfig output +$parser = Datasift\IfconfigParser::fromDistributions($os->getPossibleClassNames()); + +// Next, we create a new netifaces instance, passing in our OS and Parser +$netifaces = new Datasift\netifaces($os, $parser); + +// Then we can list the available adapters +var_dump($netifaces->listAdapters()); + +// Or get the IP address if a specific adapter +var_dump($netifaces->getIpAddress("eth0")); diff --git a/src/Datasift/netifaces.php b/src/Datasift/netifaces.php new file mode 100644 index 0000000..0c1efbc --- /dev/null +++ b/src/Datasift/netifaces.php @@ -0,0 +1,136 @@ + + * @copyright 2013-present Mediasift Ltd www.datasift.com + * @license http://www.opensource.org/licenses/bsd-license.php BSD License + * @link http://github.com/datasift/netifaces + */ + +namespace Datasift; + +use Datasift\netifaces\NetifacesException; + +/** + * netifaces + * + * @author Michael Heap + */ +class netifaces { + + private $os; + private $parser; + + /** + * __construct + * + * @param \Datasift\Os\OsInterface $os os + * @param \Datasift\IfconfigParser\Base $parser parser + * + * @return void + */ + public function __construct(\Datasift\Os\OsInterface $os, \Datasift\IfconfigParser\Base $parser){ + $this->os = $os; + $this->parser = $parser; + } + + /** + * listAdapters + * + * + * @return array List of available adapters + */ + public function listAdapters(){ + + $adapters = array(); + + // Loop through all of our interfaces, extracting only + // the interface name + foreach ($this->getParsedIfconfig() as $r){ + $adapters[] = $r['interface']; + } + + return $adapters; + } + + /** + * getIpAddress + * + * @param string $interface Interface to get the IP address of + * + * @return string + * @throws \Datasift\netifaces\Exception + */ + public function getIpAddress($interface = null){ + + // Make sure we have an interface to look for + if (is_null($interface)){ + throw new NetifacesException("No interface supplied when asking for IP address"); + } + + // Search through our adapters + foreach ($this->getParsedIfconfig() as $r){ + // If this is the interface we're looking for + if ($r['interface'] == $interface){ + // If we know about the adapter but it doesn't have an IP + // address, fall through + if (!isset($r['ip_address'])){ + break; + } + + return $r['ip_address']; + } + } + + throw new NetifacesException("Could not retrieve IP address for '".$interface."'"); + + } + + /** + * getParsedIfconfig + * + * Helper method for the common task of getting ifconfig + * output and parsing it + * + * @return array + */ + private function getParsedIfconfig(){ + $ifConfig = $this->os->runCommand("ifconfig"); + $res = $this->parser->parse($ifConfig); + + return $res; + } +} diff --git a/src/Datasift/netifaces/NetifacesException.php b/src/Datasift/netifaces/NetifacesException.php new file mode 100644 index 0000000..6ebd176 --- /dev/null +++ b/src/Datasift/netifaces/NetifacesException.php @@ -0,0 +1,52 @@ + + * @copyright 2013-present Mediasift Ltd www.datasift.com + * @license http://www.opensource.org/licenses/bsd-license.php BSD License + * @link http://github.com/datasift/netifaces + */ + +namespace Datasift\netifaces; + +/** + * NetifacesException + * + * @author Michael Heap + */ +class NetifacesException extends \Exception { + +}