Created
November 29, 2011 01:03
-
-
Save justincjahn/1402861 to your computer and use it in GitHub Desktop.
Simple script that edits a bind zone file, updates the SOA line, commits to etckeeper, and restarts BIND.
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
#!/usr/bin/env bash | |
# The file we will be editing | |
FILE=/etc/bind/domain.tld | |
DOMAIN=domain.tld | |
# A path to the BIND init script | |
BIND=/etc/init.d/bind9 | |
ETCKEEP=/usr/sbin/etckeeper | |
SUDO=/usr/bin/sudo | |
# | |
# Command Line Arguments | |
# | |
if [ "$2" != "" ]; then | |
DOMAIN=$2 | |
fi | |
if [ "$1" != "" ]; then | |
FILE=$1 | |
fi | |
# | |
# Run VIM in sudo to open the file. | |
# | |
echo "" | |
echo "Editing $FILE..." | |
$SUDO vim -c ":set tabstop=8" -c ":set shiftwidth=8" -c ":set noexpandtab" $FILE | |
# | |
# Check to make sure the syntax is correct before continuing. | |
# | |
echo -n "Syntax check: " | |
#named-checkzone $DOMAIN $FILE > /dev/null 2>&1 | |
ERROR=$(named-checkzone $DOMAIN $FILE 2>&1) | |
if [ $? -ne 0 ]; then | |
echo -e "\t\t[INVALID]\n\n" | |
echo $ERROR | |
exit 1 | |
else | |
echo -e "\t\t[VALID]" | |
fi | |
# | |
# Continue to automatic functionality. | |
# | |
read -p "Ready to commit? (y/n): " -e continue | |
if [ "$continue" != "y" ]; then | |
echo "" | |
echo "Changes will not be automatically committed, exiting." | |
exit | |
fi | |
# | |
# Automatically update the SOA | |
# | |
echo -n "Updating SOA: " | |
$SUDO perl -i.bak -pe 'BEGIN {chomp ($now=qx/date +%Y%m%d/)}; | |
/(\d{8})(\d{2})(\s+;\s+serial)/i and do { | |
$serial = ($1 eq $now ? $2+1 : 0); | |
s/\d{8}(\d{2})/sprintf "%8d%02d",$now,$serial/e; | |
}' $FILE | |
# Sanity check | |
named-checkzone $DOMAIN $FILE > /dev/null 2>&1 | |
if [ $? -ne 0 ]; then | |
echo -e "\t\t[FAILED]" | |
echo "Sanity check failed, reverting to old SOA." | |
$SUDO mv $FILE.bak $FILE | |
exit 1 | |
else | |
$SUDO rm -f $FILE.bak | |
echo -e "\t\t[OK]" | |
fi | |
# | |
# Run etckeeper | |
# | |
echo "" | |
read -p "Commit Message: " -e commit_msg | |
echo -n "Commit Changes:" | |
if [ -n "$commit_msg" ]; then | |
$SUDO $ETCKEEP commit "\"$commit_msg\"" > /dev/null | |
if [ $? -ne 0 ]; then | |
exit 1 | |
else | |
echo -e "\t\t[OK]" | |
fi | |
else | |
echo -e "\t\t[FAILED]" | |
echo "Empty commit message, exiting..." | |
exit 1 | |
fi | |
# | |
# Restart BIND | |
# | |
echo "" | |
echo -n "Restarting BIND:" | |
$SUDO $BIND restart > /dev/null | |
if [ $? -ne 0 ]; then | |
echo -e "\t\t[FAILED]" | |
exit 1 | |
else | |
echo -e "\t\t[OK]" | |
fi | |
echo -e "!!!\nBe sure to edit the reverse DNS files!\n!!!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment