Input details in cmd via Process
-
Hello, I have created a Process that runs a command from cmd and handles it's output using event Handler. I want to input user name when cmd asks for "Enter Username :". This comes immediately after 1st line. Then comes Enter Password where I want to pass password. Then their is no input and can use my event handler till I am done. I tried the following, but didn't work :
processInfo = new ProcessStartInfo("cmd.exe", "/C " + command); sb = new StringBuilder(); processInfo.UseShellExecute = false; processInfo.RedirectStandardOutput = true; processInfo.CreateNoWindow = true; public int ConnectToServer() { processInfo.RedirectStandardInput = true; process = Process.Start(processInfo); process.StandardInput.WriteLine("username"); process.StandardInput.Flush(); process.StandardInput.WriteLine("myPswd$"); process.StandardInput.Flush(); process.StandardInput.Close(); process.BeginOutputReadLine(); process.OutputDataReceived += new DataReceivedEventHandler(Process\_OutputDataReceived); } private void Process\_OutputDataReceived(object sender, DataReceivedEventArgs e) { string d = e.Data; if (!string.IsNullOrEmpty(d)) { if (d.indexOf("Completed")) { connected = true; } } }
It shows error authentication failed. I guess I didn't input username & passowrd on time, so this must have happened. How to know when the app is asking to write and to write proepr details ? I didn't find such way of inputting in any articles searched on internet. Any help is highly appreciated.
Thanks & Regards,
-
Hello, I have created a Process that runs a command from cmd and handles it's output using event Handler. I want to input user name when cmd asks for "Enter Username :". This comes immediately after 1st line. Then comes Enter Password where I want to pass password. Then their is no input and can use my event handler till I am done. I tried the following, but didn't work :
processInfo = new ProcessStartInfo("cmd.exe", "/C " + command); sb = new StringBuilder(); processInfo.UseShellExecute = false; processInfo.RedirectStandardOutput = true; processInfo.CreateNoWindow = true; public int ConnectToServer() { processInfo.RedirectStandardInput = true; process = Process.Start(processInfo); process.StandardInput.WriteLine("username"); process.StandardInput.Flush(); process.StandardInput.WriteLine("myPswd$"); process.StandardInput.Flush(); process.StandardInput.Close(); process.BeginOutputReadLine(); process.OutputDataReceived += new DataReceivedEventHandler(Process\_OutputDataReceived); } private void Process\_OutputDataReceived(object sender, DataReceivedEventArgs e) { string d = e.Data; if (!string.IsNullOrEmpty(d)) { if (d.indexOf("Completed")) { connected = true; } } }
It shows error authentication failed. I guess I didn't input username & passowrd on time, so this must have happened. How to know when the app is asking to write and to write proepr details ? I didn't find such way of inputting in any articles searched on internet. Any help is highly appreciated.
Thanks & Regards,
how about adding username and password as command arguments eg.
cmd.exe "username" "password"
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
----------------------------------------------- 128 bit encrypted signature, crack if you can
-
how about adding username and password as command arguments eg.
cmd.exe "username" "password"
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
----------------------------------------------- 128 bit encrypted signature, crack if you can
Thanks Dear, But no, can't add like that. Can either pass a txt file or input on screen. As I don't want to have txt file with readable username & password, so thought of this option.
Thanks & Regards,
-
Hello, I have created a Process that runs a command from cmd and handles it's output using event Handler. I want to input user name when cmd asks for "Enter Username :". This comes immediately after 1st line. Then comes Enter Password where I want to pass password. Then their is no input and can use my event handler till I am done. I tried the following, but didn't work :
processInfo = new ProcessStartInfo("cmd.exe", "/C " + command); sb = new StringBuilder(); processInfo.UseShellExecute = false; processInfo.RedirectStandardOutput = true; processInfo.CreateNoWindow = true; public int ConnectToServer() { processInfo.RedirectStandardInput = true; process = Process.Start(processInfo); process.StandardInput.WriteLine("username"); process.StandardInput.Flush(); process.StandardInput.WriteLine("myPswd$"); process.StandardInput.Flush(); process.StandardInput.Close(); process.BeginOutputReadLine(); process.OutputDataReceived += new DataReceivedEventHandler(Process\_OutputDataReceived); } private void Process\_OutputDataReceived(object sender, DataReceivedEventArgs e) { string d = e.Data; if (!string.IsNullOrEmpty(d)) { if (d.indexOf("Completed")) { connected = true; } } }
It shows error authentication failed. I guess I didn't input username & passowrd on time, so this must have happened. How to know when the app is asking to write and to write proepr details ? I didn't find such way of inputting in any articles searched on internet. Any help is highly appreciated.
Thanks & Regards,
-
Thanks, but I don't want such huge code for a small task. I believe such a thing can be done with no big pain. Couple of days ago I had seen a codeo nsome site that showed stdin & stdout using Process and cmd. I searched a lot but couldn't find that site back. On my start of process, immediately after 1 line, cmd needs to input username. The code I have written doesn't let the user write anything and just goes away. When and how can the app know that it needs to write/input something. If its the 1st time then its username, if 2nd time then password. That's it. Then no input only get the output. But am not able to know when and how to know. Just saw WaitForInputIdle(). Might be that can help me. Will try with that.
Thanks & Regards,
-
Hello, I have created a Process that runs a command from cmd and handles it's output using event Handler. I want to input user name when cmd asks for "Enter Username :". This comes immediately after 1st line. Then comes Enter Password where I want to pass password. Then their is no input and can use my event handler till I am done. I tried the following, but didn't work :
processInfo = new ProcessStartInfo("cmd.exe", "/C " + command); sb = new StringBuilder(); processInfo.UseShellExecute = false; processInfo.RedirectStandardOutput = true; processInfo.CreateNoWindow = true; public int ConnectToServer() { processInfo.RedirectStandardInput = true; process = Process.Start(processInfo); process.StandardInput.WriteLine("username"); process.StandardInput.Flush(); process.StandardInput.WriteLine("myPswd$"); process.StandardInput.Flush(); process.StandardInput.Close(); process.BeginOutputReadLine(); process.OutputDataReceived += new DataReceivedEventHandler(Process\_OutputDataReceived); } private void Process\_OutputDataReceived(object sender, DataReceivedEventArgs e) { string d = e.Data; if (!string.IsNullOrEmpty(d)) { if (d.indexOf("Completed")) { connected = true; } } }
It shows error authentication failed. I guess I didn't input username & passowrd on time, so this must have happened. How to know when the app is asking to write and to write proepr details ? I didn't find such way of inputting in any articles searched on internet. Any help is highly appreciated.
Thanks & Regards,
I think you don't need anything going asynchronous here. Remove the OutputDataReceived event handler and the invoke to BeginOutputReadLine, so you can do it synchronously. For example:
I've not tested the code, but I guess it should work. The OutputDataReceived is only fired when a line is written on the output stream, so if there is not an end of line when it is asking for a username, you will not receive the event.