change ssh2 into new SSH2

This commit is contained in:
pbao9
2026-01-21 10:27:14 +07:00
parent b97676d557
commit fb6b2159da
3 changed files with 15 additions and 38 deletions
+10 -32
View File
@@ -300,11 +300,7 @@ class vCenter
$action = "powershell.exe; Connect-viserver -Server '$this->apiUri' -User '$this->username' -Password \"'$this->password'\" -SaveCredentials; $cmd";
//Run action
$stream = ssh2_exec($auth, $action);
stream_set_blocking($stream, true);
return stream_get_contents($stream);
return $auth->exec($action);
} catch (Exception $error) {
return $error->getCode();
}
@@ -314,9 +310,10 @@ class vCenter
{
try {
$connection = ssh2_connect('10.10.10.173', 22);
$connection = new SSH2('10.10.10.173', 22);
if (ssh2_auth_password($connection, 'administrator', 'Thegioiso@123**$$8899TgTk2')) {
if ($connection->login('administrator', 'Thegioiso@123**$$8899TgTk2')) {
$connection->setTimeout(0); // No timeout
return $connection;
}
return false;
@@ -351,14 +348,8 @@ class vCenter
. '-Command "' . str_replace('"', '\"', $psCommand) . '"';
try {
$stream = ssh2_exec($auth, $action);
if (!$stream)
throw new \RuntimeException("ssh2_exec failed");
$err = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
stream_set_blocking($stream, true);
stream_set_blocking($err, true);
$out = stream_get_contents($stream) ?: '';
$errOut = stream_get_contents($err) ?: '';
$out = $auth->exec($action);
$errOut = $auth->getStdError();
return $out . ($errOut ? "\n[STDERR]\n" . $errOut : '');
} catch (\Throwable $e) {
return "Error: " . $e->getMessage();
@@ -412,14 +403,8 @@ PS;
$action = "powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -EncodedCommand $encoded";
try {
$stream = ssh2_exec($auth, $action);
if (!$stream)
return "ssh2_exec failed";
$err = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
stream_set_blocking($stream, true);
stream_set_blocking($err, true);
$out = stream_get_contents($stream) ?: '';
$errOut = stream_get_contents($err) ?: '';
$out = $auth->exec($action);
$errOut = $auth->getStdError();
return $out . ($errOut ? "\n[STDERR]\n" . $errOut : '');
} catch (\Throwable $e) {
return "Error: " . $e->getMessage();
@@ -467,15 +452,8 @@ PS;
$action = "powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -EncodedCommand $encoded";
try {
$stream = ssh2_exec($auth, $action);
if (!$stream)
return "ssh2_exec failed";
$err = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
stream_set_blocking($stream, true);
stream_set_blocking($err, true);
$out = stream_get_contents($stream) ?: '';
$errOut = stream_get_contents($err) ?: '';
$out = $auth->exec($action);
$errOut = $auth->getStdError();
if ($expectJson) {
// CẮT JSON cuối: {...} hoặc [...]