Skip to content
Snippets Groups Projects
Commit 19585e7d authored by Jan Mach's avatar Jan Mach
Browse files

Improved utility script fetch-negistry.sh for fetching geolocation databases.

Added comments, added ability to receive and process command line arguments for better usability, few additional minor tweaks. (Redmine issue: #3443)
parent b21c421a
No related branches found
No related tags found
No related merge requests found
......@@ -8,4 +8,4 @@
#
# Run every day at 1am
0 1 * * * mentat /etc/mentat/scripts/fetch-negistry.sh
0 1 * * * root /etc/mentat/scripts/fetch-negistry.sh --quiet
#!/bin/bash
#-------------------------------------------------------------------------------
# Fetch whois data from CESNET`s Negistry service.
# Utility script for fetching database from CESNET's Negistry service.
#
# Copyright (C) since 2011 CESNET, z.s.p.o
# Use of this source is governed by the MIT license, see LICENSE file.
#-------------------------------------------------------------------------------
function die {
/bin/echo "ERROR: $1" 1>&2
#
# Helper functions for displaying information of various severity to the user.
#
function error {
/bin/echo -e "${RED}[ERROR] $1${NC}" 1>&2
exit 1
}
function info {
if [ $FLAG_QUIET != 'YES' ]; then
/bin/echo "$1"
fi
}
function success {
if [ $FLAG_QUIET != 'YES' ]; then
/bin/echo -e "${GREEN}[SUCCESS] $1${NC}"
fi
exit 0
}
function usage {
/bin/echo ""
/bin/echo "Utility script for fetching database from CESNET's Negistry service."
/bin/echo ""
/bin/echo "Available options:"
/bin/echo " -h | --help display this help message and exit"
/bin/echo " -d | --devel fetch whois database from development server"
/bin/echo " -q | --quiet run in quiet mode, display only errors"
/bin/echo " -s | --stub install stub file in case of download error"
/bin/echo ""
/bin/echo " -t=file"
/bin/echo " --target=file name of the target file (defaults to '/var/mentat/whois-negistry.json')"
/bin/echo ""
}
#-------------------------------------------------------------------------------
#
# Color code definitions for colored terminal output.
# Source: https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
#
RED='\033[0;31m'
GREEN='\033[0;32m'
ORANGE='\033[0;33m'
NC='\033[0m'
#
# Define global variables.
#
FLAG_DEVEL=NO
FLAG_QUIET=NO
FLAG_STUB=NO
LINK="https://negistry.cesnet.cz/db/api/v1/negistry-ripe-cesnet-ipranges.json"
LINK_DEV="https://irgames.cesnet.cz/negistry/db/api/v1/negistry-ripe-cesnet-ipranges.json"
TARGET="/var/mentat/whois-negistry.json"
if [ $# -gt 0 ]; then
if [ $1 == "dev" ]; then
LINK=$LINK_DEV
fi
#-------------------------------------------------------------------------------
#
# Command line argument parsing.
# Source: https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash
#
for i in "$@"
do
case $i in
-h|--help)
usage
exit 0
;;
-d|--devel)
FLAG_DEVEL=YES
shift # past argument=value
;;
-q|--quiet)
FLAG_QUIET=YES
shift # past argument=value
;;
-s|--stub)
FLAG_STUB=YES
shift # past argument=value
;;
-t=*|--target=*)
TARGET="${i#*=}"
shift # past argument=value
;;
*)
usage
error "Invalid usage, unknown command line option '$i'"
;;
esac
done
#-------------------------------------------------------------------------------
if [ $FLAG_DEVEL == 'YES' ]; then
LINK=$LINK_DEV
fi
info "Using remote server '$LINK'"
info "Using local target file '$TARGET'"
# Change to root directory
#
# Change to root directory.
#
cd /
# Prefered method is curl
#
# Prefered method is curl.
#
if which curl > /dev/null
then
curl -4 --silent --insecure --output $TARGET $LINK
# Wget fallback
#
# Use wget as fallback.
#
elif which wget > /dev/null; then
wget -4 --no-verbose --no-check-certificate -O $TARGET $LINK
# Report the error
else
die "Unable to download whois dump file, missing curl or wget"
error "Unable to download Negistry whois database, missing 'curl' or 'wget' utilities"
fi
#
# Check, that the download was successfull (target file must exist).
#
if [ ! -f "$TARGET" ]
then
if [ $FLAG_STUB == 'YES' ]; then
info "Download of Negistry whois database failed, installing empty stub file '$TARGET'"
echo '{"__negistry_about__":"Stub negistry file"}' > "$TARGET"
else
error "Download of Negistry whois database failed, file '$TARGET' does not exist"
fi
fi
# Check, that the download was successfull (target file must not be older than 300s)
if [ "$(( $(date +"%s") - $(date -r $TARGET +%s) ))" -gt "300" ]
#
# Check, that the download was successfull (target file must not be older than 60 s).
#
if [ "$(( $(date +"%s") - $(date -r $TARGET +%s) ))" -gt "60" ]
then
die "Download of whois dump file failed, file is too old to be downloaded recently"
error "Download of Negistry whois database failed, file '$TARGET' is too old to be downloaded recently (last update at `date -r $TARGET`)"
fi
exit 0
success "Successfully downloaded Negistry whois database into file '$TARGET' at `date`"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment