Initial commit device-management
tests / ci (push) Has been cancelled

This commit is contained in:
Wian Drs
2026-08-25 09:42:00 +07:00
commit 1e80be180e
1159 changed files with 149545 additions and 0 deletions
+72
View File
@@ -0,0 +1,72 @@
<?php
namespace App\Network\Clients\Zte;
use phpseclib3\Net\SSH2;
use RuntimeException;
class ZteSshClient
{
private SSH2 $ssh;
public function __construct(private readonly string $host, private readonly int $port, private readonly int $timeout = 10) {}
public function connect(string $username, string $password): void
{
$this->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;
}
}