Skip to content

Instantly share code, notes, and snippets.

@infinite-Joy
Created February 5, 2017 18:09
Show Gist options
  • Save infinite-Joy/2ae285ba18f5070181b0dc69ab66454a to your computer and use it in GitHub Desktop.
Save infinite-Joy/2ae285ba18f5070181b0dc69ab66454a to your computer and use it in GitHub Desktop.
flask app to list out the bash scripts and get the output after executing the bash scripts.
import os
import json
import subprocess
from flask import Flask
from flask import render_template
app = Flask(__name__)
#######################
"""
template/home.html
<html>
<body>
<ul>
{% for i in data: %}
<li><a href="{{ url_for('script_file', filename=i)}}">
Files: {{ i }}
</a></li>
{% endfor %}
</ul>
</body>
</html>
"""
@app.route('/')
def get_data():
def file_in_dir():
for entry in make_tree(get_root_dir()):
yield entry
return render_template('home.html', data=file_in_dir())
@app.route('/<filename>')
def script_file(filename):
if ".sh" in filename:
shell_script = {'file_path': get_root_dir(), 'filename': filename}
subprocess.call("bash {file_path}/{filename} > out.txt".format(**shell_script), shell=True)
with open("out.txt") as f:
contents = f.read()
return contents
else:
return "Not an executable bash script"
def make_tree(path):
lst = "No files"
try: lst = os.listdir(path)
except OSError:
pass #ignore errors
return lst
def get_root_dir():
return os.path.dirname(os.path.realpath(__file__))
if __name__ == "__main__":
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment