Skip to content

Instantly share code, notes, and snippets.

@ssstonebraker
Created April 26, 2018 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssstonebraker/5ca03b305a85611790569332e64c39d8 to your computer and use it in GitHub Desktop.
Save ssstonebraker/5ca03b305a85611790569332e64c39d8 to your computer and use it in GitHub Desktop.
Determine Country Code for a List of IP Addresses
#!/bin/bash
# Usage: ./ipinfo.sh file_containing_one_ip_per_line
filename=$1
ipAddresses=`cat $filename`
`echo "" > out.txt` #To empty the file
readonly ourPath="$(dirname $0)"
# Check if geoiplookup Exists
if [ ! -x "$(which geoiplookup)" ]; then
echo "geoiplookup does not exist, exiting..."
exit 1
fi
# Download Country DB if it does not exist
readonly geoIPDB="$ourPath/GeoIP.dat"
if [ ! -e "$geoIPDB" ]; then
echo "GeoIP DB not found... downloading"
cd $ourPath
curl -s -O "http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz"
gunzip GeoIP.dat.gz
fi
for ip in $ipAddresses
do
cmd="$(geoiplookup -f GeoIP.dat ${ip} | awk '{ print $4 }' | sed 's/.$//')"
echo "${ip} ${cmd}" >> out.txt
done
echo "country codes appended to ip addresses. File can be found at out.txt"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment