1
addr = socket.gethostbyname('dalitstan.org')
target = [addr]
result, unans = traceroute(target,maxttl=32)

I am using above code and getting below output. How can I get the number of hops in between my machine and server i.e 18 for the below case?

Begin emission:
*Finished to send 32 packets.
************************
Received 25 packets, got 25 answers, remaining 7 packets
   185.53.178.6:tcp80 
1  192.168.43.1    11 
3  10.71.80.2      11 
4  172.16.26.245   11 
5  172.26.31.242   11 
11 49.44.18.38     11 
13 103.198.140.164 11 
14 103.198.140.27  11 
15 80.81.194.27    11 
16 217.64.161.25   11 
17 217.64.170.214  11 
18 185.53.178.6    SA 
19 185.53.178.6    SA 
20 185.53.178.6    SA 
21 185.53.178.6    SA 
22 185.53.178.6    SA 
23 185.53.178.6    SA 
24 185.53.178.6    SA 
25 185.53.178.6    SA 
26 185.53.178.6    SA 
27 185.53.178.6    SA 
28 185.53.178.6    SA 
29 185.53.178.6    SA 
30 185.53.178.6    SA 
31 185.53.178.6    SA 
32 185.53.178.6    SA 
25
tarun14110
  • 940
  • 5
  • 26
  • 57

1 Answers1

1

The result you're looking for is the first answer with a TCP layer (since previous hops answer with an ICMP packet). To be even more precise, you're looking for the lower TTL of the packets you have sent that got a TCP answer.

In Scapy, you can select the (probe, answer) tuples from an SndRcvList object containing a TCP layer using [TCP]: result[TCP]. You can compare result.show() with result[TCP].show() if I'm not clear enough.

Now, to extract the ttl values of the sent packets, you would use [snd[IP].ttl for snd, _ in result[TCP]]. You just need to get the minimum value using min() (or the first value using [0] or next() actually, since the packets are sorted; I feel like using min() is safer however):

result, _ = traceroute('dalitstan.org')
min(snd[IP].ttl for snd, _ in result[TCP])
Pierre
  • 6,047
  • 1
  • 30
  • 49