fix: show server IP address in Network field instead of "Unavailable"

Three-level fallback chain:
1. gethostbyname(gethostname()) - PHP built-in, works on Linux and Windows
2. ip -4 addr show scope global | grep inet - Linux shell fallback
3. hostname -I - Linux shell secondary fallback

All results validated with FILTER_VALIDATE_IP before display.
This commit is contained in:
DevSoftTech Server 2026-08-04 02:40:33 +05:30
parent bb8ed60216
commit a4bccc765c

View File

@ -430,12 +430,29 @@ class SystemOperations
private function readNetworkStatus(): string
{
$result = $this->safeCommand('ip addr 2>/dev/null | head -n 5');
if ($result === 'Unavailable' || trim($result) === '') {
return 'Unavailable';
// Try PHP built-in first — works on Linux and Windows without shell
$hostname = gethostname();
if ($hostname !== false) {
$ip = gethostbyname($hostname);
// gethostbyname returns the hostname unchanged if resolution fails
if ($ip !== $hostname && filter_var($ip, FILTER_VALIDATE_IP)) {
return $ip;
}
}
return 'Detected';
// Fallback: parse first non-loopback IPv4 from ip addr output
$result = $this->safeCommand("ip -4 addr show scope global | grep inet | awk '{print $2}' | cut -d/ -f1 | head -1 2>/dev/null");
if ($result !== 'Unavailable' && trim($result) !== '' && filter_var(trim($result), FILTER_VALIDATE_IP)) {
return trim($result);
}
// Fallback: hostname -I
$result = $this->safeCommand('hostname -I 2>/dev/null | awk \'{print $1}\'');
if ($result !== 'Unavailable' && trim($result) !== '' && filter_var(trim($result), FILTER_VALIDATE_IP)) {
return trim($result);
}
return 'Unavailable';
}
private function readInternetStatus(): string