问题
addr = socket.gethostbyname('dalitstan.org')
target = [addr]
result, unans = traceroute(target,maxttl=32)
I am doing this and getting the below output. Is there some way here to get the IP of specific hop in a variable. And also why some hop number are missing in the traceroute result?
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.83.18 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
On trying result.get_trace()['185.53.178.6'][7]
I am getting below error. What may be the issue.
Traceback (most recent call last):
File "cens2.py", line 16, in <module>
print result.get_trace()['185.53.178.6'][7]
File "/usr/local/lib/python2.7/dist-packages/scapy/layers/inet.py", line 1040, in get_trace
m = min(x for x, y in k.itervalues() if y[1])
File "/usr/local/lib/python2.7/dist-packages/scapy/layers/inet.py", line 1040, in <genexpr>
m = min(x for x, y in k.itervalues() if y[1])
TypeError: 'bool' object has no attribute '__getitem__'
回答1:
You can extract the value for a hop using result.get_trace() So for hop 7 use result.get_trace()['185.53.178.6'][7]
>>> result.get_trace()['185.53.178.6'][7]
('203.50.6.93', False)
or
>>> result.get_trace()['185.53.178.6'][7][0]
'203.50.6.93'
>>> something = result.get_trace()['185.53.178.6'][7][0]
>>> something
'203.50.6.93'
Some index checking from a different trace shown as.
>>> result.get_trace()['161.0.97.141'].keys()
dict_keys([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20])
^^^^ missing hop 13 ^^^^ (Same as your hop 7)
>>> for item in result.get_trace()['161.0.97.141'].keys():
>>> print('Row number ' + str(item) + ' hop IP is : ' + result.get_trace()['161.0.97.141'][item][0])
Row number 1 hop IP is : 10.0.0.138
Row number 2 hop IP is : 10.218.192.1
Row number 3 hop IP is : 58.160.250.129
Row number 4 hop IP is : 203.50.44.36
Row number 5 hop IP is : 203.50.11.136
Row number 6 hop IP is : 203.50.11.180
Row number 7 hop IP is : 203.50.6.93
Row number 8 hop IP is : 203.50.13.98
Row number 9 hop IP is : 202.84.222.62
Row number 10 hop IP is : 202.84.136.209
Row number 11 hop IP is : 202.84.253.86
Row number 12 hop IP is : 134.159.61.46
<<< no hop 13 shown >>>
Row number 14 hop IP is : 4.59.90.110
Row number 15 hop IP is : 63.245.106.96
Row number 16 hop IP is : 63.245.5.100
Row number 17 hop IP is : 63.245.79.85
Row number 18 hop IP is : 190.242.166.37
Row number 19 hop IP is : 190.112.224.132
Row number 20 hop IP is : 161.0.97.141
来源:https://stackoverflow.com/questions/45777867/how-can-i-get-ip-of-a-specific-hop-for-traceroute-in-python