bind/OLD_Versions/bind_get_domain_by_hostname.sh
2017-08-04 00:15:29 +02:00

114 lines
2.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# - Bind configuration file containing zone definitions
# -
ZONE_CONF_FILE=/etc/bind/named.conf.local
## --
## -- End: Variable definitions
## ***
## *** Don't make changes after this line ***
## ***
## --- some functions
## ---
echononl(){
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo -e -n "$*\\c" 1>&2
else
echo -e -n "$*" 1>&2
fi
rm /tmp/shprompt$$
}
warn (){
echo ""
echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*"
echo ""
}
info (){
echo ""
echo -e "\t[ \033[32m\033[1mInfo\033[m ]: $*"
echo ""
}
error(){
echo ""
echo -e "\t[ \033[31m\033[1mFehler\033[m ]: $*"
echo ""
}
echo_ok() {
echo -e "\033[75G[ \033[32mok\033[m ]"
}
echo_failed(){
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
}
echo_skipped() {
echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]"
}
## ---
## --- END: functions
hostname=$1
# - Test whether stdout (file descriptor 1) is a terminal or not (e.g. cron
# - or if you pipe the output to some other program)
#
if [[ -t 1 ]] ; then
verbose=true
else
verbose=false
fi
$verbose && echo ""
# - 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]))*$"
$verbose && echononl "\tValidate syntax of given hostname/domain.."
if [[ $hostname =~ $valid_domain_regex ]]; then
if [[ ! $hostname =~ \. ]]; then
$verbose && echo_failed
$verbose && error "Invalid hostname/domain given!"
exit 10
else
$verbose && echo_ok
fi
else
$verbose && echo_failed
$verbose && error "Invalid hostname/domain given!"
exit 10
fi
_failed=false
_hostname=$(echo ${hostname//\./\\.})
while ! grep -e "$_hostname" $ZONE_CONF_FILE > /dev/null 2>&1 ; do
hostname=${hostname#*.}
_hostname=$(echo ${hostname//\./\\.})
if [[ ! $_hostname =~ \. ]]; then
_failed=true
break
fi
done
if $_failed ; then
$verbose && error "hostname \"$1\" not supported by this nameserver!"
else
domain=$hostname
if $verbose ; then
info "Domain: $domain"
else
echo "$domain"
fi
fi
$verbose && echo
exit 0