- windows10
- python3.6.1
- pycharm2018
- scapy2.4.2
Graphviz和ImageMagick的应用程序,而非python的模块
直接pip install scapy即可
- Graphviz的windows msi安装包如下,linux自己去Graphviz官网解决
https://graphviz.gitlab.io/_pages/Download/windows/graphviz-2.38.msi - ImageMagick的windows msi安装包如下,linux自己去ImageMagick官网解决
https://imagemagick.org/download/binaries/
笔者的ImageMagick版本为:ImageMagick-7.0.8-34-Q16-x64-dll.exe
注:链接进不去的话搭配请搭配梯子
分别给Graphviz和ImageMagick添加环境变量
这点很重要!!!!没有添加的话将无法使用traceroute中的graph函数绘制追踪图。
- 笔者添加的Graphviz的环境变量路径如下 C:\Program Files (x86)\Graphviz2.38\bin
- 笔者添加的ImageMagick的环境变量路径 C:\Program Files\ImageMagick-7.0.8-Q16
直接上代码:
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] != '': # 启动路由跟踪 res, unans = traceroute(domains, dport=dport, retry=-2) # traceroute生成的信息绘制成svg res.graph(target="> graph.svg", ASres=AS_resolver_radb(), type="svg") # ASres=AS_resolver_radb()改变为可用的whois提供商,而非原来的ASres=None后默认的被qiang了的提供商 time.sleep(1) # svg 转格式为 png #subprocess.Popen("/usr/local/bin/convert test.svg test.png", shell=True) else: print("IP/domain number of errors, exit")
结果如下:
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
报错类型为
RuntimeError: Could not contact whois providers
想了解详细原因的请移步 https://www.oschina.net/question/2917603_2278439 围观最下面的okidingme的回答。
解决办法,请看笔者给出的代码:
from scapy.as_resolvers import AS_resolver_radb
res.graph(target="> graph.svg", ASres=AS_resolver_radb(), type="svg")
ASres=AS_resolver_radb()改变为可用的whois提供商,而非原来的ASres=None后默认的被qiang了的提供商