Scott Greenup

Holder of Coding Trinkets


Published January 20, 2016
Posted by Scott Greenup

It's kind of like a traceroute but does a geolookup as well. This requires geoip and traceroute to be installed.

$ pacman -S geoip traceroute

The python script is below. It basically just does a traceroute and then runs a geoip lookup on every IP in the traceroute result. It is a very basic script but it is interesting to see where your routes are going.

#!/usr/bin/env python2

from subprocess import Popen
from subprocess import PIPE

import sys

if len(sys.argv) < 2:
    print "usage:", sys.argv[0], "target_ip"

target = sys.argv[1]

print "performing traceroute...\n"
x = Popen("sudo traceroute -I", shell=True, stdout=PIPE)

splat = x.split('\n')
splat = splat[2:]

for i in splat:
    if "* * *" in i:
        print "* * *"
        print ""
    if "(" not in i:

    ip =  i.split("(")[1].split(")")[0]
    hostname = i.split(" ")
    for j in hostname:
        if "." in j:
            hostname = j

    print "IPv4:", ip
    print "HOSTNAME:", hostname
    geo = Popen("geoiplookup " + ip, shell=True, stdout=PIPE)
    print geo