How do I get the Tor exit node IP address over the control port?

pintpint picture pintpint · Mar 19, 2012 · Viewed 9.2k times · Source

I want to monitor the status of running Tor instances.

I am already able to get information via a TCP connection to the control ports. E.g. "GETINFO stream-status" returns data, but I am not able to determine the IP address of the currently chosen exit node.

It would be possible to simply request something like whatismyip.org, but that is too slow and does not scale well.

So what is the best way to get the exit node IP address of a Tor connection?

Answer

Damian picture Damian · Jun 16, 2013

This is a great question! Here's a short script for doing it using stem...

from stem import CircStatus
from stem.control import Controller

with Controller.from_port(port = 9051) as controller:
  controller.authenticate()

  for circ in controller.get_circuits():
    if circ.status != CircStatus.BUILT:
      continue

    exit_fp, exit_nickname = circ.path[-1]

    exit_desc = controller.get_network_status(exit_fp, None)
    exit_address = exit_desc.address if exit_desc else 'unknown'

    print "Exit relay"
    print "  fingerprint: %s" % exit_fp
    print "  nickname: %s" % exit_nickname
    print "  address: %s" % exit_address
    print

Thanks for the question. I've added this to our FAQ.