简体   繁体   English

如何在 plink.exe 中抑制“键盘交互”提示

[英]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)

There's no way to suppress keyboard-interactive prompts in Plink.在 Plink 中无法抑制键盘交互提示。

I suggest you use a native .NET SSH implementation, like SSH.NET instead of driving an external console application.我建议您使用本机 .NET SSH 实现,例如SSH.NET,而不是驱动外部控制台应用程序。

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.

相关问题 如何在Powershell中处理键盘交互式身份验证提示? - How to handle keyboard-interactive authentication prompts in powershell? 使用SecureString进行plink.exe命令的密码加密 - Password encryption using SecureString for plink.exe command 无法将密码传递给sudo su-使用Powershell + plink.exe组合的用户名 - Cannot pass password to sudo su - username using Powershell + plink.exe combination 在 PowerShell 中回答 Plink hostkey 和 KEX 提示 - Answering Plink hostkey and KEX prompts in PowerShell PowerShell 中是否有用于交互式提示的模块或类似的东西? - Is there a Module or Something Similar for Interactive Prompts in PowerShell? 防止脚本中参数值的交互式提示 - Preventing interactive prompts for parameter values in scripts 如何在.exe窗口的一行中提供交互式输入 - How to give interactive inputs in a single line for .exe windows Powershell&Plink-如何捕获“访问被拒绝”消息 - Powershell & Plink - How to capture the message “Access Denied” 主机未知时如何捕获Plink故障 - How to catch plink failure when host not known 在没有任何用户提示的情况下安装CitrixReceiver.exe - Installing CitrixReceiver.exe without any user prompts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM