Created
March 15, 2016 15:47
-
-
Save h3xxx/518f766ce6378610aa14 to your computer and use it in GitHub Desktop.
CGI script in Python for executing predefined commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 '''<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