update rm deadcode
This commit is contained in:
@@ -4,6 +4,9 @@ namespace App\Http\Services\vCenter\Concerns;
|
||||
|
||||
use App\Http\Facade\SignRSA;
|
||||
use App\Models\InfraManagement;
|
||||
use App\Support\PowerCliCertificatePolicy;
|
||||
use App\Support\PowerShellEscaper;
|
||||
use App\Support\SshHostKeyVerifier;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use phpseclib3\Net\SSH2;
|
||||
@@ -19,6 +22,11 @@ use phpseclib3\Net\SSH2;
|
||||
*/
|
||||
trait ManagesSsh
|
||||
{
|
||||
private function powerCliCertificateLine(): string
|
||||
{
|
||||
return PowerCliCertificatePolicy::configurationLine();
|
||||
}
|
||||
|
||||
// ─── Auth ────────────────────────────────────────────────────────────────
|
||||
|
||||
private function LoginAuth(): SSH2|false
|
||||
@@ -41,6 +49,8 @@ trait ManagesSsh
|
||||
|
||||
$connection = new SSH2($sshHost, $sshPort);
|
||||
|
||||
SshHostKeyVerifier::verify($connection, $sshHost);
|
||||
|
||||
if ($connection->login($infra->username, SignRSA::DecryptFunc($infra->password))) {
|
||||
$connection->setTimeout(0);
|
||||
return $connection;
|
||||
@@ -83,7 +93,7 @@ trait ManagesSsh
|
||||
try {
|
||||
$psCommand = 'powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "'
|
||||
. 'Import-Module VMware.PowerCLI; '
|
||||
. 'Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false -Scope Session; '
|
||||
. $this->powerCliCertificateLine() . '; '
|
||||
. '$cred = Import-Clixml -Path \'C:\secure\vcenter.cred\'; '
|
||||
. 'Connect-VIServer -Server \'' . $this->apiUri . '\' -Credential $cred; '
|
||||
. $cmd . '; '
|
||||
@@ -113,8 +123,8 @@ trait ManagesSsh
|
||||
return 'Login_Fail';
|
||||
}
|
||||
|
||||
$action = 'powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass '
|
||||
. '-Command "' . str_replace('"', '\"', $psCommand) . '"';
|
||||
$action = 'powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -EncodedCommand '
|
||||
. PowerShellEscaper::encodedCommand($psCommand);
|
||||
|
||||
try {
|
||||
$out = $auth->exec($action);
|
||||
@@ -136,7 +146,6 @@ trait ManagesSsh
|
||||
return 'Login_Fail';
|
||||
}
|
||||
|
||||
$esc = fn(string $v) => str_replace("'", "''", $v);
|
||||
$script = strtr(
|
||||
<<<'PS'
|
||||
$ProgressPreference = 'SilentlyContinue'
|
||||
@@ -145,7 +154,7 @@ $WarningPreference = 'SilentlyContinue'
|
||||
$ErrorActionPreference = 'Stop'
|
||||
|
||||
Import-Module VMware.PowerCLI -ErrorAction Stop
|
||||
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Scope Session -Confirm:$false | Out-Null
|
||||
<CERT>
|
||||
Connect-VIServer -Server '<SERVER>' -User '<USER>' -Password '<PASS>' | Out-Null
|
||||
|
||||
& {
|
||||
@@ -153,15 +162,16 @@ Connect-VIServer -Server '<SERVER>' -User '<USER>' -Password '<PASS>' | Out-Null
|
||||
} | Out-String
|
||||
PS,
|
||||
[
|
||||
'<SERVER>' => $esc($this->apiUri ?? ''),
|
||||
'<USER>' => $esc($this->username ?? ''),
|
||||
'<PASS>' => $esc($this->password ?? ''),
|
||||
'<CERT>' => $this->powerCliCertificateLine(),
|
||||
'<SERVER>' => PowerShellEscaper::singleQuote($this->apiUri ?? ''),
|
||||
'<USER>' => PowerShellEscaper::singleQuote($this->username ?? ''),
|
||||
'<PASS>' => PowerShellEscaper::singleQuote($this->password ?? ''),
|
||||
'<CMD>' => $cmd,
|
||||
]
|
||||
);
|
||||
|
||||
$encoded = base64_encode(mb_convert_encoding($script, 'UTF-16LE', 'UTF-8'));
|
||||
$action = "powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -EncodedCommand $encoded";
|
||||
$action = 'powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -EncodedCommand '
|
||||
. PowerShellEscaper::encodedCommand($script);
|
||||
|
||||
try {
|
||||
$out = $auth->exec($action);
|
||||
@@ -183,7 +193,6 @@ PS,
|
||||
return 'Login_Fail';
|
||||
}
|
||||
|
||||
$esc = fn(string $v) => str_replace("'", "''", $v);
|
||||
$script = strtr(
|
||||
<<<'PS'
|
||||
$ProgressPreference = 'SilentlyContinue'
|
||||
@@ -193,7 +202,7 @@ $ErrorActionPreference = 'Stop'
|
||||
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
||||
|
||||
Import-Module VMware.PowerCLI -ErrorAction Stop
|
||||
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Scope Session -Confirm:$false | Out-Null
|
||||
<CERT>
|
||||
Connect-VIServer -Server '<SERVER>' -User '<USER>' -Password '<PASS>' | Out-Null
|
||||
|
||||
& {
|
||||
@@ -201,15 +210,16 @@ Connect-VIServer -Server '<SERVER>' -User '<USER>' -Password '<PASS>' | Out-Null
|
||||
}
|
||||
PS,
|
||||
[
|
||||
'<SERVER>' => $esc($this->apiUri ?? ''),
|
||||
'<USER>' => $esc($this->username ?? ''),
|
||||
'<PASS>' => $esc($this->password ?? ''),
|
||||
'<CERT>' => $this->powerCliCertificateLine(),
|
||||
'<SERVER>' => PowerShellEscaper::singleQuote($this->apiUri ?? ''),
|
||||
'<USER>' => PowerShellEscaper::singleQuote($this->username ?? ''),
|
||||
'<PASS>' => PowerShellEscaper::singleQuote($this->password ?? ''),
|
||||
'<CMD>' => $cmd,
|
||||
]
|
||||
);
|
||||
|
||||
$encoded = base64_encode(mb_convert_encoding($script, 'UTF-16LE', 'UTF-8'));
|
||||
$action = "powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -EncodedCommand $encoded";
|
||||
$action = 'powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -EncodedCommand '
|
||||
. PowerShellEscaper::encodedCommand($script);
|
||||
|
||||
try {
|
||||
$out = $auth->exec($action);
|
||||
|
||||
Reference in New Issue
Block a user