Reputation: 26392
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.
Upvotes: 1
Views: 981
Reputation: 202262
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'
Upvotes: 1
Reputation: 1229
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
}
Upvotes: 0