stem_examples/src/stem_examples/list_circuits.py

46 lines
1.3 KiB
Python
Raw Permalink Normal View History

2024-01-17 16:30:48 +00:00
#!/usr/local/bin/python3.sh
2024-01-14 11:55:48 +00:00
# -*-mode: python; py-indent-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
# http://vt5hknv6sblkgf22.onion/tutorials/examples/list_circuits.html
import sys
import os
import getpass
from stem import CircStatus
from tor_controller import get_controller
def iMain():
if os.path.exists('/run/tor/control'):
controller = get_controller(unix='/run/tor/control')
else:
controller = get_controller(port=9051)
2024-01-14 14:28:53 +00:00
2024-02-21 08:03:10 +00:00
password = os.environ.get('TOR_CONTROLLER_PASSWORD', '')
2024-01-14 11:55:48 +00:00
try:
controller.authenticate(password)
2024-01-14 14:28:53 +00:00
2024-01-14 11:55:48 +00:00
for circ in sorted(controller.get_circuits()):
if circ.status != CircStatus.BUILT:
continue
2024-01-14 14:28:53 +00:00
2024-01-14 11:55:48 +00:00
print("")
print("Circuit %s (%s)" % (circ.id, circ.purpose))
2024-01-14 14:28:53 +00:00
2024-01-14 11:55:48 +00:00
for i, entry in enumerate(circ.path):
div = '+' if (i == len(circ.path) - 1) else '|'
fingerprint, nickname = entry
2024-01-14 14:28:53 +00:00
2024-01-14 11:55:48 +00:00
desc = controller.get_network_status(fingerprint, None)
address = desc.address if desc else 'unknown'
2024-01-14 14:28:53 +00:00
2024-01-14 11:55:48 +00:00
print(" %s- %s (%s, %s)" % (div, fingerprint, nickname, address))
2024-01-14 14:28:53 +00:00
2024-01-14 11:55:48 +00:00
except Exception as e:
print(e)
finally:
2024-01-14 14:28:53 +00:00
del controller
2024-01-14 11:55:48 +00:00
return 0
if __name__ == '__main__':
sys.exit( iMain() )