<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Friendly patterns and algorithms &#187; mashups</title>
	<atom:link href="http://www.palgorithm.co.uk/category/mashups/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.palgorithm.co.uk</link>
	<description>Discussion of algorithms for games, graphics and general engineering</description>
	<lastBuildDate>Mon, 31 May 2010 11:21:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Mini-mashup &#8211; a quick stab at mapping IP addresses</title>
		<link>http://www.palgorithm.co.uk/2008/11/mini-mashup-a-quick-stab-at-mapping-ip-addresses/</link>
		<comments>http://www.palgorithm.co.uk/2008/11/mini-mashup-a-quick-stab-at-mapping-ip-addresses/#comments</comments>
		<pubDate>Sat, 29 Nov 2008 19:02:22 +0000</pubDate>
		<dc:creator>Sam Martin</dc:creator>
				<category><![CDATA[mashups]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[interweb]]></category>
		<category><![CDATA[one liners]]></category>

		<guid isPermaLink="false">http://www.palgorithm.co.uk/?p=9</guid>
		<description><![CDATA[(First post! Something simple to get started with&#8230;)
Wheel re-invention, we all understand, is a bad thing. But don&#8217;t let a mantra like that stop you! Sometimes re-inventing a wheel is just the best way in really understand how wheels work, and who knows you may invent a better wheel.
With than in mind I had a [...]]]></description>
			<content:encoded><![CDATA[<p>(First post! Something simple to get started with&#8230;)</p>
<p>Wheel re-invention, we all understand, is a bad thing. But don&#8217;t let a mantra like that stop you! Sometimes re-inventing a wheel is just the best way in really understand how wheels work, and who knows you may invent a better wheel.</p>
<p>With than in mind I had a play around with visualising the route my home pc take when talking to the host of this blog, <a href="http://laughingsquid.com/">laughing squid</a>. Web developers have long since mashed-up utilities such as traceroute with the Google Maps API to show the route taken from your PC to another ip address, but I was curious to know the details. Obtaining and displaying this kind of data on the command line is the kind of thing you might hope would be trivial, and thankfully it doesn&#8217;t seem too hard to get a rough and ready result – but the emphasis is definitely on the &#8216;rough&#8217;.</p>
<p>First off, the bulk of the work is done by the common-garden *nix utility &#8216;traceroute&#8217;. In a nutshell it reports basic details of each computer it hops through on the way to a specified destination.</p>
<p><code><br />
<span class="prompt">$</span> <span class="stdin">traceroute laughingsquid.com</span></code></p>
<p><span class="stdout">traceroute to laughingsquid.com (72.32.93.164), 30 hops max, 40 byte packets<br />
1  192.168.1.1 (192.168.1.1)  0.594 ms  0.899 ms  1.199 ms<br />
2  10.128.192.1 (10.128.192.1)  9.130 ms  9.376 ms  15.346 ms<br />
&#8230;<br />
15  aggr115a.dfw1.rackspace.net (72.3.129.109)  123.069 ms  123.392 ms  127.058 ms<br />
16  octopus.laughingsquid.net (72.32.93.164)  127.960 ms  123.888 ms  121.828 ms<br />
</span></p>
<p>Extremely helpful, but very boring. A helpful article in <a href="http://www.linuxjournal.com/article/7856">Linux Journal</a> notes that it&#8217;s possible to  lookup an ip address with a simple web query to the public <a href="http://www.caida.org/tools/utilities/netgeo/">NetGeo</a> database, although the location data is now really out of date and so has to be taken with a big pinch of salt. You can try it out in your web browser:</p>
<p><code>http://netgeo.caida.org/perl/netgeo.cgi?target=<em>my_ip_address</em></code></p>
<p>Returning a webpage isn&#8217;t the most helpful form for a one-liner, but it&#8217;s got a basic structure so you can do some rough and ready parsing to pull out the good bits. Using wget (“web get”) with the <code>-O -</code> option (print the output to standard out) you can lookup the geographical info of an ip address in a one-liner:</p>
<p><code><br />
<span class="prompt">$</span><span class="stdin"> function geolookup { wget -q -O - http://netgeo.caida.org/perl/netgeo.cgi?target=$1 | egrep '^[A-Z]+:' | sed 's/&lt;br&gt;//' }</span></code></p>
<p><span class="prompt">$</span><span class="stdin"> geolookup 207.46.193.254 # lookup microsoft</span></p>
<p><span class="stdout">TARGET:        207.46.193.254<br />
NAME:          MICROSOFT-GLOBAL-NET<br />
NUMBER:        207.46.0.0 &#8211; 207.46.255.255<br />
CITY:          REDMOND<br />
STATE:         WASHINGTON<br />
COUNTRY:       US<br />
LAT:           47.67<br />
LONG:          -122.12<br />
NIC:           ARIN<br />
RATING:<br />
STATUS:        OK</span></p>
<p>Very handy! Incidentally, the latitude and longitude are exactly what you need if you were going to plug this into the Google Maps API.</p>
<p>One fly in the ointment is that NetGeo database only accepts ip address, not domain names. As a hack – and I&#8217;m sure there&#8217;s probably a cleaner way to do this &#8211; you can make use of &#8216;ping&#8217; to resolve domain names to ip addresses for you. (I find this easier than parsing the output of &#8216;host&#8217;, which is the other, better, alternative).</p>
<p><code><br />
<span class="prompt">$</span><span class="stdin"> function iplookup { ping -c 1 $1 | grep 'PING' | sed 's/[\(\)]//g' | awk '{print $3}' }</span><br />
<span class="prompt">$</span><span class="stdin"> iplookup www.microsoft.com</span><br />
<span class="stdout">207.46.193.254</span><br />
<span class="prompt">$</span><span class="stdin"> geolookup $(iplookup www.microsoft.com)</span><br />
<span class="stdout">TARGET:        207.46.193.254<br />
NAME:          MICROSOFT-GLOBAL-NET<br />
..<br />
</span></code></p>
<p>In order to feed the output of traceroute through geolookup we need to scrape the ip addresses out of each line. This will just print a list of each ip address traceroute encounters:<br />
<code><span class="prompt">$</span><span class="stdin"> function iproute { traceroute $1 | grep -v 'traceroute|*' | sed 's/[\(\)]//g' | awk '{ print $3 }' } </span><br />
<span class="stdout">192.168.1.1<br />
10.128.192.1<br />
...</span><br />
</code></p>
<p>It&#8217;s then possible to loop over the output of iproute, feed it into geolookup, and strip off the info we want from that. Here, I&#8217;m just reporting the second word in any line that starts with CITY.</p>
<p><code><br />
<span class="prompt">$</span><span class="stdin"> for i in $(iproute www.microsoft.com); do geolookup $i | grep CITY | awk '{print $2}'; done</span></code></p>
<p><span class="stdout">MARINA<br />
MARINA<br />
AMSTERDAM<br />
AMSTERDAM<br />
FARNBOROUGH<br />
AMSTERDAM<br />
MUNSTER<br />
AMSTERDAM<br />
REDMOND<br />
REDMOND<br />
REDMOND<br />
REDMOND<br />
REDMOND<br />
REDMOND<br />
REDMOND<br />
</span></p>
<p>Hmm. Sort of there. The approach is ok, but the content is a bit crap.</p>
<p>The MARINA entries are the city result returned for local network addresses (192.168.*) and would need special casing to handle properly, but more serious is that you don&#8217;t have to try this out on many addresses before you find problems with the NetGeo database and/or uninteresting traceroute results &#8211; although I can&#8217;t say I wasn&#8217;t warned. The NetGeo front page states all over it that it&#8217;s unreliable!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.palgorithm.co.uk/2008/11/mini-mashup-a-quick-stab-at-mapping-ip-addresses/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
