Click here to Skip to main content
Click here to Skip to main content

Run Interactive Command Shell or Batch Files From ASP.NET

, 12 May 2007
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

darkalchemy
Web Developer
India India
Hi To All

Comments and Discussions

 
QuestionNot working when the project is deployed Pinmemberj_s_salvador5-Jun-12 13:49 
QuestionRun Interactive Command Shell or Batch Files From ASP.NET PinmemberMember 86819251-Mar-12 1:16 
GeneralRequest for Info PinmemberVijay Chandra Sekhar Parepalli23-Mar-08 16:38 
GeneralExtra Ordinary Pinmemberhackrogenius28-Sep-07 23:18 
GeneralNice PinmemberWill Asrari8-Jun-07 12:23 
QuestionKudo's / Question / Bug??? Pinmembersides_dale19-May-07 10:55 
Generalsmall bug Pinmembermargiex13-May-07 3:46 
GeneralRe: small bug Pinmemberdarkalchemy13-May-07 21:17 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 13 May 2007
Article Copyright 2007 by darkalchemy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid