Skip to content

Instantly share code, notes, and snippets.

@VBaczynski
Created March 10, 2018 09:32
Show Gist options
  • Save VBaczynski/6371a2e65e187aa7f2bbfdc85ea7e0d0 to your computer and use it in GitHub Desktop.
Save VBaczynski/6371a2e65e187aa7f2bbfdc85ea7e0d0 to your computer and use it in GitHub Desktop.
Selection of country-specific IP addresses
#!/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/root/bin
if [ $# != 2 ]; then
echo
echo Usage example:
echo "sh ip2country.sh IPs.list US"
echo
exit 1
fi
iplist=$1
ccode=$2
for IP in `cat $iplist`
do
country=`geoiplookup "$IP" | head -1 | awk '{print $4}' | cut -d , -f1`
if [ "x$country" = 'x' ]; then
# If the IP address does not belong to any country (not in the database)
country="XX"
fi
if [ "$country" = "$ccode" ]; then
echo $IP >> $ccode.ips
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment