【Python】生成動態路由軌跡圖
阿新 • • 發佈:2019-02-08
準備檔案:
- linux系統
- 安裝:apt-get install tcpdump graphviz ImageMagick
- 安裝:pip install scapy
過程:(要正確配置etc/resolv.config裡面的DNS伺服器)
- 通過scapy的traceroute方法以SYN方式對目標進行TCP服務掃描
- 同時啟動tcpdump抓包, 捕獲所經過的路由點
- 通過graph()方法, 生成svg圖形檔案
- 最後用ImageMagick工具講svg轉換成png檔案
import os,sys,time,subprocess import warnings,logging warnings.filterwarnings("ignore", category=DeprecationWarning) logging.getLogger("scapy.runtime").setLevel(logging.ERROR) from scapy.all import traceroute domains = raw_input('Please input one or more IP/domain: ') target = domains.split(' ') dport = [80] if len(target) >= 1 and target[0]!='': res,unans = traceroute(target,dport=dport,retry=-2) res.graph(target="> test.svg") time.sleep(1) subprocess.Popen("/usr/bin/convert test.svg test.png", shell=True) else: print "IP/domain number of errors,exit"