Skip to content

Instantly share code, notes, and snippets.

Last active Aug 29, 2015
What would you like to do?
pfSense Bash script to d/l .csv of Country IP ranges and extract each range into it's respective country file
# script downloads from
# extracts IP ranges into pfBlocker's country files (ie:/usr/local/pkg/pfblocker/US.txt)
# req unzip util,
# ie: pkg_add -r
# still needs method to convert ranges to CIDR
# define working dir
# specify data tempfile
# specify temp country code files
# download, unzip the contents into working dir
cd $workdir
/usr/local/bin/unzip $workdir/
# 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
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
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
# 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/
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