Created
April 26, 2018 06:14
-
-
Save ssstonebraker/5ca03b305a85611790569332e64c39d8 to your computer and use it in GitHub Desktop.
Determine Country Code for a List of IP Addresses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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