Last active
June 26, 2024 14:19
-
-
Save inaz2/78651ec593af1e0521be to your computer and use it in GitHub Desktop.
Python implementation of traceroute
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
How to handle the OSError: Socket error: [WinError 10060]?
Hi! Thanks for this piece of code!
I updated it with python3 and added the time that the call last.
You have it in: https://gist.github.com/aalmiramolla/e9cd17a387498fc61b3881c331cc2dc5
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:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to handle exception arising to none in every address