2011-09-22 24 views
0

使用此代碼,我看到登錄窗口提示輸入密碼,但我似乎無法將密碼寫入shell窗口。C#如何運行cygwin scp並使用Process和StandardInputWriter輸入密碼?

 Process scp = new Process(); 
     scp.StartInfo.FileName = @"c:\cygwin\bin\scp"; 
     scp.StartInfo.Arguments = "/cygdrive/c" + path + " " + username + "@" + ServerName + ":/cygdrive/c/Temp/."; 
     scp.StartInfo.UseShellExecute = false; 
     scp.StartInfo.RedirectStandardOutput = true; 
     scp.StartInfo.RedirectStandardError = true; 
     scp.StartInfo.RedirectStandardInput = true; 
     scp.Start(); 

     //I've tried this with no success: 
     using (StreamWriter sw = scp.StandardInput) 
     { 
      if (sw.BaseStream.CanWrite) 
      { 
       sw.WriteLine(pass); 
      } 
     } 
     // Another failed attempt: 
     scp.StandardInput.Write(pass + Environment.NewLine); 
     scp.StandardInput.Flush(); 
     Thread.Sleep(1000); 

我知道我可以得到這個與cygwin的期望,但寧願使用c#與窗口輸入/輸出交互。如預期,並沒有必要要求

回答

0

此代碼工作正常沖洗或睡眠:

Process p = new Process(); 
ProcessStartInfo info = new ProcessStartInfo(); 
info.FileName = "cmd.exe"; 
info.RedirectStandardInput = true; 
info.UseShellExecute = false; 

p.StartInfo = info; 
p.Start(); 

using (StreamWriter sw = p.StandardInput) 
{ 
    if (sw.BaseStream.CanWrite) 
    { 
     sw.WriteLine("dir"); 
    } 
} 

100%確保您cygwin只是等待PWD?

1

試試這個:

[DllImport("user32.dll")] 
    static extern bool SetForegroundWindow(IntPtr hWnd); 

    Process scp = new Process(); 
    scp.StartInfo.FileName = @"c:\cygwin\bin\scp"; 
    scp.StartInfo.Arguments = "/cygdrive/c" + path + " " + username + "@" + ServerName + ":/cygdrive/c/Temp/."; 
    scp.StartInfo.UseShellExecute = false; 
    scp.StartInfo.RedirectStandardOutput = true; 
    scp.StartInfo.RedirectStandardError = true; 
    scp.StartInfo.RedirectStandardInput = true; 
    scp.Start(); 

    Process[] p = Process.GetProcessesByName("cmd"); 
    SetForegroundWindow(p[0].MainWindowHandle); 
    SendKeys.SendWait(pass); 

    scp.WaitForExit(); 

編輯:一定要包括\npass結束。

相關問題