0

So I've been trying to understand how this code works using Scapy on Python as I am trying to create a similar traceroute for my project. However, this does not work. I tried printing the reply type and it shows '11' in between hops. Does anyone have any suggestions on how to fix this or does this whole thing just not work?

hostname = "172.217.17.46" #Change that to user input later

    print("Destination: " + hostname)

    for i in range(1,28):
        pkt = IP(dst=hostname, ttl=i)/ UDP(dport=33434)
    
        #packet is sent
        reply = sr1(pkt, verbose=0)
        #print(reply.type)
    
        #No reply
        if reply is None:
            print("hi")
            break
            
        elif reply.type == 3:
            #reached
            print("Done", reply.src)
            break
            #continue
        else:
            #print(reply.type)
            print ("%d hops away: " %i, reply.src)
            #continue
        break
  • What do you mean by "this does not work"? There's at least two problems: you haven't set a timeout for `sr1`, so it will potentially hang indefinitely. You also have a `break` at the bottom of the loop, so it will never get past the first iteration. Once you fix those two things, (along with getting rid of the `break` in the `reply is None` case), it works. – Carcigenicate Nov 16 '20 at 15:22
  • For reference, [here's a simple traceroute](https://gist.github.com/carcigenicate/c407d7c592864fadc3f72959bb616e4a) I wrote a few months ago, and [here's](https://codereview.stackexchange.com/questions/249905/simplified-windows-tracert-command) a more complicated version that mimics the actual command. – Carcigenicate Nov 16 '20 at 15:27

0 Answers0