Skip to content

Instantly share code, notes, and snippets.

@crackjack crackjack/ip2geo.sh

Created Apr 3, 2013
Embed
What would you like to do?
Bash Script to find Geolocation from IP Addresses in apache's access log. You need to have "geoiplookup" package from GeoLite package. The excerpts are taken from http://danielmiessler.com/blog/a-simple-script-for-harvesting-dns-country-state-and-city-information-from-a-list-of-ip-addresses
#!/usr/bin/env bash
cat /var/log/apache2/access_log | awk '{print $1}' > ips.txt
uniq ips.txt > uniqips.txt
IPS=`cat uniqips.txt`
for i in $IPS
do
echo "$i,`geoiplookup $i | cut -d "," -f2 | sed -e 's/^[\t]//'`" >> ipinfo.csv
done
@REDLINK

This comment has been minimized.

Copy link

REDLINK commented Dec 16, 2014

Shouldn't it be sort ips.txt | uniq > uniqips.txt?

@denics

This comment has been minimized.

Copy link

denics commented Nov 10, 2015

To get this output: xxx.xxx.xxx.xxx, xx.xxx yy.yyy change the echo line with the following

echo "$i, `geoiplookup $i | awk -F, 'NR==2{print $7, $8}' | sed -e 's/,//'`" >> ipinfo.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.