Answers:
假设有可免费下载的GeoIP数据库,几乎所有编程语言和库的免费绑定,允许在世界地图上绘制点,您可以尝试实现自己的可视化traceroute。这对于一个小项目来说是个好主意。
编辑:虽然这个答案更适合StackOverflow,但这是一个用Python编写的非常原始的可视traceroute应用程序。虽然它适用于Linux,但它并不适合所有人,因为它有很多缺点:
Import Error
如果你没有它们,你会得到的mtr
安装的Linux上运行这是代码:
#!/usr/bin/env python
"""visual traceroute"""
import subprocess
import sys
import time
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import pygeoip
import wugeo
GEOIPDB = "GeoLiteCity.dat"
class MyForm(QDialog):
"""Main app window"""
def __init__(self, parent = None):
"""doc"""
super(MyForm, self).__init__(parent)
self.setWindowTitle("Visual Route")
self.image_label = QLabel(self)
self.image_label.setMinimumSize(800, 600)
self.image_label.setAlignment(Qt.AlignCenter)
self.load_image("map.jpg")
self.ip_edit = QLineEdit(self)
self.tr_button = QPushButton("Traceroute", parent=self)
layout = QVBoxLayout()
layout.addWidget(self.image_label)
layout.addWidget(self.ip_edit)
layout.addWidget(self.tr_button)
self.setLayout(layout)
self.connect(self.tr_button, SIGNAL("clicked()"), self.traceroute)
def load_image(self, file_name):
"""Loads an image"""
image = QImage(file_name)
self.image_label.setPixmap(QPixmap.fromImage(image))
self.repaint()
def traceroute(self):
"""Do the traceroute thing"""
self.tr_button.setEnabled(False)
ip = self.ip_edit.text()
p = subprocess.Popen(["sudo", "/usr/sbin/mtr", "-n", "-c", "1",
"--raw", ip], stdout=subprocess.PIPE)
output = p.communicate()[0]
lines = output.split("\n")
ip_lines = lines[::2][:-1] # filter odds, skip last
ips = [x.split()[2] for x in ip_lines]
coords = self.get_coords(ips)
self.draw_dots(coords)
self.tr_button.setEnabled(True)
@staticmethod
def get_coords(ips):
"""Get coords using pygeoip"""
coords = []
geoip = pygeoip.GeoIP(GEOIPDB, pygeoip.MMAP_CACHE)
for ip in ips:
record = geoip.record_by_addr(ip)
latitude = record["latitude"]
longitude = record["longitude"]
location = (latitude, longitude, 1, "red")
coords.append(location)
return coords
def draw_dots(self, coords):
"""Draws dots on the world map
Uses temporary files (ugly!)"""
infile = "map.jpg"
outfile = "/tmp/outmap.jpg"
for coord in coords:
wugeo.geo_marker([coord], infile, outfile)
self.load_image(outfile)
time.sleep(1)
infile = outfile
def main():
"""Main function"""
app = QApplication(sys.argv)
form = MyForm()
form.show()
app.exec_()
if __name__ == "__main__":
main()