Tuesday, November 06, 2007

Web Shell: Prototype & IronPython

{

I've been experimenting with IronPython of late trying to get the gestalt of Python as a language.  It occurred to me that something I had been thinking about for a while as a web project would make sense as an Iron Python project so I dabbled a bit.

The idea I had was a command line type application that approximated commands going to a windows shell and sent the results back to the user.  It's a part of a larger suspicion that I've had: that command lines are crisp, efficient user interfaces that are easy to lose sight of because it's not a natural way of thinking on the web.

The screen shots tell you the basic story: you open your browser to nothing but an underlined textbox after which you type your command and press the enter key. 

The client code is pretty straight forward - I've become disillusioned with YUI and moved back to using Prototype:

function runCommand(cmd){
    var request = new Ajax.Request(
        'eruServer.aspx',
        {
            parameters:'cmd=' + cmd,
            onComplete: handleResult
        }   
    );
}

function handleResult(response){
    $('loaderImage').style.visibility = 'hidden';
    var cmdResult = response.responseText;
    $('stdoutDiv').innerHTML = '<pre>' + cmdResult + '</pre>';
}

So that's all pretty simple, on the server processing the request is equally simple in IronPython.

from System.Diagnostics import *
from System.IO import *

def Page_Load(sender, e):
    cmd = Request.cmd
    p = Process()
    p.StartInfo.FileName = "cmd.exe"
    p.StartInfo.UseShellExecute = 0
    p.StartInfo.CreateNoWindow = 1
    p.StartInfo.RedirectStandardInput = 1
    p.StartInfo.RedirectStandardOutput = 1
    p.Start()
    myOut = p.StandardInput
    myIn = p.StandardOutput   
    myOut.WriteLine(cmd)    
    p.StandardInput.Flush()
    p.StandardInput.Close()
    output = myIn.ReadToEnd()
    p.StandardOutput.Close()    
    p.Dispose()
    Response.Write(Server.HtmlEncode(output))

The web project is fairly small - go ahead and download it here. If you have any improvement, let me know - I'm not sure how to start this as an "open source" affair but it would be a great solution as a light weight tool for remotely operating on a server.

Oh yeah, security: I was thinking of simple NTFS permissions on the files along with Windows Security on the website.  Should do the trick...

}

No comments: