Skip to content

Commit dd780c5

Browse files
committed
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.
1 parent fa49cf8 commit dd780c5

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

lib/OperatingSystems/DefaultOs.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,10 @@ public function getNetworkInfo(): array {
137137
$result['hostname'] = \gethostname();
138138
$dns = shell_exec('cat /etc/resolv.conf |grep -i \'^nameserver\'|head -n1|cut -d \' \' -f2');
139139
$result['dns'] = $dns;
140-
$gw = shell_exec('ip route | awk \'/default/ { print $3 }\'');
141-
$result['gateway'] = $gw;
140+
$ip_route = $this->executeCommand('ip route');
141+
preg_match_all("/^default.*\bvia ([[:xdigit:].:]+)\b/m", $ip_route, $matches);
142+
$allgateways = implode(' ', $matches[1]);
143+
$result['gateway'] = $allgateways;
142144
return $result;
143145
}
144146

lib/OperatingSystems/FreeBSD.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,9 @@ public function getNetworkInfo(): array {
136136
$alldns = implode(' ', $matches[0]);
137137
$result['dns'] = $alldns;
138138
$netstat = $this->executeCommand('netstat -rn');
139-
preg_match("/(?<=^default).*\b\d/m", $netstat, $gw);
140-
$result['gateway'] = $gw[0];
139+
preg_match_all("/^default\s+([[:xdigit:].:]+)\b/m", $netstat, $matches);
140+
$allgateways = implode(' ', $matches[1]);
141+
$result['gateway'] = $allgateways;
141142
} catch (\RuntimeException $e) {
142143
return $result;
143144
}

0 commit comments

Comments
 (0)