1

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.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
leeand00
  • 25,510
  • 39
  • 140
  • 297
  • 1
    At https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.standardinput?view=netframework-4.7.2 they don't write to StandardInput till aftter the process is started. Not sure whether that is the issue or not. – Kory Gill Jan 17 '19 at 01:35
  • @KoryGill Yup, that's it; I actually found it here: https://stackoverflow.com/questions/16098366/can-i-send-some-text-to-the-stdin-of-an-active-process-under-windows it's just confusing because in the `echo y | plink` statement it comes before it so you'd think there would be no waiting piping from standard input and you'd think that it would run echo first...but I guess not. Even in order of operations...I'd think it'd be run first but I guess not. – leeand00 Jan 17 '19 at 02:15

2 Answers2

1

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'
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • 1
    I like this. My answer was just to accomplish what the OP was asking but this is a more complete answer! – Paul G Jan 17 '19 at 18:27
  • I know it's wrong. I'm doing it because I have no other choice. I'm being rushed into higher numbers and we just press `y` anyway. – leeand00 Jan 25 '19 at 00:50
0

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
}
Paul G
  • 1,219
  • 7
  • 14