diff --git a/install/agent-install.sh b/install/agent-install.sh index eeb3348..6706f89 100644 --- a/install/agent-install.sh +++ b/install/agent-install.sh @@ -21,6 +21,7 @@ echo "[1/5] Creating config directory..." mkdir -p "$CONFIG_DIR" echo "[2/5] Installing agent binary..." +mkdir -p "$(dirname "$AGENT_BIN")" cat > "$AGENT_BIN" << 'AGENTSCRIPT' #!/bin/bash set -e @@ -49,7 +50,6 @@ collect_metrics() { } push_metrics() { - local payload payload=$(cat < "$CONFIG_FILE" << EOF SERVER_URL="$SERVER_URL" diff --git a/src/Services/AgentService.php b/src/Services/AgentService.php index e092679..c8f4469 100644 --- a/src/Services/AgentService.php +++ b/src/Services/AgentService.php @@ -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']); diff --git a/src/Services/SSHService.php b/src/Services/SSHService.php index 29d5f3c..1702945 100755 --- a/src/Services/SSHService.php +++ b/src/Services/SSHService.php @@ -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,