import subprocess import time import ipaddress def run_subprocess(cmd, check=True, delay=0): cmd_split = cmd.split(" ") output = subprocess.run( cmd_split, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=check ).stdout.decode("utf-8") if delay > 0: time.sleep(delay) return output while True: try: myip = run_subprocess("curl ifconfig.co") results1 = run_subprocess("docker exec -it vpn nordvpn status") results2 = run_subprocess("docker exec -it dante curl ifconfig.co") NoIP = False try: ip = ipaddress.ip_address(myip.strip()) ip = ipaddress.ip_address(results2.strip()) except: print("Socks error") NoIP = False if NoIP or results1.find("Status: Connected") == -1: # Not Connected print("Restarting") run_subprocess("docker-compose restart vpn", delay=30) run_subprocess("docker-compose restart dante", delay=10) run_subprocess("docker-compose restart qbittorrent") else: pass except: print("Restarting") run_subprocess("docker-compose restart vpn", delay=30) run_subprocess("docker-compose restart dante", delay=10) run_subprocess("docker-compose restart qbittorrent") time.sleep(10 * 60)