#!/usr/local/bin/python3.sh -u # -*-mode: python; py-indent-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*- __doc__ = """ A script by adrelanos@riseup.net to check what percentage of boostrapping tor is at. """ ## Copyright (C) 2012 - 2020 ENCRYPTED SUPPORT LP ## See the file COPYING for copying conditions. import sys import os import re from stem.connection import connect from tor_controller import get_controller def iMain(lArgs=None): password = os.environ.get('TOR_CONTROLLER_PASSWORD') if os.path.exists('/run/tor/control'): controller = get_controller(password=password, unix='/run/tor/control') else: controller = get_controller(password=password, port=9051) # controller.connect() bootstrap_status = controller.get_info("status/bootstrap-phase") ## Possible answer, if network cable has been removed: ## 250-status/bootstrap-phase=WARN BOOTSTRAP PROGRESS=80 TAG=conn_or SUMMARY="Connecting to the Tor network" WARNING="No route to host" REASON=NOROUTE COUNT=26 RECOMMENDATION=warn ## Possible answer: ## 250-status/bootstrap-phase=NOTICE BOOTSTRAP PROGRESS=85 TAG=handshake_or SUMMARY="Finishing handshake with first hop" ## Possible answer, when done: ## 250-status/bootstrap-phase=NOTICE BOOTSTRAP PROGRESS=100 TAG=done SUMMARY="Done" ## TODO: parse the messages above. print(format(bootstrap_status)) progress_percent = re.match('.* PROGRESS=([0-9]+).*', bootstrap_status) exit_code = int(progress_percent.group(1)) controller.close() return exit_code if __name__ == '__main__': sys.exit( iMain())