advanced web statistics

Run a Command Shell Executable from ASP.NET

8/2/2006 11:34:14 AM

PLEASE STOP HARASSING ME ABOUT THIS CODE! IT IS MERELY A REPOST OF SOMEONE ELSE'S CODE AND IS THE BASIS OF MY ENTIRELY DIFFERENT SOLUTION.

What a pain. I`ve spent about 3 days trying to figure out how to run command shell executables from an ASP.NET solution and it drove me completely insane.  I knew that System.Diagnostics.Process was going to be used. Check.  Since I have never started a process from a .NET application I browsed for some sample code from MSDN.

After many unsuccessful attempts of opening Notepad.exe I tried the next logical thing: opening a command shell (cmd.exe). Success! I then stumbled upon Brendan Tompkins` blog and a tutorial entitled "Run a .BAT File from ASP.NET" along with some sample code.  If you are able to successfully open cmd.exe from your application then you should simply create a text file (.txt, .bat, etc..)with command line(s), open cmd.exe and use a streamwriter to write out the line.  It worked like a charm!

I also think that it is worth mentioning that you should concatenate an Environment.NewLine (or VB equivalent) where you write out the line from the text file. This will act as a carriage return.  When I ran the code without using the newline it didn`t appear to work, and did as soon as I added the return.  But this may just be me.

Code Snippet
string filepath = Server.MapPath("import/ga_import.bat");

// Create the ProcessInfo object
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;

// Start the process
Process proc = Process.Start(psi);
StreamReader sr = File.OpenText(filepath);
StreamWriter sw = proc.StandardInput;

while(sr.Peek() != -1)
{
   // Make sure to add Environment.NewLine for carriage return!
   sw.WriteLine(sr.ReadLine() + Environment.NewLine);
}

sr.Close();
proc.Close();
sw.Close();

Quasi-Important Note
Usually (at least in my experience) console applications should be pretty straight-forward with minimal bells and whistles.  The executable I am dealing with has 2 alert boxes when it is ran. The first tells you that it is going to run and prompts you (OK / Cancel).  The second lets you know that it is closing down.  While running the code from above I would hear that annoying error beep. There is a very simple solution for this.

Original
\\server\exec\file.exe /Argument1=1 /Argument2=2

Edited
\\server\exec\file.exe /GO /Argument1=1 /Argument2=2

You can also execute a command without loading a .bat or .txt file.  In my case I stored the arguments and server name in my web.config file, created a command string at run time, and then used the StreamWriter to write out a line. Easy.

RUNNING A .BAT IN AN INTERNET SETTING IS DANGEROUS AND SHOULD BE DONE WITH DISCRETION. MY SOLUTION (EVEN THOUGH IT DID NOT USE A .BAT FILE) WAS FOR AN INTRANET APPLICATION.

Related Links:
Brendan Tompkins - Run a .BAT File from ASP.NET
Unable to Start a Process from ASP.NET

.NET, C#, Code, Interesting, Programming

kick it on DotNetKicks.com

Comments


I`d be concerned about the security implications of what you are doing. The command utility will run under the security of the rather poor ASPNET user, which may not have the rights to do the things you wish to do in your command line utility... like writing output to a file. The temptation would be to increase the privs of the web app, which opens the door for hackers in other ways. Now a hacker can get into your bat file list, change one, and call your web app which will merrily invoke his malware on your server.

In general, running a bat file from a web app is a bad idea, whether it is ASP.Net or any other language.

Posted by: Nick Malik | 10/6/2006 5:32:14 AM

Yes, we all know it`s dangerous, but it`s a nice post. And very handy for what I am about to do. Lord help me.

Posted by: Ian | 6/28/2007 6:32:46 AM

This was for an Intranet application.

Posted by: Will Asrari | 9/18/2007 2:11:59 PM

Fuck all

Arabian Hackers

Posted by: Ahmad Najaf Reza | 11/19/2007 6:06:58 AM

hi,

It throws access denied error in secured website ,

what type of access spec we have to assign ?

Any idea !

Posted by: Prabu | 4/5/2008 3:10:56 AM

Hi,

I tried to implement this in one of my applications with some modifications. I inserted one more line to hide the command window -

proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

But here I want to display the error messages(if any) thrown by windows"system32"cmd.exe. The common try...catch block is not catching it. Any suggestion?

Posted by: Thomas | 7/3/2008 10:39:20 AM

i had the problem to run Application (EXE) from ASP.Net C# on server side

i changed local policies for APSNET user:
in WinXP: run secpol.msc

go to Local Policies->User Rights Assignment
find "Deny log on locally" and remove ASPNET user from it.

then find "Deny logon locally" and remove ASPNET user from it.


more details:
http://mxdev.blogspot.com/2008/09/asp-net-run-application-exe-from-aspnet.html

Posted by: mxdev | 9/2/2008 4:47:53 PM

Ow man, what a fuck !

Posted by: nasser | 10/13/2009 10:06:56 AM

Leave a Comment

   

  Enter the text to proceed!