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

标签 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 ...这样的东西并将其从标准输入中以管道方式读取,但是我想对此进行更多控制。

最佳答案

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

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
    $pident = ($p.Start()).Id
    Write-Host("pid: $($pident)");
    $p.StandardInput.WriteLine("Y") # Pass a Y to stdin ignore that...
    #$p.WaitForExit();
    #$p.StandardOutput.ReadToEnd();
    return $p
}

关于powershell - 使用System.Diagnostics.Process在标准输入上将y输入到Plink?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54227684/

相关文章:

git - 如何将 ssh 公钥添加到特定的 git 存储库

ssh - PuTTY - 无法使用 key 文件(不是私钥)

excel - 如何在 PowerShell 中获取打开的 Excel 工作簿列表

powershell字符串仅包含数字

linux - ssh cronjob with while 仅执行 1 次

java - 将 sshpass 与 bash 一起使用可以从命令行运行,而不是从 java exec 运行

windows - 如何配置pycharm以使用腻子或OpenSSH SSH堡垒主机

PHP脚本被杀死没有解释

object - Powershell:将所有项目/属性加载到新对象中

powershell - 如何检查调用者是否设置了 PowerShell 可选参数