Skip to content

Instantly share code, notes, and snippets.

@poqdavid
Last active April 9, 2023 22:33
Show Gist options
  • Save poqdavid/45ff00078cdf63220a4003618800683d to your computer and use it in GitHub Desktop.
Save poqdavid/45ff00078cdf63220a4003618800683d to your computer and use it in GitHub Desktop.
This gist contains a set of scripts and instructions for updating and reading Maxmind and DB-IP databases using bash. The scripts are written in bash and can be used to update the databases on a regular basis. The instructions provide detailed information on how to use the scripts.

Instructions

Requirements

Follow these instructions to install geoipupdate: https://github.com/maxmind/geoipupdate#installing-on-ubuntu-via-ppa

Install ./jq:

$ sudo apt install jq

Install mmdbinspect:

sudo curl -LJO https://github.com/maxmind/mmdbinspect/releases/download/v0.1.1/mmdbinspect_0.1.1_linux_amd64.deb && sudo dpkg -i mmdbinspect_0.1.1_linux_amd64.deb

installing scripts

Script geoip:

$ touch /sbin/geoip
$ nano /sbin/geoip
$ chmod +x /sbin/geoip

Script update_dbip:

$ touch /opt/geoip/update_dbip
$ nano /opt/geoip/update_dbip
$ chmod +x /opt/geoip/update_dbip

Script update_geoip:

$ touch /opt/geoip/update_geoip
$ nano /opt/geoip/update_geoip
$ chmod +x /opt/geoip/update_geoip

Auto update databases

Run $ sudo crontab -e

And add these to the crontab

00 7 * * * /opt/geoip/update_dbip no >> /opt/geoip/logs/`date +\%m-\%d-\%Y`-update_dbip.log 2>&1
00 7 * * * /opt/geoip/update_geoip >> /opt/geoip/logs/`date +\%m-\%d-\%Y`-update_geoip.log 2>&1
#!/bin/bash
#####################################################################
# Variables
#####################################################################
IP=$1
MODE=$2
DBTYPE=$3
file_geoip_country="/usr/share/GeoIP/GeoLite2-Country.mmdb"
file_geoip_city="/usr/share/GeoIP/GeoLite2-City.mmdb"
file_geoip_asn="/usr/share/GeoIP/GeoLite2-ASN.mmdb"
file_dbip_country="/usr/share/DBIP/DBIPLite-Country.mmdb"
file_dbip_city="/usr/share/DBIP/DBIPLite-City.mmdb"
file_dbip_asn="/usr/share/DBIP/DBIPLite-ASN.mmdb"
file_country=""
file_city=""
file_asn=""
#####################################################################
# Functions
#####################################################################
function getasvalue () {
if [ $1 != "null" ];
then
echo "AS$1 $2"
else
echo "null"
fi
}
function valid_ip()
{
local ip=$IP
local stat=1
if [[ $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($IP)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
#####################################################################
# Validate
#####################################################################
# No argument given?
if [ -z "$IP" ]; then
printf "\nUsage:\n\n geoip2lookup IP4_ADDRESS\n\n"
exit 1
fi
# Check ip validity.
if ! valid_ip "$IP"; then
printf "\nThis is not a valid IP address. Exiting.\n\n"
exit 1
fi
#####################################################################
# Execute
#####################################################################
if [ -z "$DBTYPE" ];
then
file_city=$file_geoip_city
file_asn=$file_geoip_asn
file_country=$file_geoip_country
else
case $DBTYPE in
"geoip")
file_city=$file_geoip_city
file_asn=$file_geoip_asn
file_country=$file_geoip_country
;;
"dbip")
file_city=$file_dbip_city
file_asn=$file_dbip_asn
file_country=$file_dbip_country
;;
esac
fi
json=`mmdbinspect --db ${file_city} --db ${file_asn} "$IP" 2>/dev/null`
json_city=$(echo $json | jq -r '.[0].Records')
json_asn=$(echo $json | jq -r '.[1].Records')
city=$(echo $json_city | jq -r '.[0].Record.city.names.en')
subdivisions=$(echo $json_city | jq -r '.[0].Record.subdivisions[0].names.en')
country=$(echo $json_city | jq -r '.[0].Record.country.names.en')
continent=$(echo $json_city | jq -r '.[0].Record.continent.names.en')
location_lat=$(echo $json_city | jq -r '.[0].Record.location.latitude')
location_lon=$(echo $json_city | jq -r '.[0].Record.location.longitude')
location_code=$(echo $json_city | jq -r '.[0].Record.location.metro_code')
location_tz=$(echo $json_city | jq -r '.[0].Record.location.time_zone')
postal=$(echo $json_city | jq -r '.[0].Record.postal.code')
asn_number=$(echo $json_asn | jq -r '.[0].Record.autonomous_system_number')
asn_name=$(echo $json_asn | jq -r '.[0].Record.autonomous_system_organization')
function mode1 () {
printf "
City: ${city}
Territory: ${subdivisions}
Country: ${country}
Continent: ${continent}
Location (approx): ${location_lat},${location_lon}
Metro Code: ${location_code}
Timezone: ${location_tz}
Postal Code: ${postal}
ASN Number: ${asn_number}
ASN Organization: ${asn_name}
\n"
}
function mode2 () {
printf "$IP, ${city}, ${country}, ${continent}, $(getasvalue "${asn_number}" "${asn_name}")\n"
}
function mode3 () {
printf "${city}, ${country}, ${continent}, $(getasvalue "${asn_number}" "${asn_name}")\n"
}
if [ -z "$MODE" ];
then
mode1
else
case $MODE in
"mode1")
mode1
;;
"mode2")
mode2
;;
"mode3")
mode3
;;
esac
fi
#!/bin/bash
REBOOT=$1
DOIREBOOT=false
DATABASEDIR=/usr/share/DBIP
CURRENTDATE=`date +%Y-%m`
LASTUPDATE=``
CITYURL="https://download.db-ip.com/free/dbip-city-lite-${CURRENTDATE}.mmdb.gz"
ASNURL="https://download.db-ip.com/free/dbip-asn-lite-${CURRENTDATE}.mmdb.gz"
CITYDATAFILE=${DATABASEDIR}/citylastupdate
ASNDATAFILE=${DATABASEDIR}/asnlastupdate
CITYDATABASENAME=DBIPLite-City.mmdb
ASNDATABASENAME=DBIPLite-ASN.mmdb
CITYDATABASEFULLPATH="${DATABASEDIR}/${CITYDATABASENAME}"
ASNDATABASEFULLPATH="${DATABASEDIR}/${ASNDATABASENAME}"
CITYGZIPFILE="${DATABASEDIR}/dbip-city-lite-${CURRENTDATE}.mmdb.gz"
CITYUNZIPEDFILE="${DATABASEDIR}/dbip-city-lite-${CURRENTDATE}.mmdb"
ASNGZIPFILE="${DATABASEDIR}/dbip-asn-lite-${CURRENTDATE}.mmdb.gz"
ASNUNZIPEDFILE="${DATABASEDIR}/dbip-asn-lite-${CURRENTDATE}.mmdb"
function checkdatafile() {
if [ -f "$1" ];
then
echo $(date '+%H:%M:%S')": Info: File $1 exists!"
else
echo "0000-00" > $1
fi;
read -r LASTUPDATE < $1
}
function updatedb() {
checkdatafile $2
echo $(date '+%H:%M:%S')": >${CURRENTDATE}< >${LASTUPDATE}<"
echo $(date '+%H:%M:%S')": Current URL $1"
if [ "${LASTUPDATE}" = "${CURRENTDATE}" ]; then
echo $(date '+%H:%M:%S')": DATABASE is Update..."
else
echo $(date '+%H:%M:%S')": DATABASE isn't Update..."
echo $(date '+%H:%M:%S')": Checking Update Source..."
status="$(curl -Is $1 | head -1)"
echo $(date '+%H:%M:%S')": Status: ${status}"
if [[ "$status" == *"200"* ]]; then
echo $(date '+%H:%M:%S')": Downloading DATABASE Update..."
wget -P ${DATABASEDIR} $1
echo $(date '+%H:%M:%S')": Downloaded DATABASE Update."
if [ -f "$3" ];
then
echo $(date '+%H:%M:%S')": Info: File $3 exists!"
echo $(date '+%H:%M:%S')": Decompressing $3..."
gzip -d $3
echo $(date '+%H:%M:%S')": Decompresed $3."
if [ -f "$4" ];
then
echo $(date '+%H:%M:%S')": Info: File $4 exists!"
echo $(date '+%H:%M:%S')": Replacing Update with current DATABASE..."
mv -vf "$4" "$5"
chown ubuntu:ubuntu $5
echo $(date '+%H:%M:%S')": Replaced Update with current DATABASE..."
echo "$CURRENTDATE" > $2
$DOIREBOOT = true
else
echo $(date '+%H:%M:%S')": $4 doesn't exists!"
fi;
else
echo $(date '+%H:%M:%S')": $3 doesn't exists!"
fi;
else
echo $(date '+%H:%M:%S')": UPDATE NOT AVILABLE..."
fi
fi
}
updatedb $CITYURL $CITYDATAFILE $CITYGZIPFILE $CITYUNZIPEDFILE $CITYDATABASEFULLPATH
updatedb $ASNURL $ASNDATAFILE $ASNGZIPFILE $ASNUNZIPEDFILE $ASNDATABASEFULLPATH
if [ $DOIREBOOT = true ] && [ "${REBOOT}" = "yes" ];
then
echo $(date '+%H:%M:%S')": Rebooting server..."
/sbin/reboot
fi
#!/bin/bash
status="$(geoipupdate -v 2>&1 | grep 'No new updates available for')"
echo "$status"
if [[ "$status" =~ .*"GeoLite2-City".* ]]; then
echo $(date '+%H:%M:%S')": No update..."
else
echo $(date '+%H:%M:%S')": Updated..."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment