使用System.Diagnostics.Process在标准输入上将y输入到Plink?

时间:2019-01-17 01:16:12

标签: powershell ssh putty plink

如何在PowerShell中将Y传递到System.Diagnostic.Process启动的进程中?

function Start-NewPlinkProcess(
        [string]$pfile = 'plink.exe',
        [string]$arguments = 'somehost -l somelogin -pw somepasswd ping -c 12 someOtherHost > /home/homeie/mePingTestResults.txt'
    ){
    $p = New-Object System.Diagnostics.Process;
    $p.StartInfo.UseShellExecute = $false;
    $p.StartInfo.RedirectStandardOutput = $true;
    $p.StartInfo.RedirectStandardInput = $true;
    $p.StartInfo.FileName = $pfile;
    $p.StartInfo.Arguments = $arguments
    $p.StandardInput.WriteLine("Y") # Pass a Y to stdin ignore that...
    $pident = ($p.Start()).Id
    Write-Host("pid: $($pident)");
    #$p.WaitForExit();
    #$p.StandardOutput.ReadToEnd();
    return $p
}

当我调用它时,我仍然得到:

If you trust this host, enter "y" to add the key to
PuTTY's cache and carry on connecting.
If you want to carry on connecting just once, without
adding the key to the cache, enter "n".
If you do not trust this host, press Return to abandon the
connection.
Store key in cache? (y/n)

我在其他地方读过,可以尝试像echo y | plink ...这样的东西,并从标准输入中以管道方式读取它,但是我想对此进行更多控制。

2 个答案:

答案 0 :(得分:1)

只需将您的StandardInput行移动到开始该过程的位置。

FB.logout()

答案 1 :(得分:1)

请勿!

验证主机密钥指纹是保护连接不可或缺的一部分。盲目接受任何主机密钥将使您容易受到man-in-the-middle attacks的攻击。<​​/ p>


请使用-hostkey switch提供预期/已知主机密钥的指纹。

[string]$arguments = 'somehost -l somelogin -pw somepasswd ping -hostkey xx:xx:xx:xx:... -c 12 someOtherHost > /home/homeie/mePingTestResults.txt'