Skip to content

Instantly share code, notes, and snippets.

@inaz2
Last active June 26, 2024 14:19
Show Gist options
  • Save inaz2/78651ec593af1e0521be to your computer and use it in GitHub Desktop.
Save inaz2/78651ec593af1e0521be to your computer and use it in GitHub Desktop.
Python implementation of traceroute
# references:
# Learning by doing: Writing your own traceroute in 8 easy steps (Ksplice Blog)
# https://blogs.oracle.com/ksplice/entry/learning_by_doing_writing_your
import sys
import socket
def traceroute(dest_addr, max_hops=30, timeout=0.2):
proto_icmp = socket.getprotobyname('icmp')
proto_udp = socket.getprotobyname('udp')
port = 33434
for ttl in xrange(1, max_hops+1):
rx = socket.socket(socket.AF_INET, socket.SOCK_RAW, proto_icmp)
rx.settimeout(timeout)
rx.bind(('', port))
tx = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, proto_udp)
tx.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl)
tx.sendto('', (dest_addr, port))
try:
data, curr_addr = rx.recvfrom(512)
curr_addr = curr_addr[0]
except socket.error:
curr_addr = None
finally:
rx.close()
tx.close()
yield curr_addr
if curr_addr == dest_addr:
break
if __name__ == "__main__":
dest_name = sys.argv[1]
dest_addr = socket.gethostbyname(dest_name)
print "traceroute to %s (%s)" % (dest_name, dest_addr)
for i, v in enumerate(traceroute(dest_addr)):
print "%d\t%s" % (i+1, v)
@rednafi
Copy link

rednafi commented Jun 2, 2023

Thanks for this one. I updated it here for Python 3 (ran it on 3.11) and made some changes so that the script doesn't create new socket connections in every iteration:

https://rednafi.com/python/implement_traceroute_in_python/#writing-a-crappier-version-of-traceroute-in-python

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment