Skip to content

Instantly share code, notes, and snippets.

@Reiner030
Created April 26, 2019 09:31
Show Gist options
  • Save Reiner030/2516a7f9d04576e952ab3990cde785da to your computer and use it in GitHub Desktop.
Save Reiner030/2516a7f9d04576e952ab3990cde785da to your computer and use it in GitHub Desktop.
Improved combined script of https://github.com/mschmitt/GeoLite2xtables for updating Lite and commercial GeoIP2 databases
#/bin/bash
# Lite Access:
# AccountID 0
# LicenseKey 000000000000
# EditionIDs GeoLite2-Country GeoLite2-City
# Licensed Access:
# AccountID 12345
# LicenseKey abcdefghijkl
# EditionIDs 106 GeoIP2-Country GeoLite2-City GeoLite2-ASN
DATE="$(date '+%Y%m%d-%H%M%S')"
SCRIPT_NAME="$(basename $0 | sed -e "s/.sh//")"
TMP_LOG="/tmp/${SCRIPT_NAME}-${DATE}.log"
if [ -t 1 ]
then
# logging to STDOUT and parallel to logfile on interactive run
exec > >(tee -a ${TMP_LOG})
exec 2> >(tee -a ${TMP_LOG} >&2)
else
# logging to STDOUT and parallel to logfile only for cron
exec > ${TMP_LOG}
exec 2>&1
fi
COUNTRY_URL="http://download.geonames.org/export/dump/countryInfo.txt"
COUNTRY_FILE="$(basename ${COUNTRY_URL})"
LICENSE_KEY="$(sed -ne "s/^LicenseKey *//p" /etc/GeoIP.conf)"
EDITION="$(sed -ne "s/^EditionIDs .*\s\(\w\+-Country\) .*/\1/p" /etc/GeoIP.conf)-CSV"
echo
echo -e "$(date '+%Y%m%d-%H%M%S') LicenseKey is:\t${LICENSE_KEY}"
echo -e "$(date '+%Y%m%d-%H%M%S') EditionID is:\t${EDITION}"
if [[ ${EDITION} =~ Lite ]] || [ "${LICENSE_KEY}" = "000000000000" ]
then
EDITION_URL="https://geolite.maxmind.com/download/geoip/database/${EDITION}.zip"
else
EDITION_URL="https://download.maxmind.com/app/geoip_download?edition_id=${EDITION}&license_key=${LICENSE_KEY}&suffix=zip"
fi
# Fetch actual country GeoIP/GeoLite2 Country CSV file:
echo
echo -e "$(date '+%Y%m%d-%H%M%S') Downloading:\t${COUNTRY_URL}"
curl -o /usr/share/GeoIP/${COUNTRY_FILE} -z /usr/share/GeoIP/${COUNTRY_FILE} ${COUNTRY_URL}
# Fetch actual country GeoIP/GeoLite2 Country CSV file:
echo
echo -e "$(date '+%Y%m%d-%H%M%S') Downloading:\t${EDITION_URL}"
curl -o /usr/share/GeoIP/${EDITION}.zip -z /usr/share/GeoIP/${EDITION}.zip ${EDITION_URL}
if find /usr/share/GeoIP/${COUNTRY_FILE} -mtime 0 || \
find /usr/share/GeoIP/${EDITION}.zip -mtime 0
then
echo
echo -e "$(date '+%Y%m%d-%H%M%S') Unzipping updated archive:\t${EDITION}.zip"
unzip -d /usr/share/GeoIP -o -j /usr/share/GeoIP/${EDITION}.zip '*/Geo*2-Country-Blocks*' '*/Geo*2-Country-Locations-en.csv'
# for 3.x script usage:
#if ! [[ ${EDITION} =~ Lite ]] || [ "${LICENSE_KEY}" != "000000000000" ]
#then
# echo -e "Symlinking licensed files for Lite2 based script"
# # setup symlinks for xt_geoip script which expects only Lite2 content
# ln -sf GeoIP2-Country-Locations-en.csv /usr/share/GeoIP/GeoLite2-Country-Locations-en.csv
# ln -sf GeoIP2-Country-Blocks-IPv4.csv /usr/share/GeoIP/GeoLite2-Country-Blocks-IPv4.csv
# ln -sf GeoIP2-Country-Blocks-IPv6.csv /usr/share/GeoIP/GeoLite2-Country-Blocks-IPv6.csv
#fi
echo
echo
echo -e "$(date '+%Y%m%d-%H%M%S') Converting GeoIP2/Lite2 content to version 1 content:"
echo
cat /usr/share/GeoIP/Geo*2-Country-Blocks-IPv{4,6}.csv | \
/usr/lib/xtables-addons/convert_geolite2_to_v1.pl /usr/share/GeoIP/${COUNTRY_FILE} \
> /usr/share/GeoIP/GeoIP-legacy.csv
EXIT_CODE=$?
if [ ${EXIT_CODE} -gt 0 ]
then
echo -e "$(date '+%Y%m%d-%H%M%S') Got error while converting data files for xt_geoip_build 2.x usage; sending full log of update run:"
echo
echo
cat ${TMP_LOG}
exit ${EXIT_CODE}
fi
echo
echo
echo -e "$(date '+%Y%m%d-%H%M%S') Building xt_geoip database for iptables:"
echo
/usr/lib/xtables-addons/xt_geoip_build -D /usr/share/xt_geoip /usr/share/GeoIP/GeoIP-legacy.csv
EXIT_CODE=$?
if [ ${EXIT_CODE} -gt 0 ]
then
echo -e "$(date '+%Y%m%d-%H%M%S') Got error while creating xt_geoip data files; sending full log of update run:"
echo
echo
cat ${TMP_LOG}
exit ${EXIT_CODE}
fi
# for 3.x script usage:
#echo
#echo "$(date '+%Y%m%d-%H%M%S') Creating xt_geoip based database for iptable usage:"
#echo
#/usr/lib/xtables-addons/xt_geoip_build2 -S /usr/share/GeoIP -D /usr/share/xt_geoip/
#EXIT_CODE=$?
#if [ ${EXIT_CODE} -gt 0 ]
#then
# echo -e "$(date '+%Y%m%d-%H%M%S') Got error while creating xt_geoip data files; sending full log of update run:"
# echo
# echo
# cat ${TMP_LOG}
# exit ${EXIT_CODE}
#fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment