Hello, I’m Zac, a student at University of Victoria majoring in Geomatics (a joint Computer Science and Geography program). Today I’ll be writing about one of the first programs I wrote years ago. Have you ever been curious about the path your packets take geographically, between your computer and the sites you visit? The unix command line utility traceroute (or tracert on windows) shows you this information as a series of IP addresses, from your home router to destination. It works…

“[…]by sending packets with gradually increasing TTL value, starting with TTL value of one. The first router receives the packet, decrements the TTL value and drops the packet because it then has TTL value zero. The router sends an ICMP Time Exceeded message back to the source. The next set of packets are given a TTL value of two, so the first router forwards the packets, but the second router drops them and replies with ICMP Time Exceeded. Proceeding in this way, traceroute uses the returned ICMP Time Exceeded messages to build a list of routers that packets traverse, until the destination is reached and returns an ICMP Echo Reply message” (Comer, 2004)

What my code does is parse the IP addresses from the output with regular expressions, sends them off to an online geoip translation service, then writes out the results into a single .csv that can be read in by mapping software such as ArcGIS online webmaps. Here is an example, going to www.sysselmannen.no, which is the website of the Governor of Svalbard.

Here’s code for this script:

#!/bin/bash
# traceMap.sh
# Zac Henderson

echo "enter your destination (ex: www.gov.za): "
read hostn
mapfile -t trace < <( traceroute $hostn )

for i in "${trace[@]}"
do
        echo $i | grep -oP "([0-9]{1,3}\.){3}[0-9]{1,3}" >> IPlist.txt
done

mapfile -t ips < <( uniq --check-chars=7 < IPlist.txt )

output[0]='IP,CountryCode,CountryName,RegionCode,RegionName,City,ZipCode,TimeZone,Latitude,Longitude,MetroCode'
echo ${output[0]} > $hostn.csv

cnt="${#ips[@]}"
for ((i=2;i<cnt;i++))
do 
	output[i-1]=$(curl "freegeoip.net/csv/"${ips[i]})
done 

for e in "${output[@]}"
do
	echo $e >> $hostn.csv
done

rm IPlist.txt

At present the shell script isn’t terribly portable (it would likely not work with Mac’s builtin version of bash), as it relies on a newer function called “mapfile” that maps command output to an array. It should work on any *nix system however, and it might work on Windows through their linux subsystem and “Bash on Ubuntu on Windows.” All the same, it shouldn’t be too difficult to port or modify. My next article will likely focus on the cheap and fascinating RTL-SDR dongle and the cool things you can do with it, like intercepting NOAA weather satellite broadcasts for weather maps, and mapping boat and plane positions with AIS and ADS-B signals pulled from the ether.

Until next time,
Zac


References:

Comer, Douglas (2004). Computer Network and Internets with Internet Applications. Pearson Education, Inc. pp. 360–362. ISBN 0131433512.