如何在python中获取traceroute的特定跃点的IP

时间:2017-08-20 02:13:19

标签: python scapy traceroute

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

我这样做并得到以下输出。有没有办法在变量中获取特定跃点的IP。还有为什么traceroute结果中缺少一些跳数?

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 

在尝试result.get_trace()['185.53.178.6'][7]时,我遇到了错误。可能是什么问题。

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 个答案:

答案 0 :(得分:0)

您可以使用result.get_trace()提取跃点的值 所以对于第7跳使用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'

从不同的迹线检查某些索引,显示为。

    >>> 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