From 4b2c42ec99f9e3e72e6c5e18b58e2778240d87a0 Mon Sep 17 00:00:00 2001 From: Christoph Date: Tue, 21 Feb 2017 02:28:05 +0100 Subject: [PATCH] Initial import --- dhcp_sync_from_primary.sh | 150 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100755 dhcp_sync_from_primary.sh diff --git a/dhcp_sync_from_primary.sh b/dhcp_sync_from_primary.sh new file mode 100755 index 0000000..0ce2877 --- /dev/null +++ b/dhcp_sync_from_primary.sh @@ -0,0 +1,150 @@ +#!/usr/bin/env bash + +# --- +# - Script checks, if host declarations at (this) dhcp failover server +# - are in sync with primary dhcp server. If needed, dhcp failover server +# - will be updated. +# --- + +src_host=gw-ah.kanzlei-kiel.netz + +src_file_remote=/etc/dhcp/hosts.lan.conf +target_file_local=/etc/dhcp/hosts.lan.conf + +# ------------- +# --- 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 "fatal error: $*" + echo "" + echo -e "\t\033[31m\033[1mInstalllation will be interrupted\033[m\033[m" + echo "" + exit 1 +} + +error(){ + echo "" + echo -e "\t[ \033[31m\033[1mFehler\033[m ]: $*" + echo "" +} + +warn (){ + echo "" + echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*" + echo "" +} + +info (){ + echo "" + echo -e "\t[ \033[32m\033[1mInfo\033[m ]: $*" + echo "" +} + +echo_done() { + echo -e "\033[80G[ \033[32mdone\033[m ]" +} +echo_ok() { + echo -e "\033[80G[ \033[32mok\033[m ]" +} +echo_warning() { + echo -e "\033[80G[ \033[33m\033[1mwarn\033[m ]" +} +echo_failed(){ + echo -e "\033[80G[ \033[1;31mfailed\033[m ]" +} +echo_skipped() { + echo -e "\033[80G[ \033[33m\033[1mskipped\033[m ]" +} + + +# - Is this script running on terminal ? +# - +if [[ -t 1 ]] ; then + terminal=true +else + terminal=false +fi + + +# - Is this a systemd system? +# - +if [[ "X`which systemd`" = "X" ]]; then + systemd_exists=false +else + systemd_exists=true +fi + +tmp_include_file="/tmp/$(basename $src_file_remote).$$" +tmp_err_msg=$(mktemp) + +rsync -a -q -e ssh ${src_host}:${src_file_remote} $tmp_include_file > $tmp_err_msg 2>&1 +if [[ $? -ne 0 ]]; then + if $terminal ; then + error "$(cat $tmp_err_msg)" + else + echo -e "\n[ Error ]: $(cat $tmp_err_msg)" + fi + + rm -f $tmp_include_file + rm -f $tmp_err_msg + exit 1 +fi + +diff $tmp_include_file $target_file_local > $tmp_err_msg 2>&1 +if [[ $? -ne 0 ]]; then + cp -a $tmp_include_file $target_file_local + if $systemd_exists ; then + systemctl restart isc-dhcp-server > $tmp_err_msg 2>&1 + if [[ $? -ne 0 ]]; then + if $terminal ; then + error "$(cat $tmp_err_msg)" + else + echo -e "\n[ Error ]: $(cat $tmp_err_msg)" + fi + + rm -f $tmp_include_file + rm -f $tmp_err_msg + exit 1 + else + if $terminal ; then + echo -e "\n\tUpdate was successfully done.\n" + fi + fi + else + /etc/init.d/isc-dhcp-server restart > $tmp_err_msg 2>&1 + if [[ $? -ne 0 ]]; then + if $terminal ; then + error "$(cat $tmp_err_msg)" + else + echo -e "\n[ Error ]: $(cat $tmp_err_msg)" + fi + + rm -f $tmp_include_file + rm -f $tmp_err_msg + exit 1 + else + if $terminal ; then + echo -e "\n\tUpdate was successfully done.\n" + fi + fi + fi +else + if $terminal ; then + echo -e "\n\tServer is already up-to-date.\n" + fi +fi + +rm -f $tmp_include_file +rm -f $tmp_err_msg +exit 0