C# Process Call, Interact with Standard Input and Standard Output - c#

I have a button I click that executes a command. The command may prompt for some standard input and I need to respond to that input, the problem is the way the program runs may be different from day to day so I need to interpret the standard output and redirect standard input accordingly.
I have this simple piece of code that reads the standard output line by line and when it sees a prompt for password, it'll send standard input, however the program just hangs since it never sees the prompt for the password, but when I run the batch file the password prompt is there.
Here is the batch file I am calling to execute this test:
#echo off
echo This is a test of a prompt
echo At the prompt, Enter a response
set /P p1=Enter the Password:
echo you entered "%p1%"
Here is the output of that batch file when run from the command line:
C:\Projects\SPP\MOSSTester\SPPTester\bin\Debug>test4.bat
This is a test of a prompt
At the prompt, Enter a response
Enter the Password: Test1
you entered "Test1"
Here is the C# snippet I am using to call the batch file that is hanging:
var proc = new Process();
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.Arguments = "/c test4.bat";
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
//read the standard output and look for prompt for password
StreamReader sr = proc.StandardOutput;
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
Debug.WriteLine(line);
if (line.Contains("Password"))
{
Debug.WriteLine("Password Prompt Found, Entering Password");
proc.StandardInput.WriteLine("thepassword");
}
}
sr.Close();
proc.WaitForExit();
Here is the debug standard output I see, notice that I never see the prompt for the password, why is this? it just hangs?
This is a test of a prompt
At the prompt, Enter a response
Is there a way I can watch the standard output for prompting and react to it accordingly?

Your main problem is that you are working through the stream reader using sr.ReadLine() this is an issue because the prompt for a password halts until the user enters a new line (hits enter after typing the password).
So you need to read the stream 1 character at a time. This example should help you get started.
while (!sr.EndOfStream)
{
var inputChar = (char)sr.Read();
input.Append(inputChar);
if (StringBuilderEndsWith(input, Environment.NewLine))
{
var line = input.ToString();
input = new StringBuilder();
Debug.WriteLine(line);
}
else if (StringBuilderEndsWith(input, "Password:"))
{
Debug.WriteLine("Password Prompt Found, Entering Password");
proc.StandardInput.WriteLine("thepassword");
var line = input.ToString();
input = new StringBuilder();
Debug.WriteLine(line);
}
}
private static bool StringBuilderEndsWith(StringBuilder haystack, string needle)
{
var needleLength = needle.Length - 1;
var haystackLength = haystack.Length - 1;
for (int i = 0; i < needleLength; i++)
{
if (haystack[haystackLength - i] != needle[needleLength - i])
{
return false;
}
}
return true;
}

With the process class you can run the batch file directly. The standard I/O might not be getting through since it has to go through cmd first
proc.StartInfo.FileName = "test4.bat";
proc.StartInfo.Arguments = "";

Related

C# Running batch file with arguments using UseShellExecute read the content instead executing it

I'm trying to run a batch file with arguments and also redirect the output.
When I don't redirect the output and use UseShellExecute = true, the batch file runs with the arguments as expected.
When I use UseShellExecute = false (to do the redirect), then I see that the command-line opens for a split a second and then closes.
I've read the output, using string output = proc.StandardOutput.ReadToEnd();
and I see that it contains the batch file content...
Can someone help me understand why it happens?
Thank you for your help :)
Here's the relevant code:
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.CreateNoWindow = false;
proc.EnableRaisingEvents = false;
// the command
proc.StartInfo.FileName = command;
// the parameters of the command
proc.StartInfo.Arguments = args;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
try
{
// in case there is missing '"'
if (isContainQuote && index < 0)
{
str = "missing '\"' in the command " + commandStr;
}
else
{
proc.Start();
string output = proc.StandardOutput.ReadToEnd();
if (bWaitForExit)
{
proc.WaitForExit(m_SHELL_CMD_TIMEOUT);
str = "Succeed to run the command: " + commandStr + ", Output: " + proc.StandardOutput.ReadToEnd();
}
else
{
str = "Succeed to run the command: " + commandStr;
}
}
}
catch (Exception e)
{
str = e.Message + ". Failed to run the command: " + commandStr;
// return the error from the operation system
}
Seeing your batch file could help. By default batch files echo their commands to standard out.
Try adding an #ECHO OFF command to the beginning of the batch file to be sure that the batch file commands aren't echoed to the standard output.

Loading a cmd in a console application

I have a console application that executes some commands and outputs some logs.
I need the output that display the command and its result, like this one:
>Loading the database...
>mysql -uUser -pPassword myDbName < mydumpFile
ERROR 1045 (28000): Access denied for user 'User'#'localhost' (using password: Y
ES)
>End loading the databse...
I did the folowing:
void ImportData()
{
Program.Log("INFO", "Start importing data... <<<");
Process myProcess = new Process();
string mySqlArgs = string.Format(" -u{0} -p{1} {2} < \"{3}\"",
bddUser, bddPassword, databaseName, dumpPath);
ProcessStartInfo myProcessStartInfo =
new ProcessStartInfo("mysql", mySqlArgs);
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
StreamReader reader = myProcess.StandardOutput;
string theOutput = reader.ReadToEnd();
if (theOutput.Length > 0)
Program.Log("SQL", theOutput);
Program.Log("INFO", "END importing data >>>");
}
but this code
1) does not display the command itself (just the result)
2) the request perhaps should be bad formatted, because the result is like a format error in the mysql command
UPDATE: the new code is a litte bit better
Program.Log("INFO", "Start importing Materials... <<<".Fill(code));
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
/* execute "mysql -uUser -pPassword base < dump" */
string mySqlCommand = "mysql -u{0} -p{1} {2} < \"{3}\"";
cmd.StandardInput.WriteLine(mySqlCommand, bddUser, bddPassword, databaseName, dumpPath);
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
StreamReader reader = cmd.StandardOutput;
string theOutput = reader.ReadToEnd();
if (theOutput.Length > 0)
Program.Log("SQL", Environment.NewLine + theOutput);
Program.Log("INFO", "END importing Materials >>>".Fill(code));
, but anyway, it displays additional information from the cmd.exe first execution (before mysql command) and also the command line after the mysql command result...
Execute the script as
mysql -vvv -uUser ...
this Option will Display the queries during execution of the script.
As for your other Problem: Why do you call a cmd.exe? Just call mysql.exe directly from your program and skip the shell...

Process not redirecting complete output; not showing full response from SSH Server

I'm starting up a process and making use of plink to create a reverse tunnel to an ssh that's in my local network.
I can connect fine to the server, but its not displaying the full content on the console window, my goal is to wait for everything to display and then proceed by inputting the password using process.standardInput.
I should receive this on the console window:
Using username "dayan".
Passphrase for key "rsa-key-20130516":
But i receive just the first line:
Using username "dayan".
If i press enter, it does provide me with the "Incorrect password error" but i never see "Passphrase for key rsa-key...."
Also please note that I did put in the correct password and the console remained blank, but on the Linux shell that holds the SSH server, I ran a who command and noticed I was connected successfully.
What could be the issue here?
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.FileName = Path.Combine(BinPath, "plink.exe");
processInfo.Arguments =
String.Format(#" {0} {1}:127.0.0.1:{2} -i {3} -l {4} {5}",
remoteOption, LocalPort, TargetPort, KeyPath, username, TargetIp);
processInfo.UseShellExecute = false;
processInfo.CreateNoWindow = false;
processInfo.RedirectStandardOutput = true;
processInfo.RedirectStandardInput = true;
processInfo.RedirectStandardError = true;
Process process = Process.Start(processInfo);
StreamReader output = process.StandardOutput;
while (!output.EndOfStream) {
string s = output.ReadLine();
if (s != "")
Console.WriteLine(s);
}
process.WaitForExit();
process.Close();
The user name will be already submitted here:
processInfo.Arguments =
String.Format(#" {0} {1}:127.0.0.1:{2} -i {3} -l {4} {5}",
remoteOption, LocalPort, TargetPort, KeyPath, username, TargetIp);
So when you start the process, plink will still handle the user name as input and return a line to process.StandardOutput.
Now it waits for the password but don't ends the line, so string s = output.ReadLine(); dismatchs the real output the program submits.
Try instead reading each byte of the output:
var buffer = new char[1];
while (output.Read(buffer, 0, 1) > 0)
{
Console.Write(new string(buffer));
};
This will catch also the CR+LFs, so you don't have to mention, if the output have to add a new line.
If you want to handle CR+LFs manually (espec. parsing a specific line) you can add the buffer to a string, and only send it, if you find a "\r" or a ":" or so like:
var buffer = new char[1];
string line = "";
while (process.StandardError.Read(buffer, 0, 1) > 0)
{
line += new string(buffer);
if (line.Contains("\r\n") || (line.Contains("Passphrase for key") && line.Contains(":")))
{
Console.WriteLine(line.Replace("\r\n",""));
line = "";
}
};

Responding to cmd prompt programmatically in C#

I'm stuck trying to send commands to a cmd prompt that is waiting for my input.
The command in question is JavaLoader, which if I try to use on a BlackBerry device with a password it prompts me for a password. I would like to enter a password but using C# code.
Assuming that I've successfully created the cmd.exe using a Process like so:
process = new Process();
// Invokes the cmd process specifying the command to be executed.
string cmdProcess = string.Format(System.Globalization.CultureInfo.InvariantCulture, #"{0}\cmd.exe", new object[] { Environment.SystemDirectory });
// Pass executing file to cmd (Windows command interpreter) as a arguments
//Removed /C tells cmd that we want it to execute the command that follows, and then exit.
string cmdArguments = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}", new object[] { command });
// Pass any command line parameters for execution
if (arguments != null && arguments.Length > 0)
{
cmdArguments += string.Format(System.Globalization.CultureInfo.InvariantCulture, " {0}", new object[] { arguments, System.Globalization.CultureInfo.InvariantCulture });
}
process.StartInfo.FileName = cmdProcess;
process.StartInfo.Arguments = cmdArguments;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false; // Must be false for redirection
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
bool s = process.Start();
How do I go about responding the cmd's prompt for a passwords?
I've tried getting the Input stream and using .WriteLine("randompassword") like so:
while (process.Responding)
{
sw.WriteLine(response);
}
Another thing I've noticed is the prompt for a password is not being picked up as Output or Error and the application will in fact hang here because it's waiting for my input.

Execute command line using C#

All I am trying to do is send a command that opens a model with the program.exe
Supposed to be super simple!
Ex:
"C:\Program Files (x86)\River Logic\Enterprise Optimizer 7.4 Developer\EO74.exe" "C:\PauloXLS\Constraint Sets_1.cor"
The line above works well if pasted on the command prompt window.
However, when trying to pass the same exact string on my code it gets stuck on C:\Program
string EXE = "\"" + #tbx_base_exe.Text.Trim() + "\"";
string Model = "\"" + #mdl_path.Trim()+ "\"";
string ExeModel = EXE + " " + Model;
MessageBox.Show(ExeModel);
ExecuteCommand(ExeModel);
ExeModel is showing te following line on Visual Studio:
"\"C:\\Program Files (x86)\\River Logic\\Enterprise Optimizer 7.4 Developer\\EO74.exe\" \"C:\\PauloXLS\\Constraint Sets_1.cor\""
To me looks like it is the string I need to send in to the following method:
public int ExecuteCommand(string Command)
{
int ExitCode;
ProcessStartInfo ProcessInfo;
Process Process;
ProcessInfo = new ProcessStartInfo("cmd.exe", "/K " + Command);
ProcessInfo.CreateNoWindow = true;
ProcessInfo.UseShellExecute = true;
Process = Process.Start(ProcessInfo);
Process.WaitForExit();
ExitCode = Process.ExitCode;
Process.Close();
return ExitCode;
}
Things I've tried:
Pass only one command at a time (works as expected), but not an option since the model file will open with another version of the software.
Tried to Trim
Tried with # with \"
Can anyone see any obvious mistake? Thanks.
It's pretty straightforward. You just create a command line object then write to it, then to execute it you read back from it using SR.ReadToEnd():
private string GETCMD()
{
string tempGETCMD = null;
Process CMDprocess = new Process();
System.Diagnostics.ProcessStartInfo StartInfo = new System.Diagnostics.ProcessStartInfo();
StartInfo.FileName = "cmd"; //starts cmd window
StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
StartInfo.CreateNoWindow = true;
StartInfo.RedirectStandardInput = true;
StartInfo.RedirectStandardOutput = true;
StartInfo.UseShellExecute = false; //required to redirect
CMDprocess.StartInfo = StartInfo;
CMDprocess.Start();
System.IO.StreamReader SR = CMDprocess.StandardOutput;
System.IO.StreamWriter SW = CMDprocess.StandardInput;
SW.WriteLine("#echo on");
SW.WriteLine("cd\\"); //the command you wish to run.....
SW.WriteLine("cd C:\\Program Files");
//insert your other commands here
SW.WriteLine("exit"); //exits command prompt window
tempGETCMD = SR.ReadToEnd(); //returns results of the command window
SW.Close();
SR.Close();
return tempGETCMD;
}
Why are you opening a command prompt (cmd.exe)? Just pass the name of the executable.

Categories

Resources