Monthly Archives: January 2011

CMD on the web

Yesterday, while sitting at my desk doing my usual work <digression>actually computer was doing that. I was only telling him what to do </digression>.

So I was pretty much free to do other things and at that time I miss my home computer where I want to do things, like I want to see the status of my computer remotely, is it up or not and system uptime and the Intranet Apache server is running or not which is critical for some stuff.

So I came back to home, it was Friday night and wrote a python script to fetch commands from a web URL and run them in console and POST the results back to server. I set it up in task scheduler to run at specified intervals.

Unfortunately, the results were not at all up to what I wanted. The whole activity from sending commands from web page to actually execute them and display the results took quite a long time. The average round-trip time including execution was ~ 3 minutes. That’s really too slow.

There were bottlenecks in the solutions.

  1. The task scheduler won’t allow you to specify less than 1 min interval for your scripts, limiting the execution time > 1 min
  2. Even if I do something to set the scheduler to something like 5 sec intervals, that would be inefficient and overkill.


I realized there was a server that was performing some tasks like performing builds etc. I tried setting up a project on it to run the script in 5 sec intervals still the results were not up to the mark, but still there was significant reduction in round-trip = 1 minutes – 30 secs.

Later, I setup the Cruisecontrol to regularly ping the server to execute the task only if there is a command waiting to be executed at the server. This brought down the time to 10-40 seconds.

I loved it.


Following is the Project entry in the Cruisecontrol:

	<project name="RCProcessor" >
    		<urlTrigger url="<URL>/command.txt" />
		<sourcecontrol type="nullSourceControl" />
	  		    <buildArgs>"/c <Path-To-Script> >> commands.txt"</buildArgs>
		   	<xmllogger />


And here comes the python script…

import os
from re import split
import string
import urllib
import urllib2
import datetime
baseurl = ' < Base-URL > /'
url = baseurl + 'command.txt'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
now ="%m-%d-%y %H:%M")
headers = { 'User-Agent' : user_agent }

def SubmitData(url,data):
	user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
	values = {'data' : data}
	headers = { 'User-Agent' : user_agent }
	encdata = urllib.urlencode(values)
	req = urllib2.Request(url, encdata)
	response = urllib2.urlopen(req)
	the_page =
	return the_page
#END SubmitData

print("Get URL: "+url)
req = urllib2.Request(url)
response = urllib2.urlopen(req)
the_page =
the_page = string.replace(the_page,"\\","\")
print("Command: "+ the_page)

if (the_page != ""):
	list = ""+"cmd /c " + the_page+""
	for line in os.popen("cmd.exe /c" + the_page):
		line = string.replace(line,"<","<")
		line = string.replace(line,">",">")
		list += "rn
" + line print(":: OUTPUT ::nn") print(list) print("nn") print("SEND URL: "+baseurl + "output.php") print(SubmitData(baseurl + "output.php","rn
"+ list)) #END IF


That’s all folks J

Hope you like it, play around and try out new things and post it back here.

And do leave comments J