summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShevAbam <shevabam@gmail.com>2014-12-19 18:06:37 +0100
committerShevAbam <shevabam@gmail.com>2014-12-19 18:06:37 +0100
commitc3ec82dd6f60ac8057cc50d9bf6274738db9c02e (patch)
treefc078a574caa7c746879398fd725cdd092fc1d0e
parent258e0edbaad8e767060fa224c37f6594fea715a2 (diff)
Rewriting the network module (#12 and #16)
Some issues with Arch or old Ubuntu
-rw-r--r--libs/Utils/Misc.class.php25
-rw-r--r--libs/network.php91
2 files changed, 105 insertions, 11 deletions
diff --git a/libs/Utils/Misc.class.php b/libs/Utils/Misc.class.php
index d77bbf5..ae4b6d1 100644
--- a/libs/Utils/Misc.class.php
+++ b/libs/Utils/Misc.class.php
@@ -38,6 +38,31 @@ class Misc
return $_SERVER['SERVER_ADDR'];
}
+
+ /**
+ * Returns a command that exists in the system among $cmds
+ */
+ public static function whichCommand($cmds, $args = '', $returnWithArgs = true)
+ {
+ $return = '';
+
+ foreach ($cmds as $cmd)
+ {
+ if (trim(shell_exec($cmd.$args)) != '')
+ {
+ $return = $cmd;
+
+ if ($returnWithArgs)
+ $return .= $args;
+
+ break;
+ }
+ }
+
+ return $return;
+ }
+
+
/**
* Allows to pluralize a word based on a number
* Ex : echo 'mot'.Misc::pluralize(5); ==> prints mots
diff --git a/libs/network.php b/libs/network.php
index 20b5a3a..b63d990 100644
--- a/libs/network.php
+++ b/libs/network.php
@@ -1,12 +1,70 @@
<?php
require 'Utils/Misc.class.php';
-$datas = array();
-$network = array();
+$datas = array();
+$network = array();
-$ifconfig = trim(shell_exec('which ifconfig'));
-if (!(exec($ifconfig.' |awk -F \'[/ |: ]\' \'{print $1}\' |sed -e \'/^$/d\'', $getInterfaces)))
+// Possible commands for ifconfig and ip
+$commands = array(
+ 'ifconfig' => array('ifconfig', '/sbin/ifconfig', '/usr/bin/ifconfig', '/usr/sbin/ifconfig'),
+ 'ip' => array('ip', '/bin/ip', '/sbin/ip', '/usr/bin/ip', '/usr/sbin/ip'),
+);
+
+// Returns command line for retreive interfaces
+function getInterfacesCommand($commands)
+{
+ $ifconfig = Misc::whichCommand($commands['ifconfig'], ' | awk -F \'[/ |: ]\' \'{print $1}\' | sed -e \'/^$/d\'');
+
+ if (!empty($ifconfig))
+ {
+ return $ifconfig;
+ }
+ else
+ {
+ $ip_cmd = Misc::whichCommand($commands['ip'], ' -V', false);
+
+ if (!empty($ip_cmd))
+ {
+ return $ip_cmd.' -oneline link show | awk \'{print $2}\' | sed "s/://"';
+ }
+ else
+ {
+ return null;
+ }
+ }
+}
+
+// Returns command line for retreive IP address from interface name
+function getIpCommand($commands, $interface)
+{
+ $ifconfig = Misc::whichCommand($commands['ifconfig'], ' '.$interface.' | awk \'/inet / {print $2}\' | cut -d \':\' -f2');
+
+ if (!empty($ifconfig))
+ {
+ return $ifconfig;
+ }
+ else
+ {
+ $ip_cmd = Misc::whichCommand($commands['ip'], ' -V', false);
+
+ if (!empty($ip_cmd))
+ {
+ return 'for family in inet inet6; do '.
+ $ip_cmd.' -oneline -family $family addr show '.$interface.' | grep -v fe80 | awk \'{print $4}\' | sed "s/\/.*//"; ' .
+ 'done';
+ }
+ else
+ {
+ return null;
+ }
+ }
+}
+
+
+$getInterfaces_cmd = getInterfacesCommand($commands);
+
+if (is_null($getInterfaces_cmd) || !(exec($getInterfaces_cmd, $getInterfaces)))
{
$datas[] = array('interface' => 'N.A', 'ip' => 'N.A');
}
@@ -15,15 +73,26 @@ else
foreach ($getInterfaces as $name)
{
$ip = null;
- exec($ifconfig.' '.$name.' | awk \'/inet / {print $2}\' | cut -d \':\' -f2', $ip);
- if (!isset($ip[0]))
- $ip[0] = '';
+ $getIp_cmd = getIpCommand($commands, $name);
- $network[] = array(
- 'name' => $name,
- 'ip' => $ip[0],
- );
+ if (is_null($getIp_cmd) || !(exec($getIp_cmd, $ip)))
+ {
+ $network[] = array(
+ 'name' => $name,
+ 'ip' => 'N.A',
+ );
+ }
+ else
+ {
+ if (!isset($ip[0]))
+ $ip[0] = '';
+
+ $network[] = array(
+ 'name' => $name,
+ 'ip' => $ip[0],
+ );
+ }
}
foreach ($network as $interface)