Add in get_networks and get_subnets as utilities to parse devices and networks in preparation for scanning/probing

This commit is contained in:
Isaac Connor 2022-02-07 12:31:31 -05:00
parent 69053424cd
commit 5078eecdfd
1 changed files with 66 additions and 0 deletions

View File

@ -2414,4 +2414,70 @@ function i18n() {
return implode('-', $string);
}
function get_networks() {
$interfaces = array();
exec('ip link', $output, $status);
if ( $status ) {
$html_output = implode('<br/>', $output);
ZM\Error("Unable to list network interfaces, status is '$status'. Output was:<br/><br/>$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('<br/>', $output);
ZM\Error("Unable to list network interfaces, status is '$status'. Output was:<br/><br/>$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('<br/>', $output);
ZM\Error("Unable to list network interfaces, status is '$status'. Output was:<br/><br/>$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;
}
?>