Использование System.Diagnostics.Process для ввода y в Plink на стандартный ввод? - PullRequest
0 голосов
/ 17 января 2019

Как передать Y в процесс, запущенный System.Diagnostic.Process в 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
}

Когда я звоню, я все равно получаю:

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 голосов
/ 17 января 2019

Не надо!

Проверка отпечатка ключа хоста является неотъемлемой частью защиты вашего соединения. Слепое принятие любого ключа хоста сделает вас уязвимым для атак «человек посередине» .


Вместо этого используйте переключатель -hostkey , чтобы предоставить отпечаток ожидаемого / известного ключа хоста.

[string]$arguments = 'somehost -l somelogin -pw somepasswd ping -hostkey xx:xx:xx:xx:... -c 12 someOtherHost > /home/homeie/mePingTestResults.txt'
0 голосов
/ 17 января 2019

Просто переместите строку 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
}
...