So I have a functional python script that performs some sysadmin automation tasks. You give it a hostname and it goes off and does it's thing, SSHing to servers and printing messages to the console as it goes. What is a good technical way to incorporate this with browser support rather than running in a terminal? Really, all that would be required would be an input box on a webpage (for the servers hostname) and that could go and run the script directly and then print the stdout to the browser.
-
Your question as it stands is very broad and probably cannot be answered very accurately. Could you please recheck the question and try to narrow it down into one or several more specific questions? Leaving it this way probably gives you very diverse answers.jsalonen– jsalonen2014-02-27 11:56:51 +00:00Commented Feb 27, 2014 at 11:56
-
1I've made an edit. tl;dr is I am looking to provide a web browser interface to run my python program (typically executed in a terminal).Peter– Peter2014-02-27 12:01:45 +00:00Commented Feb 27, 2014 at 12:01
-
Okay I get it now! So are you saying you just want to have a nice web browser interface that you can use to execute Python scripts on your application servers?jsalonen– jsalonen2014-02-27 12:02:29 +00:00Commented Feb 27, 2014 at 12:02
-
Just something out of the box: have you taken a look into commando.io? Not sure if that works for you, but definitely satisfies the mentioned requirements.jsalonen– jsalonen2014-02-27 12:04:53 +00:00Commented Feb 27, 2014 at 12:04
2 Answers
Actually you won't need too much. You'll need to deploy a web server that executes the script and the script should give the output as an HTTP response rather than writing to STDOUT.
That being said, you can use Python's built in SimpleHTTPServer for starters. It is a very basic web server (which can be improved) already written for you in Python's standard library. I'll rather use this for sysadmin and intranet tasks than Apache because it is very easy to set it up and start serving.
You'll probably need to extend it so when the request to run the script comes, it know what to do with it. SimpleHTTPServer maybe is not a good fit here but you can extend BaseHTTPServer or CGIHTTPServer to accomplish the script execution.
On the script side you'll need to modify the output's target, nothing more clever than that. It'll probably require some refactoring but not much.
Keep in mind that BaseHTTPServer is not focused on security, so use this in safe environments or the data of your company might be compromised.
I can get into any more details because the question is rather big but that is how I would start doing it.
Hope it helps!
3 Comments
BaseHTTPServer or using CGIHTTPServer is much more likely to be useful; SimpleHTTPServer only serves files from the current directory, and isn't at all designed to be extended to run arbitrary Python code.SimpleHTTPServer is okay, but as its name implies, its quite simple. Try a microframework, like Flask which includes a webserver and helpers to make things easier for you. The script couldn't be more simple:
from tools import thingamajig
from flask import Flask, request
app = Flask(__name__)
@app.route('/go-go-gadget', methods=['POST','GET'])
def index():
if request.method == 'POST':
ip_address = request.form['ip_address']
results = thingamajig.do(ip_address)
return render('index.html', results=results)
return render('index.html')
if __name__ == '__main__':
app.run()
Now just create a templates/ directory and in it, add a file index.html with the following:
<form method="POST">
<input type="text" name="ip_address" />
<input type="submit">
</form>
{% if results %}
Here are the results: {{ results }}
{% endif %}
Now just python server.py and go to http://localhost:5000/go-go-gadget
1 Comment
Flask seems a very easy-to-set-up library/server for this.