import socket import time def main(): dest_name = "google.com" dest_addr = socket.gethostbyname(dest_name) port = 33434 max_hops = 30 icmp = socket.getprotobyname('icmp') udp = socket.getprotobyname('udp') ttl = 1 try: while True: recv_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp) send_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, udp ) send_socket.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl) recv_socket.bind(("", port)) send_socket.sendto("".encode("utf-8"), (dest_name, port)) curr_addr = None curr_name = None try: tic = time.perf_counter() _, curr_addr = recv_socket.recvfrom(128) toc = time.perf_counter() curr_addr = curr_addr[0] try: curr_name = socket.gethostbyaddr(curr_addr)[0] except socket.error: curr_name = curr_addr except: pass except socket.error: pass except: pass finally: send_socket.close() recv_socket.close() if curr_addr is not None: curr_host = "%s (%s)" % (curr_name, curr_addr) else: curr_host = "*" print ("%d\t%s\t\t%f" % (ttl, curr_host, toc-tic)) ttl += 1 if curr_addr == dest_addr or ttl > max_hops: break except: pass if __name__ == '__main__': main()
Here is my code, there is nothing on the terminal after a certain hop.
No comments:
Post a Comment