Whereami, find out your physical location via command line

Alec Jacobson

December 05, 2009

weblog/

Using a wget and sed combo I found on go2linux and a little web scraping, I've com up with a little command line bash script to find your public ip address and then determine your physical location. Save this in a file called whereami.sh
#!/bin/bash

# get public ip address from checkip.dyndns.org
public_ip=`wget -q -O - checkip.dyndns.org | \
sed -e 's/.*Current IP Address: //' -e 's/<.*$//'`

echo $public_ip

# get physical address from ip address from melissadata.com
wget -q -O - \
http://www.melissadata.com/Lookups/iplocation.asp?ipaddress=$public_ip | \
grep "\(\(City\)\|\(State or Region\)\|\(Country\)\)<\/td>" | \
sed "s/.*<b>\([^<]*\)<\/b>.*/\1/"
Note: If you know of a more stable ip to physical location site to scape leave it below.