From 71878b46845e1a292c8c3193be0ed85471ec2943 Mon Sep 17 00:00:00 2001 From: baodev0 Date: Mon, 13 Jul 2026 15:54:31 +0700 Subject: [PATCH] Enhance SSH execution in VsphereHostSshService to capture stdout and stderr; improve error handling for command execution --- app/Services/VsphereHostSshService.php | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/app/Services/VsphereHostSshService.php b/app/Services/VsphereHostSshService.php index 8ae1841..4c8eb1e 100644 --- a/app/Services/VsphereHostSshService.php +++ b/app/Services/VsphereHostSshService.php @@ -247,14 +247,28 @@ final class VsphereHostSshService ); } - $output = $client->exec($remoteCmd); + $stdout = $client->exec($remoteCmd); + $stderr = $client->getStdError(); Log::info('VsphereHostSshService: SSH executed', [ - 'host' => $config['host'], - 'port' => $config['port'], + 'host' => $config['host'], + 'port' => $config['port'], + 'stdout' => trim((string) $stdout), + 'stderr' => trim((string) $stderr), ]); - return (string) $output; + if ($stdout === false) { + throw new \RuntimeException( + "SSH exec failed on {$config['host']}:{$config['port']}. stderr: " . trim((string) $stderr) + ); + } + + // Windows SSH servers sometimes write script output to stderr instead of stdout + if (trim($stdout) === '' && trim((string) $stderr) !== '') { + return (string) $stderr; + } + + return (string) $stdout; } /**