如何在python中为traceroute获取特定跃点的IP [英] How can I get IP of a specific hop for traceroute in python

查看:498
本文介绍了如何在python中为traceroute获取特定跃点的IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在执行此操作,并获得以下输出.这里是否有某种方法可以获取变量中特定跃点的IP.以及为什么在traceroute结果中缺少某些跳数?

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 

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

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

推荐答案

您可以使用result.get_trace()提取跃点的值. 因此,对于跃点7,请使用result.get_trace()['185.53.178.6'] [7]

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

这篇关于如何在python中为traceroute获取特定跃点的IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆