Mapping traceroutes with Perl

I was playing around on my smartphone, looking for interesting apps and found one that displays the trace of a request to a server (e.g. www.google.com). Nevertheless, since I didn’t really need it, it was to expensive and I thought: “ok, let’s do it myself!” and I wrote a little perl script and uploaded it to GitHub. The script traces the route of a request and either produces a static google map as png or writes a GPX file, which can be used in GIS-software or Google maps.

The command “traceroute” is a powerful tool to trace your network traffic to a given server address and see through how many routers your request floats.Two perl modules exist Net::Traceroute and Net::Traceroute::PurePerl. The first one calls the normal linux traceroute command. The later one needs root privileges if I got it right, therefore I didn’t try it and only implemented the first module.

But the above command/modules don’t tell you where in the real world these routers are located. This geographic information has to be obtained from somewhere else: GeoIP-Server (e.g. freegeoip or ip-api) return a lot of information on IP addresses including longitude/latitude. The format and naming in their responses sadly differs. Therefore, they are not simply exchangable. I decided to use only the later one, since the first did not return several locations within Germany (the response was simply “Germany” and nothing else). But who knows, if the other one is more accurate, especially for dynamic IP addresses.

The script can be executed as follows:

maptraceroute.pl -v --target=www.google.com -f png -o maptrace.png
maptraceroute.pl -v --target=www.google.com -f gpx -o maptrace.gpx

The “-v” option outputs the details of the GeoIP requests on the command line. The “-o” argument specifies the file, where to write the data to. The verbose option should not be mixed with output to STDOUT, which is the default, if no “-o” argument is used, since this will produce unusable mixed output.

The first example produces a static google map as shown below, which is for unregistered users limited to the size of 640×640 pixels. The DPI can be doubled by the argument “scale=2” in the created URL.

maptrace

Not all routers are visible, either because they did not respond to the traceroute request or they have the same location and only one is visible in the foreground.

The GPX output of the second line above can be imported to a GIS program (e.g. Viking) or used in “My Maps” in Google maps:

viking.png

googlemaps

The perl script can be downloaded from GitHub.

Advertisements

One thought on “Mapping traceroutes with Perl

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s