Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Improve gateway information parsing
1. Fix FreeBSD-specific parsing that missed any digits
   of the last IP octet more than a single digit.
2. Support IPv6 addresses as the default gateway
3. Parse multiple default gateways in a dual-stack
   IPv4/IPv6 system
4. Switch DefaultOs.php to use `preg_match_all()`
   instead of a shell pipeline for parsing.

Signed-off-by: Ross Williams <[email protected]>
  • Loading branch information
overhacked committed Dec 14, 2021
commit 560e87eeddc908f369d9ab31b2b38d71cd5cfd41
6 changes: 4 additions & 2 deletions lib/OperatingSystems/DefaultOs.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,10 @@ public function getNetworkInfo(): array {
$result['hostname'] = \gethostname();
$dns = shell_exec('cat /etc/resolv.conf |grep -i \'^nameserver\'|head -n1|cut -d \' \' -f2');
$result['dns'] = $dns;
$gw = shell_exec('ip route | awk \'/default/ { print $3 }\'');
$result['gateway'] = $gw;
$ip_route = $this->executeCommand('ip route');
preg_match_all("/^default.*\bvia ([[:xdigit:].:]+)\b/m", $ip_route, $matches);
$allgateways = implode(' ', $matches[1]);
$result['gateway'] = $allgateways;
return $result;
}

Expand Down
5 changes: 3 additions & 2 deletions lib/OperatingSystems/FreeBSD.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ public function getNetworkInfo(): array {
$alldns = implode(' ', $matches[0]);
$result['dns'] = $alldns;
$netstat = $this->executeCommand('netstat -rn');
preg_match("/(?<=^default).*\b\d/m", $netstat, $gw);
$result['gateway'] = $gw[0];
preg_match_all("/^default\s+([[:xdigit:].:]+)\b/m", $netstat, $matches);
$allgateways = implode(' ', $matches[1]);
$result['gateway'] = $allgateways;
} catch (\RuntimeException $e) {
return $result;
}
Expand Down