Click here to Skip to main content
Licence CPOL
First Posted 12 May 2007
Views 42,349
Downloads 774
Bookmarked 33 times

Run Interactive Command Shell or Batch Files From ASP.NET

By | 12 May 2007 | Article
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)

About the Author

darkalchemy

Web Developer

India India

Member

Hi To All

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionRun Interactive Command Shell or Batch Files From ASP.NET PinmemberMember 86819251:16 1 Mar '12  
GeneralRequest for Info PinmemberVijay Chandra Sekhar Parepalli16:38 23 Mar '08  
GeneralExtra Ordinary Pinmemberhackrogenius23:18 28 Sep '07  
GeneralNice PinmemberWill Asrari12:23 8 Jun '07  
QuestionKudo's / Question / Bug??? Pinmembersides_dale10:55 19 May '07  
Generalsmall bug Pinmembermargiex3:46 13 May '07  
GeneralRe: small bug Pinmemberdarkalchemy21:17 13 May '07  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 13 May 2007
Article Copyright 2007 by darkalchemy
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid