fix: use script command for PTY instead of modifying SSHService
- Revert SSHService exec() back to original (remove PTY parameter) - AgentService now writes installer to /tmp/ first, then runs via script -q -c to create a PTY for sudo (handles requiretty) - Falls back to direct sudo bash if script is unavailable - Cleans up temp files after execution - Added mkdir -p for /usr/local/bin in installer - Added file verification and byte-count after writing agent binary
This commit is contained in:
@@ -51,9 +51,11 @@ class AgentService
|
||||
|
||||
$b64 = base64_encode($script);
|
||||
$remoteCommand = sprintf(
|
||||
'echo %s | base64 -d | sudo bash -s -- %s %s 2>&1',
|
||||
'echo %s | base64 -d > /tmp/_sm_install.sh && chmod +x /tmp/_sm_install.sh && (script -q -c "sudo bash /tmp/_sm_install.sh %s %s" /dev/null < /dev/null 2>/dev/null || sudo bash /tmp/_sm_install.sh %s %s) 2>&1; rc=$?; rm -f /tmp/_sm_install.sh; exit $rc',
|
||||
escapeshellarg($b64),
|
||||
escapeshellarg($serverUrl),
|
||||
escapeshellarg($agentKey),
|
||||
escapeshellarg($serverUrl),
|
||||
escapeshellarg($agentKey)
|
||||
);
|
||||
|
||||
@@ -63,7 +65,7 @@ class AgentService
|
||||
return ['success' => false, 'error' => 'SSH connection failed'];
|
||||
}
|
||||
|
||||
$result = $ssh->exec($remoteCommand, 120, true);
|
||||
$result = $ssh->exec($remoteCommand, 120);
|
||||
$ssh->disconnect();
|
||||
|
||||
$output = trim($result['output']);
|
||||
@@ -117,7 +119,7 @@ class AgentService
|
||||
$script = file_get_contents($scriptPath);
|
||||
|
||||
$remoteCommand = sprintf(
|
||||
'echo %s | base64 -d | sudo bash 2>&1',
|
||||
'echo %s | base64 -d > /tmp/_sm_uninstall.sh && chmod +x /tmp/_sm_uninstall.sh && (script -q -c "sudo bash /tmp/_sm_uninstall.sh" /dev/null < /dev/null 2>/dev/null || sudo bash /tmp/_sm_uninstall.sh) 2>&1; rc=$?; rm -f /tmp/_sm_uninstall.sh; exit $rc',
|
||||
escapeshellarg(base64_encode($script))
|
||||
);
|
||||
|
||||
@@ -127,7 +129,7 @@ class AgentService
|
||||
return ['success' => false, 'error' => 'SSH connection failed'];
|
||||
}
|
||||
|
||||
$result = $ssh->exec($remoteCommand, 60, true);
|
||||
$result = $ssh->exec($remoteCommand, 60);
|
||||
$ssh->disconnect();
|
||||
|
||||
$output = trim($result['output']);
|
||||
|
||||
@@ -73,7 +73,7 @@ class SSHService
|
||||
}
|
||||
}
|
||||
|
||||
public function exec(string $command, int $timeout = 0, bool $pty = false): array
|
||||
public function exec(string $command, int $timeout = 0): array
|
||||
{
|
||||
if ($this->connection === null || !$this->connection->isConnected()) {
|
||||
throw new \RuntimeException('No active SSH connection');
|
||||
@@ -83,22 +83,10 @@ class SSHService
|
||||
|
||||
$this->connection->setTimeout($timeout);
|
||||
|
||||
if ($pty) {
|
||||
$command .= '; echo "___EXIT___:$?"';
|
||||
}
|
||||
|
||||
$output = $this->connection->exec($command, null, $pty);
|
||||
$output = $this->connection->exec($command);
|
||||
$exitStatus = $this->connection->getExitStatus();
|
||||
$stderr = $this->connection->getStdError() ?? '';
|
||||
|
||||
if ($pty && ($exitStatus === false || $exitStatus === null)) {
|
||||
if (preg_match('/___EXIT___:(\d+)/', $output, $m)) {
|
||||
$exitStatus = (int) $m[1];
|
||||
$output = str_replace($m[0] . "\n", '', $output);
|
||||
$output = str_replace($m[0], '', $output);
|
||||
}
|
||||
}
|
||||
|
||||
$this->logger->info("SSH command executed", [
|
||||
'command' => $command,
|
||||
'exit_status' => $exitStatus,
|
||||
|
||||
Reference in New Issue
Block a user