Created
November 19, 2017 20:07
-
-
Save anthonyclarka2/f66e90ee172c438197a930736f847539 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Scans the list of hosts and checks that | |
# DNS has a forward and reverse record for them | |
# Then compares the wiki host name with the PTR host name | |
# Generates a simple dokuwiki table | |
# runs in cron early every morning: | |
# check-dns-from-wiki.sh /data/wiki/data/pages/it/hostlist.txt > /data/wiki/data/pages/it/dnscheckresults.txt | |
# data is in a table, example: | |
# | [[:IT:hosts:test01.example.net]] | A test server, managed by XYZ corp | contact this person in case of emergency | | |
HOST_LIST=$1 | |
echo "====== DNS Check results ======" | |
echo | |
echo "^ Hostname ^ A Record ^ PTR Record ^ Records match? ^" | |
while IFS= read -r WIKI_LINE | |
do | |
# cut everything before the first ] | |
# then reverse the string | |
# cut everything *after* the first : | |
# then reverse the string (that's how you "cut" the last field in a string) | |
HOST_NAME=$(echo "${WIKI_LINE}" | cut -d']' -f 1 | rev | cut -d':' -f 1 | rev) | |
# default values | |
A_RES="BAD" | |
REV_RES="BAD" | |
A_AND_REV_RES="BAD" | |
ARESULT=$(dig "${HOST_NAME}" @192.168.151.4 +short) | |
if [ -n "${ARESULT}" ] | |
then | |
A_RES="OK" | |
else | |
A_RES="BAD" | |
fi | |
if [ "${A_RES}" = "OK" ] | |
then | |
REVRESULT=$(dig -x "${ARESULT}" @192.168.151.4 +short) | |
REV_RES="OK" | |
if [ -n "${REVRESULT}" ] | |
then | |
REVRESULT_STRIPPED="${REVRESULT::-1}" | |
if [ "${HOST_NAME}" = "${REVRESULT_STRIPPED}" ] | |
then | |
# echo "And A record matches PTR record!" | |
A_AND_REV_RES="OK" | |
else | |
# echo "ERROR, A and PTR don't match!" | |
A_AND_REV_RES="BAD" | |
fi | |
else | |
# echo "${ARESULT} result is NULL" | |
REV_RES="BAD" | |
fi | |
fi | |
echo "|${HOST_NAME} | ${A_RES} | ${REV_RES} | ${A_AND_REV_RES} |" | |
done < <(cat "${HOST_LIST}" | grep -E '^\| \[\[' | grep example) | |
echo | |
echo "----" | |
echo "Go back to [[IT::start|IT]]" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment