[英]How to suppress "keyboard-interactive" prompts in plink.exe
I have a PowerShell script that calls plink.exe
regularly.我有一个定期调用
plink.exe
的 PowerShell 脚本。 Normally, the two output lines about keyboard-interactive prompts are simply annoying.通常,关于键盘交互提示的两行 output 简直令人讨厌。
However, when run using Start-Job
, they get output as error text as soon as I call Receive-Job
.但是,当使用
Start-Job
运行时,一旦我调用Receive-Job
Job ,它们就会得到 output 作为错误文本。
Is there any way to suppress these?有没有办法抑制这些? I'd rather not suppress all errors.
我宁愿不压制所有错误。
My test code:我的测试代码:
$test_scriptblock = [scriptblock]::Create(@"
param(
`$argumentlist
)
`$pw = `$argumentlist.pw
& 'C:\Program Files\Putty\Plink.exe' -ssh `"admin@*.*.*.*" -pw `$pw -batch whoami
"@)
$testParm = @{
pw = Read-Host "password"
}
$testjob = Start-Job -scriptblock $test_scriptblock -Argumentlist $testParm
$i = 0
do {
$i++
sleep 2
$results = Receive-Job $testjob
ForEach ($result in $results) {
Write-Host $result
}
if ($testjob.State -eq "Completed") {
$jobcompleted = $true
}
If ($i -gt 10) {
Stop-job $testjob
$jobcompleted = $true
}
} until ($jobcompleted)
Ir was a bit cumbersome, but finally I managed to suppress the "keyboard-interactive" messages this way: Ir 有点麻烦,但最后我设法通过这种方式抑制了“键盘交互”消息:
[String] $Plink = 'C:\Program Files\PuTTY\plink.exe'
[Array] $PlinkPar = @("-ssh", "-l", $usr, "-pw", $pwd, $hst) # Set plink parameters
[Boolean] $PlinkOK = $True
Write-Host "Accept possibly unknown host key"
"y" | & $Plink $PlinkPar "exit" 2>&1 | Tee-Object -Variable PlinkOut | Out-Null
$PlinkOut | Foreach-Object {
$PlinkStr = $_.ToString()
If ($_ -is [System.Management.Automation.ErrorRecord]) {
If (! $PlinkStr.Contains("eyboard-interactive")) {
Write-Host "Error: $PlinkStr"
$PlinkOK = $False
}
} else {
Write-Host "$PlinkStr"
}
}
If (! $PlinkOK) { exit }
$PlinkPar += "-batch
And the output is like this:输出是这样的:
>powershell .\InstCR.ps1 -usr myuser -pwd mypassword -hst doesnotexist
Accept possibly unknown host key
Error: Unable to open connection:
Error: Host does not exist
This plink call is just to accept a possibly unknown host key (without "-batch" and piping the "y" to answer the prompt).这个 plink 调用只是为了接受一个可能未知的主机密钥(没有“-batch”和管道“y”来回答提示)。 Then "-batch" is added to the Plink parameters to be used on all subsequent plink calls.
然后将“-batch”添加到 Plink 参数中,以用于所有后续 plink 调用。
Just add the stderr redirect to your plink or pscp commandline, to an extra dummy file, like只需将 stderr 重定向添加到您的 plink 或 pscp 命令行,添加到一个额外的虚拟文件,例如
pscp ... 2> stderr.txt
With a caveat that it may swallow other valid error msgs, at your own risk:)需要注意的是,它可能会吞下其他有效的错误消息,风险自负:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.