What is Flask?

Flask is a lightweight and flexible web framework for Python. It provides a simple and minimalistic approach to building web applications.

Flask is known for its simplicity and ease of use, making it a popular choice for developers who want to quickly build small to medium-sized web applications. It is often used for prototyping and developing APIs.

About this tutorial!

To create a Flask app that can execute system commands and bash scripts, you can use the subprocess module in Python. Below is a simple example of a Flask app that provides a web interface to run commands and scripts.

First, make sure to have Flask installed. You can install it using:

pip install Flask

Now, create a file named app.py and add the following code:

from flask import Flask, render_template, request
import subprocess

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/execute', methods=['POST'])
def execute():
    if request.method == 'POST':
        command = request.form.get('command', '')

        # Run the command using subprocess
        try:
            result = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, text=True)
            return render_template('index.html', result=result, command=command)
        except subprocess.CalledProcessError as e:
            return render_template('index.html', result=e.output, command=command, error=True)

if __name__ == '__main__':
    app.run(debug=True)

Now, create a folder named templates in the same directory as app.py. Inside the templates folder, create an HTML file named index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Flask Command Executor</title>
</head>
<body>
    <h1>Flask Command Executor</h1>
    <form method="post" action="{{ url_for('execute') }}">
        <label for="command">Enter Command or Script:</label>
        <input type="text" id="command" name="command" required>
        <button type="submit">Execute</button>
    </form>
    {% if command %}
        <h2>Command/Script Executed:</h2>
        <pre>{{ command }}</pre>
        {% if error %}
            <h2>Error Output:</h2>
            <pre style="color: red;">{{ result }}</pre>
        {% else %}
            <h2>Output:</h2>
            <pre>{{ result }}</pre>
        {% endif %}
    {% endif %}
</body>
</html>

This HTML file includes a form with a text input for entering commands or scripts. The Flask app uses the subprocess module to execute the entered command and displays the result on the webpage.

To run the Flask app, execute the following command in the terminal:

python app.py

Visit http://127.0.0.1:5000/ in your web browser to access the app. Enter system commands or bash scripts in the form, submit the form, and see the output displayed on the webpage.

Caution

Note that running arbitrary commands from user input can pose security risks, so use caution and sanitize inputs appropriately in a production environment.