Scott Greenup

Holder of Coding Trinkets

Georoute

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"
    sys.exit(1)

target = sys.argv[1]

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

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

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

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

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