File: traceroute.py

package info (click to toggle)
python-icmplib 2.1.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 256 kB
  • sloc: python: 1,903; makefile: 17
file content (46 lines) | stat: -rw-r--r-- 1,063 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'''
    icmplib
    ~~~~~~~

    A powerful library for forging ICMP packets and performing ping
    and traceroute.

        https://github.com/ValentinBELYN/icmplib

    :copyright: Copyright 2017-2021 Valentin BELYN.
    :license: GNU LGPLv3, see the LICENSE for details.

    ~~~~~~~

    Example: traceroute
'''

from icmplib import traceroute


hops = traceroute('1.1.1.1', timeout=1, fast=True)

print(hops)
# [ <Hop 1 [10.0.0.1]>,
#   <Hop 2 [194.149.169.49]>,
#   <Hop 3 [194.149.166.54]>,
#   <Hop 5 [212.73.205.22]>,
#   <Hop 6 [1.1.1.1]> ]

last_distance = 0

for hop in hops:
    if last_distance + 1 != hop.distance:
        print('  *     Some gateways are not responding')

    print(f'  {hop.distance:<2}    {hop.address:15}    '
          f'{hop.avg_rtt} ms')

    last_distance = hop.distance

#   1       10.0.0.1            5.196 ms
#   2       194.149.169.49      7.552 ms
#   3       194.149.166.54      12.21 ms
#   *       Some gateways are not responding
#   5       212.73.205.22       22.15 ms
#   6       1.1.1.1             13.59 ms