Using System.Diagnostics.Process to input a y to a Plink on standard input?

谁说我不能喝 提交于 2019-12-08 07:51:07

问题


How do I pass a Y into a process started by a System.Diagnostic.Process in PowerShell?

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
}

When I call it I still get:

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)

I've read elsewhere that it's possible to try something like echo y | plink ... and have it read it in piped from standard input, but I want to have more control over it then just that.


回答1:


Just move your StandardInput line below where the process is started.

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
}



回答2:


Do not!

Verifying host key fingerprint is an integral part of securing your connection. Blindly accepting any host key will make you vulnerable to the man-in-the-middle attacks.


Instead, use the -hostkey switch to provide the fingerprint of the expected/known host key.

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


来源:https://stackoverflow.com/questions/54227684/using-system-diagnostics-process-to-input-a-y-to-a-plink-on-standard-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!