import subprocess import requests from lxml import html from flask import Flask, request, jsonify import json import os app = Flask(__name__) def start_service(): command = "systemctl start {{ service_control_name }}" try: # Run the command using subprocess.run() process = subprocess.Popen(command, shell=True) except subprocess.CalledProcessError as e: return {"Error": e.strip('\n')} command = "systemctl status {{ service_control_name }} | grep Active | cut -d ':' -f 2- | cut -b 2-" try: # Run the command using subprocess.run() result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True) return {"Message": result.stdout.strip('\n')} except subprocess.CalledProcessError as e: return {"Error": e.strip('\n')} def stop_service(): command = "systemctl stop {{ service_control_name }}" try: # Run the command using subprocess.run() process = subprocess.Popen(command, shell=True) except subprocess.CalledProcessError as e: return {"Error": e.strip('\n')} command = "systemctl status {{ service_control_name }} | grep Active | cut -d ':' -f 2- | cut -b 2-" try: # Run the command using subprocess.run() result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True) return {"Message": result.stdout.strip('\n')} except subprocess.CalledProcessError as e: return {"Error": e.strip('\n')} def service_status(): command = "systemctl status {{ service_control_name }} | grep Active | cut -d ':' -f 2- | cut -b 2-" try: # Run the command using subprocess.run() result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True) command = "systemctl status {{ service_control_name }} | grep Active | cut -d ':' -f 2 | cut -d ' ' -f 2" status = subprocess.run(command, shell=True, check=True, capture_output=True, text=True) return {"Message": result.stdout.strip('\n'), "Status": status.stdout.strip('\n')} except subprocess.CalledProcessError as e: return {"Error": e.strip('\n')} @app.route('/start', methods=['GET']) def start(): try: return jsonify(start_service()) except ValueError as e: print(e) return jsonify({'error': e}), 400 @app.route('/stop', methods=['GET']) def stop(): try: return jsonify(stop_service()) except ValueError as e: print(e) return jsonify({'error': e}), 400 @app.route('/status', methods=['GET']) def status(): try: return jsonify(service_status()) except ValueError as e: print(e) return jsonify({'error': e}), 400 @app.route('/test', methods=['GET']) def test(): return jsonify({'message': 'Hello there'}) if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=5000)