External process execution argument issue in C# - c#

I am trying to send commands to RDP connections from a C# console application using PsExec, this is the command
PsExec.exe //1.2.3.4 -u administrator -p secredpassword -c RemoteAppExe.exe
wich works perfectly, runs RemoteAppExe.exe on that system, the problem is that within C# it dosen't work, here is my code :
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
pProcess.StartInfo.FileName = #"PsExec.exe";
pProcess.StartInfo.Arguments = "//1.2.3.4 -u administrator -p secredpassword -c RemoteAppExe.exe"; //argument
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
pProcess.StartInfo.CreateNoWindow = true; //not diplay a windows
pProcess.Start();
string output = pProcess.StandardOutput.ReadToEnd(); //The output result
pProcess.WaitForExit();
What is the problem? I assume it might be the fact that arguments are not properly escaped.
Any ideeas?
Thanks.

It should be:
pProcess.StartInfo.Arguments = #"\\1.2.3.4 -u administrator -p secredpassword -c RemoteAppExe.exe";
Changing the argument line made the code work for me.

Related

Aspera Error :- ascp.exe: Failed to open TCP connection for SSH, exiting

I am working on Aspera file upload functionality and I am getting one error as per below scenario.
I downloaded Aspera CLI from below URL.
https://downloads.asperasoft.com/en/downloads/62
and then with the help of command prompt ran below statement and it worked fine.
cmd Statement : ascp -P33001 -QT -l500m --file-manifest=text -k 0 -o
Overwrite=always {Source File Path} {username}#{domain}:{destination
Folder}
It worked perfectly fine.
Then I tried the same thing from console app with below code.
Process process = new Process();
process.StartInfo.FileName = #"E:\Projects\Research\AsperaFileUpload\AsperaFileUpload\AsperaLibrary\bin\ascp.exe";
process.StartInfo.Arguments = #"ascp -P33001 -QT -l500m --file-manifest=text -k 0 -o Overwrite=always {Source File Path} {username}#{domain}:{destination Folder}"; // Note the /c command (*)
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
//* Read the output (or the error)
string output = process.StandardOutput.ReadToEnd();
Console.WriteLine(output);
string err = process.StandardError.ReadToEnd();
Console.WriteLine(err);
Console.ReadLine();
process.WaitForExit();
But this code always give error like "ascp.exe: Failed to open TCP connection for SSH, exiting"
I am wondering if same command working from command line why it is not working from console app.
Can anyone please help me on this.
Thanks in advance.
This error message tells that "ascp" cannot connect to TCP port 33001 (arg "-P") of the server "{domain}"
I suggest the following:
use the more modern syntax:
ascp -P33001 -QT -l500m --file-manifest=text -k 0 -o Overwrite=always --host={domain} --user={username} --mode=send {Source File Path} {destination Folder}
add argument -L /path/to/log/folder and look at log error in file: aspera-scp-transfer.log
instead of using bare "ascp", which you will find limited, prefer to use the "TransferSDK" here:
https://developer.ibm.com/aspera

c# and ffpmeg command line trouble

I have a php code that calls ffmpeg from the windows command line (because I run apache locally) with:
//ffmpeg -i input.mp4 -r 20 output.gif
echo exec('C:\ffmpeg\bin\ffmpeg.exe -i videos/file.avi -r 10 -s 450x230 videos/file.gif');
And it works fine! Now I wrote a C# program and I try to do the same thing with:
System.Diagnostics.Process.Start
but it fails.
Question: How do I execute the same command from the PHP code but from inside a C# code?
Process process = Process.Start(
new ProcessStartInfo
{
FileName = #"C:\ffmpeg\bin\ffmpeg.exe",
Arguments = "-i videos/file.avi -r 10 -s 450x230 videos/file.gif",
WindowStyle = ProcessWindowStyle.Hidden
}
);
process.WaitForExit();
If it fails an exception will be raised, e.g.
System.ComponentModel.Win32Exception with HResult=-2147467259 if the executable wasn't found.

Running a Linux Console Command in C#

I am using the following code to run a Linux console command via Mono in a C# application:
ProcessStartInfo procStartInfo = new ProcessStartInfo("/bin/bash", "-c ls");
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
String result = proc.StandardOutput.ReadToEnd();
This works as expected. But, if i give the command as "-c ls -l" or "-c ls /path" I still get the output with the -l and path ignored.
What syntax should I use in using multiple switches for a command?
You forgot to quote the command.
Did you try the following on the bash prompt ?
bash -c ls -l
I strongly suggest to read the man bash.
And also the getopt manual as it's what bash use to parse its parameters.
It has exactly the same behavior as bash -c ls
Why? Because you have to tell bash that ls -l is the full argument of -c, otherwise -l is treated like an argument of bash.
Either bash -c 'ls -l' or bash -c "ls -l" will do what you expect.
You have to add quotes like this:
ProcessStartInfo procStartInfo = new ProcessStartInfo("/bin/bash", "-c 'ls -l'");

SWFTools' pdf2swf: No text converted if started from IIS-hosted website

I'm running IIS 7.5 on a Windows Server 2008 R2, and I'm trying to convert some PDFs after the upload into SWFs with SWFTools' pdf2swf. If I start the converter manually with the same arguments, everything is fine. But if I start the converter from within my HttpHandler, either the process does not return any output (and does not seem to be started at all) or converts the PDF without any text - depending on how I start the process.
Here is how I start the process:
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = ToolsPath;
p.StartInfo.Arguments = arguments
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.Password = secPw;
p.StartInfo.UserName = username;
p.StartInfo.Domain = domain;
p.Start();
p.WaitForExit();
and which arguments I pass:
"%%source%% -o %%target%% -v -v -v -f -T 9 -t -s storeallcharacters"
Thank you very much for any help!
Edit: I also tried it without the additional StartInfo (the user-credentials), this was what I first tried, which resulted in a SWF without Text. With the credentials (as admin or standard) I do not get any SWF or output from the converter.
Edit 2: I also tried those arguments:
"%%source%% -o %%target%% -f -T 9 -t -s storeallcharacters"
Okay, I solved the issue by adding a seperate console application with administrative-rights: I added an application manifest with
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
This console application called the pdf2swf.exe and is called by my HttpHandler.
Also I added to the call of the "middleman" in my HttpHandler those lines of code:
p.StartInfo.UseShellExecute = false;
if (System.Environment.OSVersion.Version.Major >= 6)
p.StartInfo.Verb = "runas";
FYI...I had a similar problem. Upgrading to the newest version pdf2swf (build 0857) fixed the problem for me.

problems with stdout and psexec.exe from sysinternals

i have searched and read about issues with psexec.exe from sysinternals not working properly with c# and stdout. i am now trying to figure out how to just call a batch file that has the following instead of using System.Diagnostics.Process to call psexec:
test.bat contains the following line:
psexec.exe \\hostname -u user -p password ipconfig /all >c:\test.txt
test.txt will be saved on the host where i am running my c sharp app and executing psexec.
when i execute the following:
System.Diagnostics.Process psexec_run = new System.Diagnostics.Process();
psexec_run.StartInfo.FileName = "cmd.exe";
psexec_run.StartInfo.Arguments = #"/c """ + cur_dir + #"\test\test.bat""";
psexec_run.StartInfo.UseShellExecute = false;
psexec_run.Start();
psexec_run.WaitForExit();
i see the cmd window pop up and it runs something but not sure what and goes away.
if i execute the following:
System.Diagnostics.Process psexec_run = new System.Diagnostics.Process();
psexec_run.StartInfo.FileName = cur_dir + "\\test\\psexec.exe";
psexec_run.StartInfo.Arguments = #"\\hostname -u user -p password ipconfig /all";
psexec_run.StartInfo.UseShellExecute = false;
psexec_run.Start();
psexec_run.WaitForExit();
then i see the command window open and it runs psexec which takes quite a few secs and i quickly see my output i need, but i have no way of capturing the output or writing it to a file.
i guess my issue now is since psexec will not work with stdout how can i capture the output from the psexec command to write it to a file???
see the following link for the issues with psexec, the last reply on this url mentioned a way to write the process output to a file without using stdout, i'm newbie to c# i can't figure out how to write process output without use stdout :(
http://forum.sysinternals.com/psexec-fails-to-capture-stdout-when-launched-in-c_topic19333.html
based on response below i tried the following:
ProcessStartInfo psi = new ProcessStartInfo(cur_dir + "\\test\\psexec.exe", #"\\hostname -u user -p password ipconfig /all");
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
StreamReader myStreamReader = p.StandardOutput;
// Read the standard output of the spawned process.
string sOutput = myStreamReader.ReadToEnd();
i did ReadToEnd so i would make sure it got all the output, it DID NOT!! for some reason it only go the first line of ipconfig output that was it. Also the cmd window it opened up never closed for some reason. even with CreateNoWindow=true the code just hangs. so again something is wrong with psexec and stdout i think?? as i can run this code just fine using ipconfig /all command on the local host and not use psexec...
again i am looking to avoid stdout and somehow find a way to get the output from this command or unless there is something else i'm over looking? also, not to make more work for anyone, but if you d/l psexec.exe from sysinternals and test it with a command on a remote host you will see. i have spent 2 days on this one :( trying to figure out how to use psexec or find some other quick method to execute remote command on a host and get ouptput.
UPDATE:
i gave up on psexec in c# code, i saw many posts about psexec eating the output, having a child window process ,etcc
until my head hurt :) so i am trying to run a batch file and output to a file and it's not making sense...
i have a batch file test.bat with the following
psexec.exe \\\hostname -u name -p password ipconfig /all >c:\test.txt
when i run the following code:
ProcessStartInfo psi = new ProcessStartInfo(cur_dir + #"\test\test.bat");
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
p.WaitForExit();
the cmd windows comes and goes really quickly and the test.txt file is created but is 0 bytes no info in it.
so if i run the batch file in a windows cmd line with the psexec command it works perfectly fine!!???
so then to verify psexec was the issue i changed the batch file to:
ipconfig /all >c:\test.txt
i execute my code above and it works fine creates the output in the test.txt file..???!!!!
why is not working with psexec am i missing something? if it's psexec, does anyone have
any recommendations for how i can execute a command on a remote windows host and get me the
output???
I have an answer to this problem that has worked for me.
Hopefully someone else will find it useful.
I have literally just spent the last two hours tearing my hair out with this. The psexec tool runs completely fine using a normal command prompt but when attempting to redirect the streams it truncates the output and you only get half output back.
In the end how I fixed my issue was a little bit of a hack. I piped the output of the command to a text file and read it back in to return it from the function.
I also has to set UseShellExecute to true. Without this it still wouldn't work. This had the unfortunate side effect of showing the console window. To get around that I set the window style to be hidden and hey presto it works!!!
Heres my code:
string ExecutePSExec(string command)
{
string result = "";
try
{
string location = AppDomain.CurrentDomain.BaseDirectory;
// append output to file at the end of this string:
string cmdWithFileOutput = string.Format("{0} >{1}temp.log 2>&1", command,location );
// The flag /c tells "cmd" to execute what follows and exit
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + cmdWithFileOutput);
procStartInfo.UseShellExecute = true; // have to set shell execute to true to
procStartInfo.CreateNoWindow = true;
procStartInfo.WindowStyle = ProcessWindowStyle.Hidden; // as a window will be created set the window style to be hiddem
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
proc.WaitForExit();
// now read file back.
string filePath = string.Format("{0}temp.log", location);
result = System.IO.File.ReadAllText(filePath);
}
catch (Exception objException)
{
// Log the exception
}
return result;
}
and its usage:
string command = #"psexec.exe -l -u domain\username -p password /accepteula \\192.168.1.3 netstat -a -n";
ExecutePSExec(command);
I had exactly same problem. i was getting "Windows ip config. " as first line when i run with psexec. i tried with paexec it worked well. I used Marius's code.
Note: if you dont use first cmd / c in arguments command runs only on local computer even if you define target as \\remoteserver
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe");
psi.Arguments = #"cmd/c C:\paexec.exe \\\192.168.2.5 -s -u test.local\administrator -p Password1 cmd /c ipconfig";
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
System.IO.StreamReader myStreamReader1 = p.StandardOutput;
p.WaitForExit();
string sOutput = myStreamReader1.ReadToEnd();
Are you sure your sourcecode is correct? that link is quite a bit old.. maybe its fixed!
Heres an example how to redirect the standard-output and put whole output in a string via streamreader:
ProcessStartInfo psi = new ProcessStartInfo("tftp.exe");
// preferences for tftp process
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
StreamReader myStreamReader = p.StandardOutput;
p.WaitForExit();
// Read the standard output of the spawned process.
string sOutput = myStreamReader.ReadToEnd();
i found a solution. apparently psexec is NOT going to work in c sharp. so i came up with some wmi code to connect to a remote host and it's working PERFECTLY!!! :)
i used microsoft's WMICodeCreator.exe to create wmi code for C# for the process method on a remote host, wow that tool is amazing because wmi code is little confusing to me.
psexec's output goes to StandardError and not StandardOutput. I don't know why it is that way. Following code snippet access it.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.RedirectStandardError = true;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
errors = process.StandardError.ReadToEnd();
process.Close();

Categories