Skip to content

Instantly share code, notes, and snippets.

@h3xxx
Created March 15, 2016 15:47
Show Gist options
  • Save h3xxx/518f766ce6378610aa14 to your computer and use it in GitHub Desktop.
Save h3xxx/518f766ce6378610aa14 to your computer and use it in GitHub Desktop.
CGI script in Python for executing predefined commands
#!/usr/bin/python
import cgi, cgitb, os, sys
import commands
cgitb.enable(); # formats errors in HTML
form = cgi.FieldStorage()
sys.stderr = sys.stdout
print "Content-type: text/html"
print
print '''<html>
<head>
<title>Server Status</title>
</head>
<body>
<form action="server-status.py" method=GET>
cmd <input type="text" name="cmd" value="" size=20>
</form>'''
main_cmds = ('uname -a', 'w')
cmds = ('netstat -lt', 'df -h')
if form.has_key("cmd"):
cmd = form["cmd"].value;
cmds = cmds + (cmd,)
print '''<h3>Server Status</h3>'''
for cmd in main_cmds:
output = commands.getstatusoutput(cmd)[1]
output = output.replace("\n", "<br/>")
print '''<pre>'''
print output
print '''</pre>'''
for cmd in cmds:
output = commands.getstatusoutput(cmd)[1]
output = output.replace("\n", "<br/>")
print '''<h3>'''
print cmd
print '''</h3>'''
print '''<pre>'''
print output
print '''</pre>'''
print '''</body>
</html>'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment