C# 如何运行cygwin scp并使用Process和StandardInputWriter输入密码
本文关键字:Process StandardInputWriter 密码 输入 scp 何运行 运行 cygwin | 更新日期: 2023-09-27 18:09:29
使用此代码,我看到登录窗口提示输入密码,但我似乎无法将密码写入 shell 窗口。
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# 与窗口输入/输出进行交互。
试试这个:
[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
。
此代码按预期正常工作,无需调用 Flush 或 Sleep:
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
只是在等待残疾人士吗?