traceroute Routing Tracking with Python and Capy

Catalog

Author's Configuration Environment

  • windows10
  • python3.6.1
  • pycharm2018
  • scapy2.4.2

Need for support

Graphviz and ImageMagick applications, not python modules

Step 1: Install scapy

Direct pip install scapy

Step 2: Install Graphviz and Image Magick

Note: If you can't get the link in, please match the ladder.

Step 3: Add environment variables

Add environment variables to Graphviz and ImageMagick, respectively

This is very important!!!! If it is not added, the graph function in traceroute will not be able to draw the trace graph.

  • The path of Graphviz environment variable added by the author is as follows: C: Program Files (x86) Graphviz 2.38 bin
  • The environment variable path C: Program Files ImageMagick-7.0.8-Q16 added by the author

Step 4: Testing

Code directly:

import os,sys,time,subprocess
from scapy.as_resolvers import AS_resolver_radb  
from scapy.all import traceroute
domains='www.baidu.com'
target = domains.split(' ')
dport = [80]
if len(target) >= 1 and target[0] != '':
    # Start Route Tracking
    res, unans = traceroute(domains, dport=dport, retry=-2)
    # The information generated by traceroute is rendered as svg
    res.graph(target="> graph.svg", ASres=AS_resolver_radb(), type="svg") # Change ASres=AS_resolver_radb() to a available whois provider instead of the default qiang provider after the original ASres=None
    time.sleep(1)
    # svg to png
    #subprocess.Popen("/usr/local/bin/convert test.svg test.png", shell=True)
else:
    print("IP/domain number of errors, exit")

The results are as follows:

Begin emission:
********Finished sending 30 packets.
**************Begin emission:
Finished sending 8 packets.
Begin emission:
Finished sending 8 packets.

Received 22 packets, got 22 answers, remaining 7 packets
   183.232.231.174:tcp80 
1  10.21.123.254   11    
2  172.16.9.254    11    
3  10.32.254.2     11    
4  10.32.254.10    11    
5  10.36.254.18    11    
6  10.0.1.6        11    
7  111.59.124.129  11    
9  111.12.4.124    11    
17 183.232.231.174 SA    
18 183.232.231.174 SA    
19 183.232.231.174 SA    
20 183.232.231.174 SA    
21 183.232.231.174 SA    
22 183.232.231.174 SA    
23 183.232.231.174 SA    
24 183.232.231.174 SA    
25 183.232.231.174 SA    
26 183.232.231.174 SA    
27 183.232.231.174 SA    
28 183.232.231.174 SA    
29 183.232.231.174 SA    
30 183.232.231.174 SA  

Generate svg images

Problems encountered and Solutions

The error type is

RuntimeError: Could not contact whois providers

For more details, please move on. https://www.oschina.net/question/2917603_2278439 Look around at the answer of okidingme at the bottom.

The solution, please see the code given by the author:

from scapy.as_resolvers import AS_resolver_radb  
res.graph(target="> graph.svg", ASres=AS_resolver_radb(), type="svg")

Change ASres=AS_resolver_radb() to a available whois provider instead of the default qiang provider after the original ASres=None

Keywords: PHP Windows Linux Python pip

Added by Lustre on Mon, 08 Jul 2019 21:32:12 +0300