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

528 lines
13 KiB
Bash
Executable File

#!/usr/bin/env bash
## --- Variables (default Values)
## ---
_zone_file_dir=/etc/bind/master
#_zone_file_dir=/root/tmp/master
_serial_new=`date +%Y%m%d01`
_zone_file_suffix=zone
## ---
## --- End: Variables (default Values)
## --- 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$$
}
fatal(){
echo ""
echo -e "[ \033[31m\033[1mError\033[m ]: $*"
echo ""
echo -e "\t\033[31m\033[1mScript is canceled\033[m\033[m"
echo ""
exit 1
}
warn (){
echo ""
echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*"
echo ""
}
info (){
echo ""
echo -e "\t[ \033[33m\033[1mInfo\033[m ]: $*"
echo ""
}
ok (){
echo ""
echo -e "\t[ \033[36m\033[1mOk\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 ]"
}
containsElement () {
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
return 1
}
## - Test of valid IPv4 Address
## -
## - Returns 0 if valid, > 0 otherwise
## -
is_valid_ipv4() {
local -a octets=( ${1//\./ } )
local RETURNVALUE=0
# return an error if the IP doesn't have exactly 4 octets
[[ ${#octets[@]} -ne 4 ]] && return 1
for octet in ${octets[@]}
do
if [[ ${octet} =~ ^[0-9]{1,3}$ ]]
then # shift number by 8 bits, anything larger than 255 will be > 0
((RETURNVALUE += octet>>8 ))
else # octet wasn't numeric, return error
return 1
fi
done
return ${RETURNVALUE}
}
is_valid_ipv6() {
local _ipv6=$1
if [ "$1" != "${1#[0-9a-f]*:}" ] \
&& [ "$1" = "${1#*[^0-9a-f:]}" ] \
&& [ "${1#*[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]}" = "${1#*:*:*:*:*:*:*:*:*:}" ]; then
return 0
else
return 1
fi
}
## ---
## --- END: functions
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Insert IPv4-Address which do you want to change (old address)."
echo ""
echo -e "Type \"\033[33mNone\033[m\" if no IPv4 address should be changed."
echo ""
IPv4_ADDRESS_OLD=
_set_ipv4=true
while [ "X$IPv4_ADDRESS_OLD" = "X" ]; do
echononl "Old IPv4-Address: "
read IPv4_ADDRESS_OLD
## - To lower case
IPv4_ADDRESS_OLD=${IPv4_ADDRESS_OLD,,}
if [ "X$IPv4_ADDRESS_OLD" = "X" ]; then
echo -e "\n\t\033[33m\033[1mAn entry is required!\033[m\n"
IPv4_ADDRESS_OLD=""
continue
fi
if [ "$IPv4_ADDRESS_OLD" = "none" ];then
_set_ipv4=false
break
fi
ipv4_addresses_old_arr=()
for _ipv4_address in $IPv4_ADDRESS_OLD ; do
containsElement "$_ipv4_address" "${ipv4_addresses_old_arr[@]}" && continue
ipv4_addresses_old_arr+=("$_ipv4_address")
done
if [ ${#ipv4_addresses_old_arr[@]} -gt 1 ] ; then
echo -e "\n\t\033[33m\033[1mOnly one address is possible!\033[m\n"
IPv4_ADDRESS_OLD=""
continue
fi
unset ipv4_addresses_old_arr
if ! is_valid_ipv4 $IPv4_ADDRESS_OLD ; then
echo -e "\n\t\033[33m\033[1m$IPv4_ADDRESS_OLD\033[m is NOT a valid IPv4 Address\n"
IPv4_ADDRESS_OLD=""
continue
fi
done
if $_set_ipv4 ; then
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Insert IPv4-Address which do you want to replace (new address)."
echo ""
IPv4_ADDRESS_NEW=
_set_ipv4=true
while [ "X$IPv4_ADDRESS_NEW" = "X" ]; do
echononl "New IPv4-Address: "
read IPv4_ADDRESS_NEW
## - To lower case
IPv4_ADDRESS_NEW=${IPv4_ADDRESS_NEW,,}
if [ "X$IPv4_ADDRESS_NEW" = "X" ]; then
echo -e "\n\t\033[33m\033[1mAn entry is required!\033[m\n"
IPv4_ADDRESS_NEW=""
continue
fi
ipv4_addresses_new_arr=()
for _ipv4_address in $IPv4_ADDRESS_NEW ; do
containsElement "$_ipv4_address" "${ipv4_addresses_new_arr[@]}" && continue
ipv4_addresses_new_arr+=("$_ipv4_address")
done
if [ ${#ipv4_addresses_new_arr[@]} -gt 1 ] ; then
echo -e "\n\t\033[33m\033[1mOnly one address is possible!\033[m\n"
IPv4_ADDRESS_NEW=""
continue
fi
unset ipv4_addresses_new_arr
if ! is_valid_ipv4 $IPv4_ADDRESS_NEW ; then
echo -e "\n\t\033[33m\033[1m$IPv4_ADDRESS_NEW\033[m is NOT a valid IPv4 Address\n"
IPv4_ADDRESS_NEW=""
continue
fi
done
fi
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Insert IPv6-Address which do you want to change (old address)."
echo ""
echo -e "Type \"\033[33mNone\033[m\" if no IPv6 address should be changed."
echo ""
IPv6_ADDRESS_OLD=
_set_ipv6=true
while [ "X$IPv6_ADDRESS_OLD" = "X" ]; do
echononl "Old IPv6-Address: "
read IPv6_ADDRESS_OLD
## - To lower case
IPv6_ADDRESS_OLD=${IPv6_ADDRESS_OLD,,}
if [ "X$IPv6_ADDRESS_OLD" = "X" ]; then
echo -e "\n\t\033[33m\033[1mAn entry is required!\033[m\n"
IPv6_ADDRESS_OLD=""
continue
fi
if [ "$IPv6_ADDRESS_OLD" = "none" ];then
_set_ipv6=false
break
fi
ipv6_addresses_old_arr=()
for _ipv6_address in $IPv6_ADDRESS_OLD ; do
echo "$_ipv6_address"
containsElement "$_ipv6_address" "${ipv6_addresses_old_arr[@]}" && continue
ipv6_addresses_old_arr+=("$_ipv6_address")
done
if [ ${#ipv6_addresses_old_arr[@]} -gt 1 ] ; then
echo -e "\n\t\033[33m\033[1mOnly one address is possible!\033[m\n"
IPv6_ADDRESS_OLD=""
continue
fi
unset ipv6_addresses_old_arr
if ! is_valid_ipv6 $IPv6_ADDRESS_OLD ; then
echo -e "\n\t\033[33m\033[1m$IPv6_ADDRESS_OLD\033[m is NOT a valid IPv6 Address\n"
IPv6_ADDRESS_OLD=""
continue
fi
done
if $_set_ipv6 ; then
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Insert IPv6-Address which do you want to replace (new address)."
echo ""
IPv6_ADDRESS_NEW=
_set_ipv6=true
while [ "X$IPv6_ADDRESS_NEW" = "X" ]; do
echononl "New IPv6-Address: "
read IPv6_ADDRESS_NEW
## - To lower case
IPv6_ADDRESS_NEW=${IPv6_ADDRESS_NEW,,}
if [ "X$IPv6_ADDRESS_NEW" = "X" ]; then
echo -e "\n\t\033[33m\033[1mAn entry is required!\033[m\n"
IPv6_ADDRESS_NEW=""
continue
fi
ipv6_addresses_new_arr=()
for _ipv6_address in $IPv6_ADDRESS_NEW ; do
containsElement "$_ipv6_address" "${ipv6_addresses_new_arr[@]}" && continue
ipv6_addresses_new_arr+=("$_ipv6_address")
done
if [ ${#ipv6_addresses_new_arr[@]} -gt 1 ] ; then
echo -e "\n\t\033[33m\033[1mOnly one address is possible!\033[m\n"
IPv6_ADDRESS_NEW=""
continue
fi
unset ipv6_addresses_new_arr
if ! is_valid_ipv6 $IPv6_ADDRESS_NEW ; then
echo -e "\n\t\033[33m\033[1m$IPv6_ADDRESS_NEW\033[m is NOT a valid IPv6 Address\n"
IPv6_ADDRESS_NEW=""
continue
fi
done
fi
if ! $_set_ipv6 && ! $_set_ipv4 ; then
fatal "Neither IPv4-Adress nor IPv6-Adress given .."
fi
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Insert the directory, where your zone-files resides."
echo ""
echo ""
ZONE_FILE_DIR=
while [ "X$ZONE_FILE_DIR" = "X" ]; do
echononl "Zone File Directory [$_zone_file_dir]: "
read ZONE_FILE_DIR
if [ "X$ZONE_FILE_DIR" = "X" ]; then
ZONE_FILE_DIR=$_zone_file_dir
fi
if [ ! -d $ZONE_FILE_DIR ]; then
echo -e "\n\tDirectory \033[33m\033[1m$ZONE_FILE_DIR\033[m does NOT exist!\n"
ZONE_FILE_DIR=
fi
done
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Insert the file-suffix of thr zone-files"
echo ""
echo ""
ZONE_FILE_SUFFIX=
echononl "Suffix of Zone Files [$_zone_file_suffix]: "
read ZONE_FILE_SUFFIX
if [ "X$ZONE_FILE_SUFFIX" = "X" ]; then
ZONE_FILE_SUFFIX=$_zone_file_suffix
fi
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Ignore Hostnames containing \"-alt\" (as ww-alt.oopen.de or d-alt.mx.oopen.de)"
echo ""
echo ""
OK=
IGNORE_ALT_HOSTNAMES=false
while [ "$OK" != "yes" -o "$OK" != "no" ] ; do
echononl "Ignore Hostnames containing \"-alt\"? [yes/no]: "
read OK
## - To lower case
OK=${OK,,}
if [ "X$OK" = "X" ]; then
echo -e "\n\t\033[33m\033[1mAn entry is required!\033[m\n"
OK=""
continue
fi
if [ "$OK" = "yes" -o "$OK" = "no" ] ; then
break
else
OK=""
fi
echo -e "\n\t\033[33m\033[1mWrong entry!\033[m\n"
done
[[ $OK = "yes" ]] && IGNORE_ALT_HOSTNAMES=true
clear
echo ""
echo ""
echo -e "\033[21G\033[32mReplace IP-Address(es)\033[m"
echo ""
echo ""
if $_set_ipv4 ; then
echo "Old IPv4 Address..................: $IPv4_ADDRESS_OLD"
echo "New IPv4 Address..................: $IPv4_ADDRESS_NEW"
else
echo -e "IPv4 Address(es)..................: \033[33mNone\033[m"
fi
echo ""
if $_set_ipv6 ; then
echo "Old IPv6 Address..................: $IPv6_ADDRESS_OLD"
echo "New IPv6 Address..................: $IPv6_ADDRESS_NEW"
else
echo -e "IPv6 Address(es)..................: \033[33mNone\033[m"
fi
echo ""
echo "Ignore hostnames containing \"-alt\".: $IGNORE_ALT_HOSTNAMES"
echo ""
echo "Zone File Directory...............: $ZONE_FILE_DIR"
echo "Zone File Suffix..................: $ZONE_FILE_SUFFIX"
echo ""
OK=
while [ "$OK" != "yes" -o "$OK" != "no" ] ; do
echononl "Parameters ok? [yes/no]: "
read OK
## - To lower case
OK=${OK,,}
if [ "X$OK" = "X" ]; then
echo -e "\n\t\033[33m\033[1mAn entry is required!\033[m\n"
OK=""
continue
fi
if [ "$OK" != "yes" -o "$OK" != "no" ] ; then
break
fi
echo -e "\n\t\033[33m\033[1mWrong entry!\033[m\n"
done
[[ $OK = "yes" ]] || fatal Repeat execution with different parameters
echo ""
cp -a $ZONE_FILE_DIR ${ZONE_FILE_DIR}.BAK-`date +%Y-%m-%d-%H%M`
zonefiles_arr=()
if $_set_ipv4 ; then
_zone_files_ipv4=`grep -l -e "$IPv4_ADDRESS_OLD" ${ZONE_FILE_DIR}/*.$ZONE_FILE_SUFFIX`
if [ ${#zonefiles_arr[@]} -eq 0 ] ; then
for _zone_file in $_zone_files_ipv4 ; do
zonefiles_arr+=("$_zone_file")
done
else
for _zone_file in $_zone_files_ipv4 ; do
containsElement "$_zone_file" "${zonefiles_arr[@]}" && continue
zonefiles_arr+=("$_zone_file")
done
fi
fi
if $_set_ipv6 ; then
_zone_files_ipv6=`grep -l -e "$_ipv6_address" ${ZONE_FILE_DIR}/*.$ZONE_FILE_SUFFIX`
if [ ${#zonefiles_arr[@]} -eq 0 ] ; then
for _zone_file in $_zone_files_ipv6 ; do
zonefiles_arr+=("$_zone_file")
done
else
for _zone_file in $_zone_files_ipv6 ; do
containsElement "$_zone_file" "${zonefiles_arr[@]}" && continue
zonefiles_arr+=("$_zone_file")
done
fi
fi
for zone_file in ${zonefiles_arr[@]} ; do
echo -e "\tconverting $zone_file.."
## - calculate new serial
## -
declare -i __serial=`grep -e "[0-9]\{10\}" $zone_file | grep serial | awk '{print$1}'`
while [ ! $_serial_new -gt $__serial ]; do
let _serial_new++
done
## - setze neue ipv4 adresse
## -
echononl "\t Set new IPv4 address ($IPv4_ADDRESS_NEW).."
_replaced=false
if grep -e "IN\s*A\s*$IPv4_ADDRESS_OLD" $zone_file > /dev/null 2>&1 ; then
perl -i -n -p -e "s#IN\s+A\s+$IPv4_ADDRESS_OLD#IN A $IPv4_ADDRESS_NEW#" $zone_file
if [ "$?" = "0" ]; then
echo_ok
_replaced=true
else
echo_failed
error "Replacing $IPv4_ADDRESS_OLD with $IPv4_ADDRESS_NEW in zone file \"$zone_file\" failed!"
fi
else
echo_skipped
fi
if $_replaced && $IGNORE_ALT_HOSTNAMES ; then
echononl "\t Setting back hostnames containing \"-alt\".."
perl -i -n -p -e "s#^(.+(-alt).*)\s+IN\s+A\s+$IPv4_ADDRESS_NEW#\1 IN A $IPv4_ADDRESS_OLD#" $zone_file > /dev/null 2>&1
if [ "$?" = "0" ]; then
echo_ok
else
echo_failed
error "Setting back hostnames containing \"-alt\" to $IPv4_ADDRESS_OLD in zone file \"$zone_file\" failed!"
fi
fi
## - setze neue ipv6 adresse
## -
echononl "\t Set new IPv6 address ($IPv6_ADDRESS_NEW).."
_replaced_ipv6=false
if grep -e "IN\s*AAAA\s*$IPv6_ADDRESS_OLD" $zone_file > /dev/null 2>&1 ; then
perl -i -n -p -e "s#IN\s+AAAA\s+$IPv6_ADDRESS_OLD#IN AAAA $IPv6_ADDRESS_NEW#" $zone_file
if [ "$?" = "0" ]; then
echo_ok
_replaced=true
_replaced_ipv6=true
else
echo_failed
error "Replacing $IPv6_ADDRESS_OLD with $IPv6_ADDRESS_NEW in zone file \"$zone_file\" failed!"
fi
else
echo_skipped
fi
if $_replaced_ipv6 && $IGNORE_ALT_HOSTNAMES ; then
echononl "\t Setting back hostnames containing \"-alt\".."
perl -i -n -p -e "s#^(.+(-alt).*)\s+IN\s+A\s+$IPv6_ADDRESS_NEW#\1 IN A $IPv6_ADDRESS_OLD#" $zone_file > /dev/null 2>&1
if [ "$?" = "0" ]; then
echo_ok
else
echo_failed
error "Setting back hostnames containing \"-alt\" to $IPv6_ADDRESS_OLD in zone file \"$zone_file\" failed!"
fi
fi
## - setze neue serial
## -
echo ""
echononl "\t Increase Serial for zone file \"`basename $zone_file`\""
if $_replaced ; then
perl -i -n -p -e "s#^(\s*)\s$__serial(.*)#\1 $_serial_new\2#" $zone_file > /dev/null 2>&1
if [ "$?" = "0" ]; then
echo_ok
_replaced=true
else
echo_failed
error "Increasing Serial for zone file \"`basename $zone_file`\" failed!"
fi
else
echo_skipped
fi
echo
done
echo ""
exit