-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmdpy.py
55 lines (41 loc) · 1.27 KB
/
cmdpy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#! /usr/bin/env python3
import subprocess
from flask import Flask, jsonify, render_template, request
ALLOWED_COMMANDS = ("ls", "echo", "cat", "dir", "man",)
app = Flask(__name__)
def validate_command(command):
"""
Doesn't let user input whatever command he wants, it's to avoid
running unwanted commands for deleting things.
:param command: O.S. command tu run
"""
if command not in ALLOWED_COMMANDS:
raise Exception(f"You're not allowed to use: {command}")
@app.route("/")
def root():
return render_template("index.html")
@app.route("/api/commands")
def available_commands():
return jsonify(
commands=ALLOWED_COMMANDS
)
@app.route("/api/cmd", methods=["POST"])
def custom_command():
body = request.get_json()
raw_command = body["cmd"]
command = raw_command.split()
# example: "ls -oh" will be ["ls", "-oh"]
try:
validate_command(command[0])
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return jsonify(
command=raw_command,
result=result.stdout.decode(),
)
except Exception as ex:
return jsonify(
command=raw_command,
result=str(ex),
), 403
if __name__ == "__main__":
app.run()