]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Adding bin/get-pdns-info
authorFrank Brehm <frank.brehm@pixelpark.com>
Mon, 6 Nov 2017 15:12:37 +0000 (16:12 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Mon, 6 Nov 2017 15:12:37 +0000 (16:12 +0100)
bin/get-pdns-info [new file with mode: 0755]

diff --git a/bin/get-pdns-info b/bin/get-pdns-info
new file mode 100755 (executable)
index 0000000..7dbf113
--- /dev/null
@@ -0,0 +1,275 @@
+#!/bin/bash
+
+set -e
+set -u
+
+VERBOSE="n"
+DEBUG="n"
+
+VERSION="1.1"
+
+# console colors:
+RED=""
+YELLOW=""
+GREEN=""
+BLUE=""
+NORMAL=""
+
+HAS_TTY='y'
+
+DO_ASK="n"
+SIMULATE="n"
+NO_REMOVE="n"
+
+BASENAME="$(basename ${0})"
+BASE_DIR="$(dirname ${0})"
+
+INPUT_TIMEOUT=5
+
+API_KEY_GLOBAL="6d1b08e2-59c6-49e7-9e48-039ade102016"
+API_KEY_LOCAL="d94b183a-c50d-47f7-b338-496090af1577"
+API_KEY_PUBLIC="cf0fb928-2a73-49ec-86c2-36e85c9672ff"
+API_KEY="${API_KEY_GLOBAL}"
+
+PDNS_GLOBAL="systemshare.pixelpark.com"
+PDNS_LOCAL="dnsmaster-local.pixelpark.com"
+PDNS_PUBLIC="dnsmaster-public.pixelpark.com"
+PDNS_SERVER="${PDNS_GLOBAL}"
+
+API_PORT="8081"
+
+TITLE_GLOBAL="All total available data"
+TITLE_LOCAL="All local available data"
+TITLE_PUBLIC="All public available data"
+TITLE="${TITLE_GLOBAL}"
+
+declare -a ZONES=()
+
+DATE=$( date '+%G%m%d' )
+
+#-------------------------------------------------------------------
+detect_color() {
+
+    local safe_term="${TERM//[^[:alnum:]]/?}"
+    local match_lhs=""
+    local use_color="false"
+    [[ -f ~/.dir_colors   ]] && match_lhs="${match_lhs}$(<~/.dir_colors)"
+    [[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(</etc/DIR_COLORS)"
+    [[ -z ${match_lhs}    ]] \
+        && type -P dircolors >/dev/null \
+        && match_lhs=$(dircolors --print-database)
+    [[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color="true"
+
+    # console colors:
+    if [ "${use_color}" = "true" ] ; then
+        RED="\033[38;5;196m"
+        YELLOW="\033[38;5;226m"
+        GREEN="\033[38;5;46m"
+        BLUE="\033[38;5;27m"
+        NORMAL="\033[39m"
+    else
+        RED=""
+        YELLOW=""
+        GREEN=""
+        BLUE=""
+        NORMAL=""
+    fi
+
+    local my_tty=$(tty)
+    if [[ "${my_tty}" =~ 'not a tty' ]] ; then
+        my_tty='-'
+    fi
+
+    if [[ "${my_tty}" = '-' || "${safe_term}" = "dump" ]] ; then
+        HAS_TTY='n'
+    fi
+
+}
+detect_color
+
+#------------------------------------------------------------------------------
+description() {
+    echo -e $( cat <<-EOF
+               Displays information about available DNS-Zones and -Records.
+
+               If called without a zone name a list of all available zones is printed,
+               otherwise all records of a given zone are displayed.
+
+               EOF
+    )
+}
+
+#------------------------------------------------------------------------------
+usage() {
+    cat <<-EOF
+       Usage: ${BASENAME} [-P|-L] [-d] [-v] [--nocolor] [ZONE [ZONE ...]]
+              ${BASENAME} [-h|--help]
+              ${BASENAME} [-V|--version]
+
+           Options:
+               -P|--public     Show all public available zones or records.
+               -L|--local      Show all local available zones or records.
+               -d|--debug      Debug output (bash -x).
+               -v|--verbose    Set verbosity on.
+               --nocolor       Don't use colors on display.
+               -h|--help       Show this output and exit.
+               -V|--version    prints out version number of the script and exit
+       EOF
+}
+
+#------------------------------------------------------------------------------
+get_options() {
+
+    local tmp=
+    local base_dir=
+
+    set +e
+    tmp=$( getopt -o PLdvhV \
+                    --long public,local,debug,verbose,nocolor,help,version \
+                    -n "${BASENAME}" -- "$@" )
+    if [[ $? != 0 ]] ; then
+        echo "" >&2
+        usage >&2
+        exit 1
+    fi
+    set -e
+
+    # Note the quotes around `$TEMP': they are essential!
+    eval set -- "${tmp}"
+
+    local p=
+
+    while true ; do
+        case "$1" in
+            -P|--public)
+                API_KEY="${API_KEY_PUBLIC}"
+                PDNS_SERVER="${PDNS_PUBLIC}"
+                TITLE="${TITLE_PUBLIC}"
+                shift
+                ;;
+            -L|--local)
+                API_KEY="${API_KEY_LOCAL}"
+                PDNS_SERVER="${PDNS_LOCAL}"
+                TITLE="${TITLE_LOCAL}"
+                shift
+                ;;
+            -d|--debug)
+                DEBUG="y"
+                shift
+                ;;
+            -v|--verbose)
+                VERBOSE="y"
+                shift
+                ;;
+            --nocolor)
+                RED=""
+                YELLOW=""
+                GREEN=""
+                BLUE=""
+                NORMAL=""
+                shift
+                ;;
+            -h|--help)
+                description
+                usage
+                exit 0
+                ;;
+            -V|--version)
+                echo "${BASENAME} version: ${VERSION}"
+                exit 0
+                ;;
+            --) shift
+                break
+                ;;
+            *)  echo "Internal error!"
+                exit 1
+                ;;
+        esac
+    done
+
+    if [[ "${DEBUG}" = "y" ]] ; then
+        set -x
+    fi
+
+    if [[ "$#" -ge 1 ]] ; then
+        local zone
+        for zone in "$@" ; do
+            ZONES+=("${zone}")
+        done
+    fi
+
+}
+
+#########################################
+# Some often used funktions
+
+#------------------------------------------------------------------------------
+my_date() {
+    date +'%F %T.%N %:::z'
+}
+
+#------------------------------------------------------------------------------
+debug() {
+    if [[ "${VERBOSE}" != "y" ]] ; then
+        return 0
+    fi
+    echo -e " * [$(my_date)] [${BASENAME}:DEBUG]: $@" >&2
+}
+
+#------------------------------------------------------------------------------
+info() {
+    echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASENAME}:${GREEN}INFO${NORMAL}] : $@" >&2
+}
+
+#------------------------------------------------------------------------------
+warn() {
+    echo -e " ${YELLOW}*${NORMAL} [$(my_date)] [${BASENAME}:${YELLOW}WARN${NORMAL}] : $@" >&2
+}
+
+#------------------------------------------------------------------------------
+error() {
+    echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASENAME}:${RED}ERROR${NORMAL}]: $@" >&2
+}
+
+#------------------------------------------------------------------------------
+show_zones() {
+
+    echo "${TITLE} - all zones"
+
+    local url="http://${PDNS_SERVER}:${API_PORT}/api/v1/servers/localhost/zones"
+    debug "Calling URL '${url}' ..."
+    #curl -H "X-API-Key: changeme" "${url}" | jq .
+    curl -H "X-API-Key: changeme" "${url}"
+
+}
+
+
+################################################################################
+##
+## Main
+##
+################################################################################
+
+#------------------------------------------------------------------------------
+main() {
+
+    get_options "$@"
+
+    if [[ "${#ZONES[*]}" != "0" ]] ; then
+        show_records
+    else
+        show_zones
+    fi
+
+    echo
+    info "Finished."
+
+}
+
+main "$@"
+
+exit 0
+
+
+
+# vim: ts=4 list