@@ -15,6 +15,7 @@ param(
|
||||
[string]$ServerInfoJsonPath = "",
|
||||
[string]$SshKeyPath = "",
|
||||
[string]$KnownHostsPath = "",
|
||||
[string]$SshPassword = "",
|
||||
[string]$DatabaseTargetName = "mysql02",
|
||||
[int]$DatabasePort = 3306,
|
||||
[string]$DatabaseName = "jayucloud_salon",
|
||||
@@ -348,7 +349,7 @@ function Get-ServersFromJson {
|
||||
Host = [string]$item.Host
|
||||
Port = if ($item.Port) { [int]$item.Port } else { 22 }
|
||||
User = if ($item.User) { [string]$item.User } else { "root" }
|
||||
Password = ""
|
||||
Password = $SshPassword
|
||||
Purpose = if ($item.Purpose) { [string]$item.Purpose } else { "" }
|
||||
Note = if ($item.Note) { [string]$item.Note } else { "" }
|
||||
Environment = if ($item.Environment) { [string]$item.Environment } else { "prod" }
|
||||
@@ -410,7 +411,21 @@ function Invoke-RemoteShell {
|
||||
[switch]$Sensitive
|
||||
)
|
||||
|
||||
if ($SshKeyPath) {
|
||||
if ($SshPassword) {
|
||||
$sshpass = Get-Command sshpass -ErrorAction SilentlyContinue
|
||||
if ((-not $DryRun) -and (-not $sshpass)) {
|
||||
throw "找不到 sshpass,无法使用 SSH 密码登录。"
|
||||
}
|
||||
$sshpassPath = if ($sshpass) { $sshpass.Source } else { "sshpass" }
|
||||
$env:SSHPASS = $SshPassword
|
||||
try {
|
||||
Invoke-Native $sshpassPath @("-e", "ssh", "-o", "StrictHostKeyChecking=accept-new", "-p", [string]$Server.Port, (Get-SshTarget $Server), $Command) -Sensitive:$Sensitive -RemoteMutation
|
||||
}
|
||||
finally {
|
||||
Remove-Item Env:SSHPASS -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
elseif ($SshKeyPath) {
|
||||
$sshArgs = @("-o", "BatchMode=yes", "-o", "StrictHostKeyChecking=yes", "-i", $SshKeyPath)
|
||||
if ($KnownHostsPath) {
|
||||
$sshArgs += @("-o", "UserKnownHostsFile=$KnownHostsPath")
|
||||
@@ -443,7 +458,21 @@ function Copy-FileToRemote {
|
||||
throw "本地上传文件不存在:$LocalPath"
|
||||
}
|
||||
|
||||
if ($SshKeyPath) {
|
||||
if ($SshPassword) {
|
||||
$sshpass = Get-Command sshpass -ErrorAction SilentlyContinue
|
||||
if ((-not $DryRun) -and (-not $sshpass)) {
|
||||
throw "找不到 sshpass,无法使用 SSH 密码登录。"
|
||||
}
|
||||
$sshpassPath = if ($sshpass) { $sshpass.Source } else { "sshpass" }
|
||||
$env:SSHPASS = $SshPassword
|
||||
try {
|
||||
Invoke-Native $sshpassPath @("-e", "scp", "-o", "StrictHostKeyChecking=accept-new", "-P", [string]$Server.Port, $LocalPath, "$(Get-SshTarget $Server):$RemotePath") -Sensitive:$Sensitive -RemoteMutation
|
||||
}
|
||||
finally {
|
||||
Remove-Item Env:SSHPASS -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
elseif ($SshKeyPath) {
|
||||
$scpArgs = @("-o", "BatchMode=yes", "-o", "StrictHostKeyChecking=yes", "-i", $SshKeyPath)
|
||||
if ($KnownHostsPath) {
|
||||
$scpArgs += @("-o", "UserKnownHostsFile=$KnownHostsPath")
|
||||
|
||||
Reference in New Issue
Block a user