'How to suppress "keyboard-interactive" prompts in plink.exe

I have a PowerShell script that calls plink.exe regularly. Normally, the two output lines about keyboard-interactive prompts are simply annoying.

However, when run using Start-Job, they get output as error text as soon as I call Receive-Job.

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)


Solution 1:[1]

There's no way to suppress keyboard-interactive prompts in Plink.

I suggest you use a native .NET SSH implementation, like SSH.NET instead of driving an external console application.

Solution 2:[2]

Ir was a bit cumbersome, but finally I managed to suppress the "keyboard-interactive" messages this way:


    [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). Then "-batch" is added to the Plink parameters to be used on all subsequent plink calls.

Solution 3:[3]

Just add the stderr redirect to your plink or pscp commandline, to an extra dummy file, like

pscp ... 2> stderr.txt

With a caveat that it may swallow other valid error msgs, at your own risk :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martin Prikryl
Solution 2
Solution 3 Nathan W