diff --git a/web/includes/functions.php b/web/includes/functions.php
index e346ceb34..bfa4b9746 100644
--- a/web/includes/functions.php
+++ b/web/includes/functions.php
@@ -2414,4 +2414,70 @@ function i18n() {
return implode('-', $string);
}
+
+function get_networks() {
+ $interfaces = array();
+
+ exec('ip link', $output, $status);
+ if ( $status ) {
+ $html_output = implode('
', $output);
+ ZM\Error("Unable to list network interfaces, status is '$status'. Output was:
$html_output");
+ } else {
+ foreach ( $output as $line ) {
+ if ( preg_match('/^\d+: ([[:alnum:]]+):/', $line, $matches ) ) {
+ if ( $matches[1] != 'lo' ) {
+ $interfaces[$matches[1]] = $matches[1];
+ } else {
+ ZM\Debug("No match for $line");
+ }
+ }
+ }
+ }
+ $routes = array();
+ exec('ip route', $output, $status);
+ if ( $status ) {
+ $html_output = implode('
', $output);
+ ZM\Error("Unable to list network interfaces, status is '$status'. Output was:
$html_output");
+ } else {
+ foreach ( $output as $line ) {
+ if ( preg_match('/^default via [.[:digit:]]+ dev ([[:alnum:]]+)/', $line, $matches) ) {
+ $interfaces['default'] = $matches[1];
+ } else if ( preg_match('/^([.[:digit:]]+\/[[:digit:]]+) dev ([[:alnum:]]+)/', $line, $matches) ) {
+ $interfaces[$matches[2]] .= ' ' . $matches[1];
+ ZM\Debug("Matched $line: $matches[2] .= $matches[1]");
+ } else {
+ ZM\Debug("Didn't match $line");
+ }
+ } # end foreach line of output
+ }
+ return $interfaces;
+}
+
+# Returns an array of subnets like 192.168.1.0/24 for a given interface.
+# Will ignore mdns networks.
+
+function get_subnets($interface) {
+ $subnets = array();
+ exec('ip route', $output, $status);
+ if ( $status ) {
+ $html_output = implode('
', $output);
+ ZM\Error("Unable to list network interfaces, status is '$status'. Output was:
$html_output");
+ } else {
+ foreach ($output as $line) {
+ if (preg_match('/^([.[:digit:]]+\/[[:digit:]]+) dev ([[:alnum:]]+)/', $line, $matches)) {
+ if ($matches[1] == '169.254.0.0/16') {
+ # Ignore mdns
+ } else if ($matches[2] == $interface) {
+ $subnets[] = $matches[1];
+ } else {
+ ZM\Debug("Wrong interface $matches[1] != $interface");
+ }
+ } else {
+ ZM\Debug("Didn't match $line");
+ }
+ } # end foreach line of output
+ }
+ return $subnets;
+}
+
?>