[英]Use encrypted password for Plink/PuTTY
我想在 PowerShell 中加密密碼並將其與plink
和putty
一起使用。
是的,我知道它只需要明文密碼(使用 SecureString for plink.exe 命令的密碼加密)。
不,我不會使用生成的密鑰,因為我們不支持它。
我的問題:
putty
或plink
中為-pw
標志使用加密密碼-i
而不是-pw
我的securePass.ps1
代碼:
$password = read-host -prompt "Enter your Password"
write-host "$password is password"
$secure = ConvertTo-SecureString $password -force -asPlainText
$bytes = ConvertFrom-SecureString $secure
$bytes | out-file C:\encrypted_password1.txt
主要:
$securePass = Get-Content C:\encrypted_password1.txt
$pass = $securePass | ConvertTo-SecureString
plink -batch -ssh $defUser@$srv -pw $pass
putty -ssh $defUser@$srv -pw $pass
如您所知,您不能對 PuTTY/Plink 使用加密密碼 ( SecureString
)。
您所能做的就是解密安全字符串並將解密后的純文本密碼傳遞給 PuTTY/Plink。
對於解密,請參閱PowerShell - 將 System.Security.SecureString 解碼為可讀密碼:
$securePass = Get-Content C:\encrypted_password1.txt
$pass = $securePass | ConvertTo-SecureString
$Ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($pass)
$decrypted = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Ptr)
[System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Ptr)
plink -batch -ssh $defUser@$srv -pw $decrypted
PuTTY 0.77 Plink 新支持-pwfile
開關,允許通過本地文本文件(雖然仍然是純文本)以更安全的方式傳遞密碼。
你的問題2)沒有任何意義。 您寫道,您不能使用密鑰。 所以你不能使用-i
開關。 更不用說使用一些“生成的密碼”了。
$Credential = $(Get-Credential)
$user = $Credential.GetNetworkCredential().Username
$pass = $Credential.GetNetworkCredential().Password
是我在使用 -pw 的腳本中使用的; $ $putty -ssh $server -l $user -pw $pass -m $command
我知道你是說你做了 -I 而不是 -pw 但是我發現這很好用,因為在任何地方都沒有存儲密碼的文件。
這是我的解決方案,它在菜單循環中運行。 效果很好。 我只需要“緩存”我輸入的輸入或(將先前輸入的憑據自動傳遞到對話框中)否則每次我必須重新輸入憑據。
$Key = New-Object Byte[] 32
[Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($Key)
$Key | Out-File AES.key
(get-credential).Password | ConvertFrom- SecureString -key (get-content AES.key) | set-content "AESPassword.txt"
$password = Get-Content AESPassword.txt | ConvertTo-SecureString -Key (Get-Content AES.key)
$credential = New-Object System.Management.Automation.PsCredential($env:userName,$password)
$ServerName = Read-Host -Prompt "What is the server name?"
$Command = ".\plink.exe"
$arg1 = '-t'
$arg2 = $credential.GetNetworkCredential().username+'@'+$ServerName
$arg3 = '-pw'
$arg4 = $credential.GetNetworkCredential().Password
$arg5 = $scriptcmd
#Write-Output $Command $arg1 $arg2 $arg3 $arg4 $arg5
& $Command $arg1 $arg2 $arg3 $arg4 $arg5
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.