Click here to Skip to main content
15,121,549 members
Articles / Web Development / ASP.NET
Article
Posted 12 May 2007

Stats

100.9K views
2.6K downloads
34 bookmarked

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
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)

Share

About the Author

darkalchemy
Web Developer
India India
Hi To All

Comments and Discussions

 
QuestionNot working when the project is deployed Pin
j_s_salvador5-Jun-12 14:49
Memberj_s_salvador5-Jun-12 14:49 
QuestionRun Interactive Command Shell or Batch Files From ASP.NET Pin
Member 86819251-Mar-12 2:16
MemberMember 86819251-Mar-12 2:16 
GeneralRequest for Info Pin
VC Sekhar Parepalli23-Mar-08 17:38
MemberVC Sekhar Parepalli23-Mar-08 17: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 Ashraf29-Sep-07 0:18
professionalAsif Ashraf29-Sep-07 0:18 
GeneralNice Pin
Will Asrari8-Jun-07 13:23
MemberWill Asrari8-Jun-07 13:23 
QuestionKudo's / Question / Bug??? Pin
sides_dale19-May-07 11:55
Membersides_dale19-May-07 11:55 
Generalsmall bug Pin
margiex13-May-07 4:46
Membermargiex13-May-07 4:46 
GeneralRe: small bug Pin
darkalchemy13-May-07 22:17
Memberdarkalchemy13-May-07 22: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.