Skip to content
Merged
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
16 changes: 14 additions & 2 deletions lib/OperatingSystems/FreeBSD.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,23 @@ public function getThermalZones(): array {
return [];
}

/**
* Execute a command with shell_exec.
*
* The command will be escaped with escapeshellcmd.
*
* @throws RuntimeException if shell_exec is unavailable, the command failed or an empty response.
*/
protected function executeCommand(string $command): string {
$output = @shell_exec(escapeshellcmd($command));
if ($output === null || $output === '' || $output === false) {
if (function_exists('shell_exec') === false) {
throw new RuntimeException('shell_exec unavailable');
}

$output = shell_exec(escapeshellcmd($command));
if ($output === false || $output === null || $output === '') {
throw new RuntimeException('No output for command: "' . $command . '"');
}

return $output;
}

Expand Down
37 changes: 29 additions & 8 deletions lib/OperatingSystems/Linux.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ public function getCpuName(): string {
}

public function getTime(): string {
return (string)shell_exec('date');
try {
return $this->executeCommand('date');
} catch (RuntimeException $e) {
return '';
}
}

public function getUptime(): int {
Expand All @@ -139,12 +143,17 @@ public function getUptime(): int {
}

public function getNetworkInfo(): array {
$result = [];
$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;
$result = [
'hostname' => \gethostname(),
'dns' => '',
'gateway' => '',
];

if (function_exists('shell_exec')) {
$result['dns'] = shell_exec('cat /etc/resolv.conf |grep -i \'^nameserver\'|head -n1|cut -d \' \' -f2');
$result['gateway'] = shell_exec('ip route | awk \'/default/ { print $3 }\'');
}

return $result;
}

Expand Down Expand Up @@ -261,11 +270,23 @@ protected function readContent(string $filename): string {
return trim($data);
}

/**
* Execute a command with shell_exec.
*
* The command will be escaped with escapeshellcmd.
*
* @throws RuntimeException if shell_exec is unavailable, the command failed or an empty response.
*/
protected function executeCommand(string $command): string {
$output = @shell_exec(escapeshellcmd($command));
if (function_exists('shell_exec') === false) {
throw new RuntimeException('shell_exec unavailable');
}

$output = shell_exec(escapeshellcmd($command));
if ($output === false || $output === null || $output === '') {
throw new RuntimeException('No output for command: "' . $command . '"');
}

return $output;
}

Expand Down