2011-09-22 5 views
0

이 코드를 사용하면 로그인 창이 암호를 묻는 메시지가 나타나지만 암호를 셸 창에 쓸 수없는 것 같습니다.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#을 사용하여 Windows 입/출력과 상호 작용할 수 있음을 알고 있습니다.

답변

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"); 
    } 
} 

는 당신에게 당신이 cygwin 단지 비밀번호가 기다리고 있음을 100% 확신?

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(); 

편집이 : pass의 말에 \n을 포함해야합니다.