Click here to Skip to main content
15,880,972 members
Articles / Web Development / ASP.NET

Run Interactive Command Shell or Batch Files From ASP.NET

Rate me:
Please Sign up or sign in to vote.
2.56/5 (8 votes)
12 May 2007CPOL 105.7K   2.7K   34   8
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.

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
Hi To All

Comments and Discussions

 
QuestionNot working when the project is deployed Pin
j_s_salvador5-Jun-12 13:49
j_s_salvador5-Jun-12 13:49 
QuestionRun Interactive Command Shell or Batch Files From ASP.NET Pin
Member 86819251-Mar-12 1:16
Member 86819251-Mar-12 1:16 
GeneralRequest for Info Pin
VC Sekhar Parepalli23-Mar-08 16:38
VC Sekhar Parepalli23-Mar-08 16:38 
Hi,
This is the kind of stuff am looking for. But looks like am missing something here. The Dos commands work good. But c:\helloworld.exe (I put it at C:\) doesn't get executed at all. I added aspnet, IUSR and IWAM all the three to admin role in that PC I hosted this CS code at.

Could you please let me know if am missing anything?

Thanks,

VC
GeneralExtra Ordinary Pin
Asif Ashraf28-Sep-07 23:18
professionalAsif Ashraf28-Sep-07 23:18 
GeneralNice Pin
Will Asrari8-Jun-07 12:23
Will Asrari8-Jun-07 12:23 
QuestionKudo's / Question / Bug??? Pin
sides_dale19-May-07 10:55
sides_dale19-May-07 10:55 
Generalsmall bug Pin
margiex13-May-07 3:46
margiex13-May-07 3:46 
GeneralRe: small bug Pin
darkalchemy13-May-07 21:17
darkalchemy13-May-07 21:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.