Skip to content

Commit

Permalink
Improve gateway information parsing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
overhacked committed Oct 21, 2021
1 parent fa49cf8 commit dd780c5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
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

0 comments on commit dd780c5

Please sign in to comment.