代码之家  ›  专栏  ›  技术社区  ›  leeand00

使用system.diagnostics.process将y输入到标准输入上的plink?

  •  1
  • leeand00  · 技术社区  · 6 年前

    我如何通过 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 ... 让它从标准输入端以管道的形式读取它,但我希望对它有更多的控制,然后就这样。

    2 回复  |  直到 6 年前
        1
  •  1
  •   Paul G    6 年前

    只需将您的标准输入行移到流程启动位置的下方。

    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
  •  1
  •   Martin Prikryl    6 年前

    不要!

    验证主机密钥指纹是确保连接安全的重要组成部分。盲目接受任何主机密钥都会使您容易受到 man-in-the-middle attacks .


    相反,使用 -hostkey switch 提供预期/已知主机密钥的指纹。

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