diff options
author | B Stack <bgstack15@gmail.com> | 2017-05-29 10:40:19 -0400 |
---|---|---|
committer | B Stack <bgstack15@gmail.com> | 2017-05-29 10:40:19 -0400 |
commit | b506110f1c011d7ae04897f769710761faa0c359 (patch) | |
tree | 23e4e99e0f458cec4fb254fe4350bbd323d99b9c /usr | |
parent | cleaned up a few sections of dhcpd-control (diff) | |
download | ddtools-b506110f1c011d7ae04897f769710761faa0c359.tar.gz ddtools-b506110f1c011d7ae04897f769710761faa0c359.tar.bz2 ddtools-b506110f1c011d7ae04897f769710761faa0c359.zip |
added remove-mac feature
Diffstat (limited to 'usr')
-rwxr-xr-x | usr/share/ddtools/dhcpd-control.sh | 44 |
1 files changed, 41 insertions, 3 deletions
diff --git a/usr/share/ddtools/dhcpd-control.sh b/usr/share/ddtools/dhcpd-control.sh index 1c81a2d..ace5e04 100755 --- a/usr/share/ddtools/dhcpd-control.sh +++ b/usr/share/ddtools/dhcpd-control.sh @@ -10,6 +10,7 @@ # Usage: # Reference: ftemplate.sh 2017-05-24a; framework.sh 2017-05-24a # order of dhcpd servers to restart https://kb.isc.org/article/AA-01043/0/Recommendations-for-restarting-a-DHCP-failover-pair.html +# merge lines with sed http://www.linuxquestions.org/questions/programming-9/merge-lines-in-a-file-using-sed-191121/ # Improve: # provide mechanisms for non-systemd service control # Dependencies: @@ -44,7 +45,9 @@ ENDUSAGE # DEFINE TRAPS clean_dhcpdcontrol() { - rm -f ${tmp_dhcpd_combined_file} ${tmp_dhcpd_local_file} ${tmp_dhcpd_other_file} > /dev/null 2>&1 + { + rm -f ${tmp_dhcpd_combined_file} ${tmp_dhcpd_local_file} ${tmp_dhcpd_other_file} ${tmp_mac_local_file} ${tmp_macless_local_file} + } 1>/dev/null 2>&1 #use at end of entire script if you need to clean up tmpfiles } @@ -73,7 +76,7 @@ parseFlag() { "edit-local" ) action="edit-local";; "edit-other" ) action="edit-other";; "edit" ) action="edit";; - "remove-mac" ) getval; DHCPD_CONTROL_MAC_TO_REMOVE="${tempval}";; + "remove-mac" ) getval; DHCPD_CONTROL_MAC_TO_REMOVE="${tempval}"; action="remove-mac";; esac debuglev 10 && { test ${hasval} -eq 1 && ferror "flag: ${flag} = ${tempval}" || ferror "flag: ${flag}"; } @@ -248,10 +251,11 @@ trap "clean_dhcpdcontrol" 0 update_conf_other=0 update_leases_other=0 restart_service_other=0 - # WORKHERE: add local service, other_server_service + debuglev 8 && ferror "BEGIN action ${action}" case "${action}" in "flush") + debuglev 8 && ferror "BEGIN flush" # Clear temorary leases file debuglev 4 && ferror "Flushing all leases" if test -z "${DHCPD_CONTROL_LEASES_TEMP_FILE}"; @@ -298,6 +302,7 @@ trap "clean_dhcpdcontrol" 0 ;; "edit") + debuglev 8 && ferror "BEGIN edit" # prepare temp file tmp_dhcpd_combined_file="$( mktemp -p /tmp dhcpd.combined.XXXXX )" cp -p "${DHCPD_CONTROL_COMBINED_FILE}" "${tmp_dhcpd_combined_file}" @@ -316,6 +321,7 @@ trap "clean_dhcpdcontrol" 0 ;; "edit-local") + debuglev 8 && ferror "BEGIN edit-local" # prepare temp file tmp_dhcpd_local_file="$( mktemp -p /tmp dhcpd.XXXXX )" cp -p "${DHCPD_CONTROL_DHCPD_FILE}" "${tmp_dhcpd_local_file}" @@ -331,6 +337,7 @@ trap "clean_dhcpdcontrol" 0 ;; "edit-other") + debuglev 8 && ferror "BEGIN edit-other" tmp_dhcpd_other_file="$( mktemp -p /tmp dhcpd.other.XXXXX )" scp -p "${DHCPD_CONTROL_OTHER_SERVER}:${DHCPD_CONTROL_DHCPD_FILE}" "${tmp_dhcpd_other_file}" cp -p "${tmp_dhcpd_other_file}" "${tmp_dhcpd_other_file}8" #arbitrary number # edit file @@ -344,9 +351,39 @@ trap "clean_dhcpdcontrol" 0 fi ;; + "remove-mac") + debuglev 8 && ferror "BEGIN remove-mac" + # working on this + # WORKHERE: verify that doing it on local is sufficient. + # sed -n -r -e '/^lease.*\{/,/^\}/{/^lease|hardware|\}/{p}}' /tmp/foo1 | sed -e ':a;/\}/!{N;s/\n/ /;ba};' # base form + # sed -n -r -e '/^lease.*\{/,/^\}/{p}' /tmp/foo1 | sed -e ':a;/\}/!{N;s/\n/ /;ba};' -e 's/\s\+/ /g;' # slightly trimmed + # sed -n -r -e '/\{/,/^\}/{p}' /tmp/foo1 | sed -e ':a;/\}/!{N;s/\n/ /;ba};' -e 's/\s\+/ /g;' | grep -iE "ec:9a:74:48:bc:c4" # find the one mac address + tmp_mac_local_file="$( mktemp -p /tmp leases.mac.XXXXX )" + tmp_macless_local_file="$( mktemp -p /tmp leases.macless.XXXXX )" + if test -z "${DHCPD_CONTROL_MAC_TO_REMOVE}"; + then + ferror "${scripttrim}: 2. No MAC address provided. aborted." + exit 2 + fi + sed -n -r -e '/\{/,/^\}/{p}' "${DHCPD_CONTROL_LEASES_FILE}" | sed -e ':a;/\}/!{N;s/\n/ /;ba};' -e 's/\s\+/ /g;' | grep -iE "${DHCPD_CONTROL_MAC_TO_REMOVE}" > "${tmp_mac_local_file}" + if test -n "$( cat "${tmp_mac_local_file}" )"; + then + ferror "Removing leases:" + cat "${tmp_mac_local_file}" 1>&2 + fi + sed -n -r -e '/\{/,/^\}/{p}' "${DHCPD_CONTROL_LEASES_FILE}" | sed -e ':a;/\}/!{N;s/\n/ /;ba};' -e 's/\s\+/ /g;' | grep -viE "${DHCPD_CONTROL_MAC_TO_REMOVE}" > "${tmp_macless_local_file}" + if ! cmp -s "${tmp_macless_local_file}" "${DHCPD_CONTROL_LEASES_FILE}" + then + systemctl stop "${DHCPD_CONTROL_SERVICE}" + cp -p "${tmp_macless_local_file}" "${DHCPD_CONTROL_LEASES_FILE}" + restart_service_local=1 + fi + ;; + esac # Prepare instructions for other server + debuglev 8 && ferror "BEGIN prepare instructions for other server" local_instructions="" other_instructions="" instructions="" @@ -360,6 +397,7 @@ trap "clean_dhcpdcontrol" 0 other_instructions="${other_instructions}systemctl restart ${DHCPD_CONTROL_SERVICE}" # Instruct other server to act + debuglev 8 && ferror "BEGIN instruct other server to act" if test -n "${DHCPD_CONTROL_OTHER_SERVER}"; then debuglev 1 && { |