Last active
August 29, 2015 14:05
-
-
Save LinuxTracker/8332e0d620ab5f6df1dc to your computer and use it in GitHub Desktop.
pfSense Bash script to d/l .csv of Country IP ranges and extract each range into it's respective country file
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/sh | |
# script downloads GeoIPCountryCSV.zip from geolite.maxmind.com | |
# extracts IP ranges into pfBlocker's country files (ie:/usr/local/pkg/pfblocker/US.txt) | |
# req unzip util, | |
# ie: pkg_add -r http://ftp-archive.freebsd.org/pub/FreeBSD-Archive/ports/amd64/packages-8.3-release/Latest/unzip.tbz | |
# still needs method to convert ranges to CIDR | |
# define working dir | |
workdir='/usr/local/pkg' | |
# specify data tempfile | |
workfile=$workdir/workfile.txt | |
# specify temp country code files | |
clist=$workdir/clist.txt | |
countries=$workdir/countries.txt | |
# download GeoIPCountryCSV.zip, unzip the contents into working dir | |
cd $workdir | |
/usr/bin/fetch http://geolite.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip | |
/usr/local/bin/unzip $workdir/GeoIPCountryCSV.zip | |
# removes double quotes, copy remainder into workfile.txt | |
echo "--> stripping double quotes" | |
sed 's/\"//g' $workdir/GeoIPCountryWhois.csv > $workfile | |
# next we empty pfBlocker files w/ same name as files we're updating | |
echo "--> preparing pfBlocker files" | |
# extract all the country codes into countries.txt | |
while IFS=, read var1 var2 var3 var4 var5 var6 | |
do | |
echo $var5 >> $countries | |
done < $workfile | |
# erase duplicate country codes, sort the remainder and copy into clist.txt | |
cat $countries | sort -u > $clist | |
# zero out contents of pfBlocker files w/ same names as ones we're updating | |
while read p; do | |
0>"$workdir"/pfblocker/$p.txt | |
done < $clist | |
# next we copy IP ranges into their country files | |
echo "--> updating pfBlocker country data" | |
#specify comma as delimiter and choose columns | |
while IFS=, read var1 var2 var3 var4 var5 var6 | |
do | |
# add hyphen between IPs from cols 1&2, add IPs to country files | |
echo $var1-$var2 >> $workdir/pfblocker/$var5.txt | |
done < $workfile | |
# delete our temp files so we start w/ empty files next run | |
echo "--> cleanup" | |
rm $workdir/GeoIPCountryCSV.zip | |
rm $workdir/GeoIPCountryWhois.csv | |
rm $workfile | |
rm $countries | |
rm $clist |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment