I'm trying to do this:
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("git config --global user.name \"My Name\"");
sw.WriteLine("git config --global user.email \"my email\"");
sw.WriteLine("call start-ssh-agent");
sw.WriteLine("ssh-add c:\\temp\\ServerPull.ppk");
System.Threading.Thread.Sleep(10000);
sw.WriteLine(Environment.NewLine);
sw.WriteLine("git clone git@github.com:myrepo");
}
}
Console.WriteLine("Done");
}
Problem is that the "ssh-add" command wants a passphrase entering, and I can't make C# enter it. Any other commands after the Thread.Sleep go into the buffer until I actually enter something on the CMD box myself.
Console.Writeline() outputs into that same box, but it doesn't actually get "entered"
Edit: For clarity, I'm not looking to do a Console.ReadLine() or actually get input from a user. The commands are how I need them, but I need to automatically send a string to another application (ssh-add) that is asking for a passphrase. Writing the passphrase through sw.WriteLine does not work as the console wont execute any code if it's waiting for input.
EDIT: In writing my first edit, I had a eureka moment on some code suggestions from the comments. Ended up with this now:
const int VK_RETURN = 0x0D;
const int WM_KEYDOWN = 0x100;
static void Main(string[] args)
{
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("git config --global user.name \"my name\"");
sw.WriteLine("git config --global user.email \"my email\"");
sw.WriteLine("call start-ssh-agent");
var enterThread = new Thread(
new ThreadStart(
() =>
{
Thread.Sleep(10000);
var hWnd = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
PostMessage(hWnd, WM_KEYDOWN, VK_RETURN, 0);
}
));
enterThread.Start();
sw.WriteLine("ssh-add c:\\temp\\ServerPull.ppk");
sw.WriteLine("git clone myrepo");
sw.WriteLine("ping 127.0.0.1");
}
}
Console.WriteLine("Done");
}
[DllImport("User32.Dll", EntryPoint = "PostMessageA")]
private static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);
which sends the enter key after a delay. I should be able to modify this from there to send in whatever I need.