Last active
December 16, 2015 04:19
-
-
Save dermoth/1f27d9731cd4bb57aefd to your computer and use it in GitHub Desktop.
CGI script in plain Bash...
This file contains hidden or 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 | |
# Send all command output to STDERR while allowing us to write to STDOUT | |
# using fd 3 | |
exec 3>&1 1>&2 | |
set -eu | |
trap "echo -e 'Status: 500 Internal Server Error\n' >&3; exit 0" ERR | |
# Only accept HEAD & GET | |
[ "$REQUEST_METHOD" == "HEAD" -o "$REQUEST_METHOD" == "GET" ] | |
# Update the config... | |
cd /path/to/repo | |
git pull >/dev/null | |
# Then print a full list of A/AAAA/CNAME records | |
out=$(find var/named/config/namedb -type f -name 'db.external.*' |xargs grep -HE '^[^[:space:];]+.*[[:space:]]+(A|AAAA|CNAME)[[:space:]]+.*$' |perl -pe 's#^var/named/config/namedb/db.external.(.*).:(\S*)\s+(?:\w+\s+)+(\S*)\s*$#sprintf("%s%s\n", ($2 ne "@" ? $2 . ".": ""), $1)#e;'|grep -Ev '^;') | |
# Check the list isn't empty | |
[ -n "$out" ] | |
# Send back results | |
echo -e 'Status: 200 OK\nContent-type: text/plain\n' >&3 | |
[ "$REQUEST_METHOD" == "HEAD" ] && exit 0 | |
for domain in $out | |
do | |
echo $domain >&3 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment