from flask import Flask, request from reconnect import reboot from os import environ from subprocess import call app = Flask(__name__) # Get router username and password from environment variables username = environ.get('ROUTER_USERNAME') password = environ.get('ROUTER_PASSWORD') # Get router IP and path from environment variables if they exist url = environ.get('ROUTER_IP') or '192.168.1.1' path = environ.get('ROUTER_PATH') or '/ws' @app.route('/', methods=['POST']) def result(): print(request.json) return 'Received request, printing to console!' @app.route('/safe', methods=['POST']) def safe(): # Try to ping the router 100 times for i in range(50): if call(['ping', '-c', '1', url]) == 0: break else: return 'Error: Unable to ping and reboot router automatically!' # Try to nslookup 5 different websites websites = ['www.google.com', 'www.facebook.com', 'www.twitter.com', 'www.reddit.com', 'www.amazon.com'] success = 0 for website in websites: if call(['nslookup', website]) == 0: success += 1 if success > 3: return 'Error: DNS seems to be working, debug manually!' # Reboot router status = reboot(url, path, username, password).text if '{"status":true}' not in status: return 'Error: Something went wrong while rebooting router!' return 'Success: Rebooted router!' @app.route('/force', methods=['POST']) def force(): status = reboot(url, path, username, password).text if '{"status":true}' not in status: return 'Error: Something went wrong while rebooting router!' return 'Success: Rebooted router!' @app.route('/grafana', methods=['POST']) def grafana(): if request.json['state'] == 'alerting': safe()