使用 System.Diagnostics.Process 在标准输入上将 y 输入到 Plink?
Using System.Diagnostics.Process to input a y to a Plink on standard input?
如何在 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
}
不要!
验证主机密钥指纹是确保连接安全不可或缺的一部分。盲目接受任何主机密钥将使您容易受到 man-in-the-middle attacks.
而是使用 -hostkey
switch 提供 expected/known 主机密钥的指纹。
[string]$arguments = 'somehost -l somelogin -pw somepasswd ping -hostkey xx:xx:xx:xx:... -c 12 someOtherHost > /home/homeie/mePingTestResults.txt'
如何在 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
}
不要!
验证主机密钥指纹是确保连接安全不可或缺的一部分。盲目接受任何主机密钥将使您容易受到 man-in-the-middle attacks.
而是使用 -hostkey
switch 提供 expected/known 主机密钥的指纹。
[string]$arguments = 'somehost -l somelogin -pw somepasswd ping -hostkey xx:xx:xx:xx:... -c 12 someOtherHost > /home/homeie/mePingTestResults.txt'