- Add Configuration file support for 'bind_get_domain_by_hostname.sh'.

This commit is contained in:
Christoph 2017-08-03 14:15:10 +02:00
parent 5a4eab9261
commit 3b0b759850

View File

@ -1,19 +1,52 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# - Bind configuration file containing zone definitions working_dir="$(dirname $(realpath $0))"
# - conf_file="${working_dir}/conf/bind.conf"
ZONE_CONF_FILE=/etc/bind/named.conf.local
## -- log_file="$(mktemp)"
## -- End: Variable definitions
## *** #---------------------------------------
## *** Don't make changes after this line *** #-----------------------------
## *** # Base Function(s)
#-----------------------------
#---------------------------------------
usage() {
echo
[ -n "$1" ] && echo -e "Error: $1\n"
cat<<EOF
Usage: $(basename $0) [Options] <full-qualified-hostname>
Script checks if a given hostname is supported by this nameserver. Hostname can
alos be a TLD name.
As result the Script returns the zone (domain) of the given hostname." In silent
mode (-q), script returns an empty String, if the given hostname is not
supported by this nameserver.
Options:
-h
Prints this help.
-q
Runs in silent mode.
EOF
clean_up 1
}
clean_up() {
# Perform program exit housekeeping
rm $log_file
exit $1
}
## --- some functions
## ---
echononl(){ echononl(){
if $verbose ; then
echo X\\c > /tmp/shprompt$$ echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo -e -n "$*\\c" 1>&2 echo -e -n "$*\\c" 1>&2
@ -21,41 +54,49 @@ echononl(){
echo -e -n "$*" 1>&2 echo -e -n "$*" 1>&2
fi fi
rm /tmp/shprompt$$ rm /tmp/shprompt$$
fi
} }
warn (){ warn (){
if $verbose ; then
echo "" echo ""
echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*" echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*"
echo "" echo ""
fi
} }
info (){ info (){
if $verbose ; then
echo "" echo ""
echo -e "\t[ \033[32m\033[1mInfo\033[m ]: $*" echo -e "\t[ \033[32m\033[1mInfo\033[m ]: $*"
echo "" echo ""
fi
} }
error(){ error(){
if $verbose ; then
echo "" echo ""
echo -e "\t[ \033[31m\033[1mFehler\033[m ]: $*" echo -e "\t[ \033[31m\033[1mFehler\033[m ]: $*"
echo "" echo ""
fi
} }
echo_ok() { echo_ok() {
if $verbose ; then
echo -e "\033[75G[ \033[32mok\033[m ]" echo -e "\033[75G[ \033[32mok\033[m ]"
fi
} }
echo_failed(){ echo_failed(){
if $verbose ; then
echo -e "\033[75G[ \033[1;31mfailed\033[m ]" echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
fi
} }
echo_skipped() { echo_skipped() {
if $verbose ; then
echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]" echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]"
fi
} }
## ---
## --- END: functions
hostname=$1
# - Test whether stdout (file descriptor 1) is a terminal or not (e.g. cron # - Test whether stdout (file descriptor 1) is a terminal or not (e.g. cron
# - or if you pipe the output to some other program) # - or if you pipe the output to some other program)
# #
@ -65,30 +106,109 @@ else
verbose=false verbose=false
fi fi
while getopts hq opt ; do
case $opt in
q) verbose=false
;;
h) usage
;;
*)
;;
esac
done
shift $(expr $OPTIND - 1)
if [[ $# -ne 1 ]] ; then
if $verbose ; then
usage "wrong number of arguments"
else
clean_up 10
fi
fi
hostname=$1
$verbose && echo "" $verbose && echo ""
if [[ -z "$hostname" ]] ; then
error "No hostname/domain given!"
clean_up 10
fi
#---------------------------------------
#-----------------------------
# Setting Defaults
#-----------------------------
#---------------------------------------
DEFAULT_CONF_FILE_DIR="/etc/bind"
#---------------------------------------
#-----------------------------
# Load default values from bind.conf
#
# Overwrites the settings above
#
#-----------------------------
#---------------------------------------
if $verbose ; then
clear
echo ""
echo -e "\033[32mRunning script \033[1m"$(basename $0)"\033[m .."
echo ""
fi
info "Given hostname: \033[1m${hostname}\033[m"
echononl "\t Loading default Configuration values from $(basename ${conf_file}).."
if [[ ! -f "$conf_file" ]]; then
echo_skipped
else
source "${conf_file}" > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fatal "$(cat $log_file)"
fi
fi
[[ -n "$CONF_FILE_DIR" ]] || CONF_FILE_DIR="$DEFAULT_CONF_FILE_DIR"
[[ -n "$ZONES_DECLARATION_FILE" ]] || ZONES_DECLARATION_FILE="${CONF_FILE_DIR}/named.conf.local"
$verbose && echo ""
# - Validate Syntax of given domain # - Validate Syntax of given domain
# - # -
valid_domain_regex="^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$" valid_domain_regex="^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$"
$verbose && echononl "\tValidate syntax of given hostname/domain.." echononl "\t Validate syntax of given hostname/domain.."
if [[ $hostname =~ $valid_domain_regex ]]; then if [[ $hostname =~ $valid_domain_regex ]]; then
if [[ ! $hostname =~ \. ]]; then if [[ ! $hostname =~ \. ]]; then
$verbose && echo_failed echo_failed
$verbose && error "Invalid hostname/domain given!" error "Invalid hostname/domain given!"
exit 10 clean_up 10
else else
$verbose && echo_ok echo_ok
fi fi
else else
$verbose && echo_failed echo_failed
$verbose && error "Invalid hostname/domain given!" error "Invalid hostname/domain given!"
exit 10 clean_up 10
fi fi
_failed=false _failed=false
_hostname=$(echo ${hostname//\./\\.}) _hostname=$(echo ${hostname//\./\\.})
while ! grep -e "$_hostname" $ZONE_CONF_FILE > /dev/null 2>&1 ; do while ! grep -E "^\s*zone\s+\"?$_hostname" $ZONES_DECLARATION_FILE > /dev/null 2>&1 ; do
hostname=${hostname#*.} hostname=${hostname#*.}
_hostname=$(echo ${hostname//\./\\.}) _hostname=$(echo ${hostname//\./\\.})
if [[ ! $_hostname =~ \. ]]; then if [[ ! $_hostname =~ \. ]]; then
@ -102,12 +222,12 @@ if $_failed ; then
else else
domain=$hostname domain=$hostname
if $verbose ; then if $verbose ; then
info "Domain: $domain" info "Hosted Zone: \033[1m${domain}\033[m"
else else
echo "$domain" echo "$domain"
fi fi
fi fi
$verbose && echo $verbose && echo
exit 0 clean_up 0