ssh = new SSH2($this->host, $this->port, $this->timeout); $this->ssh->setTimeout($this->timeout); if (! $this->ssh->login($username, $password)) { throw new RuntimeException('AUTHENTICATION_FAILED: login SSH ZTE ditolak.'); } } public function enterEnable(?string $enablePassword): void { $prompt = $this->readPrompt(); if (str_ends_with(trim($prompt), '#')) { return; } $this->ssh->write("enable\n"); $response = $this->ssh->read('/(?:Password\s*:|[#>]\s*$)/i', SSH2::READ_REGEX); if (preg_match('/Password\s*:/i', $response)) { if ($enablePassword === null || $enablePassword === '') { throw new RuntimeException('ENABLE_PASSWORD_REQUIRED: password enable ZTE belum diisi.'); } $this->ssh->write($enablePassword."\n"); $response = $this->readPrompt(); } if (! str_ends_with(trim($response), '#')) { throw new RuntimeException('ENABLE_FAILED: gagal masuk privileged mode ZTE.'); } } public function command(string $command): string { $this->ssh->write($command."\n"); $output = $this->readPrompt(); if (preg_match('/%(?:Error|Invalid|Incomplete|Ambiguous)/i', $output)) { preg_match('/%(?:Error|Invalid|Incomplete|Ambiguous)[^\r\n]*/i', $output, $detail); $safeDetail = str($detail[0] ?? 'ZTE menolak perintah CLI')->limit(180)->toString(); throw new RuntimeException('COMMAND_REJECTED: '.$safeDetail); } return $output; } public function disconnect(): void { if (isset($this->ssh)) { $this->ssh->disconnect(); } } private function readPrompt(): string { $output = $this->ssh->read('/(?:\([^\r\n]+\))?[#>]\s*$/', SSH2::READ_REGEX); if ($output === false || $output === '') { throw new RuntimeException('CONNECTION_TIMEOUT: prompt CLI ZTE tidak diterima.'); } return $output; } }