Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Run Interactive Command Shell or Batch Files From ASP.NET

0.00/5 (No votes)
12 May 2007 1  
You can run an interactive command shell and run commands from a textbox with this code.

Screenshot - runcmd.jpg

Introduction

This is a nice command shell that can we embedded in a webpage to run commands on the server.

Background

I took the original code from Will Asrari's site, ao all thanks go to him:

I just made some changes to his code.

The code does not have any error handling implemented.

Using the Code

You can modify the code to run a batch file or commands directly.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Page.IsPostBack)
        {
            string exec = TextBox1.Text;
            // Get the full file path
            string strFilePath = Server.MapPath("fine.bat");

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

            // Start the process
            System.Diagnostics.Process proc = 
                       System.Diagnostics.Process.Start(psi);

            // Open the batch file for reading
            //System.IO.StreamReader strm = 
            //           System.IO.File.OpenText(strFilePath);
            System.IO.StreamReader strm = proc.StandardError;
            // Attach the output for reading
            System.IO.StreamReader sOut = proc.StandardOutput;

            // Attach the in for writing
            System.IO.StreamWriter sIn = proc.StandardInput;

            // Write each line of the batch file to standard input
            /*while(strm.Peek() != -1)
            {
                sIn.WriteLine(strm.ReadLine());
            }*/
            sIn.WriteLine(exec);
            strm.Close();

            // Exit CMD.EXE
            string stEchoFmt = "# {0} run successfully. Exiting";

            sIn.WriteLine(String.Format(stEchoFmt, strFilePath));
            sIn.WriteLine("EXIT");

            // Close the process
            proc.Close();

            // Read the sOut to a string.
            string results = sOut.ReadToEnd().Trim();

            // Close the io Streams;
            sIn.Close();
            sOut.Close();

            // Write out the results.
            string fmtStdOut = "<font face=courier size=0>{0}</font>";
            this.Response.Write("<br>");
            this.Response.Write("<br>");
            this.Response.Write("<br>");
            this.Response.Write(String.Format(fmtStdOut, 
               results.Replace(System.Environment.NewLine, "<br>")));
        }
    }
}

Points of Interest

There can be a potentially unwanted use of this code as a trojan or as an ASP.NET hack into the server. Use it at your own risk and responsibility.

History

Suggestions are welcome at darknessends@gmail.com. Any bugs or improvements will be highly appreciated.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here