From 8f78b7350a63cc0fe2a98318fa47f8a62195fee0 Mon Sep 17 00:00:00 2001 From: Christoph Date: Thu, 22 Nov 2018 03:01:58 +0100 Subject: [PATCH] Add script 'clean_up_arp_table.sh'. --- clean_up_arp_table.sh | 220 ++++++++++++++++++++++++++++ conf/clean_up_arp_table.conf.sample | 15 ++ 2 files changed, 235 insertions(+) create mode 100755 clean_up_arp_table.sh create mode 100644 conf/clean_up_arp_table.conf.sample diff --git a/clean_up_arp_table.sh b/clean_up_arp_table.sh new file mode 100755 index 0000000..d0dc7ad --- /dev/null +++ b/clean_up_arp_table.sh @@ -0,0 +1,220 @@ +#!/usr/bin/env bash + +script_name="$(basename $(realpath $0))" +working_dir="$(dirname $(realpath $0))" + +conf_file="${working_dir}/conf/${script_name%%.*}.conf" +LOCK_DIR="$(mktemp -d)" + +log_file="${LOCK_DIR}/script_name.log" + +# ---------- +# Base Function(s) +# ---------- +clean_up() { + + # Perform program exit housekeeping + rm -rf "$LOCK_DIR" + blank_line + exit $1 +} + + +echononl(){ + if $terminal ; then + 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$$ + fi +} + + +fatal(){ + echo "" + if $terminal ; then + echo -e " [ \033[31m\033[1mFatal\033[m ] $*" + else + echo -e " [ Fatal ] $*" + fi + echo "" + if $terminal ; then + echo -e " \033[1mScript terminated\033[m.." + else + echo -e " Script terminated.." + fi + echo "" + rm -rf $LOCK_DIR + exit 1 +} + +error (){ + echo "" + if $terminal ; then + echo -e " [ \033[31m\033[1mError\033[m ] $*" + else + echo " [ Error ] $*" + fi + echo "" +} + +warn (){ + if $LOGGING || $terminal ; then + echo "" + if $terminal ; then + echo -e " [ \033[33m\033[1mWarn\033[m ] $*" + else + echo " [ Warn ] $*" + fi + echo "" + fi +} + +info (){ + if $LOGGING || $terminal ; then + echo "" + if $terminal ; then + echo -e " [ \033[32m\033[1mInfo\033[m ] $*" + else + echo " [ Info ] $*" + fi + echo "" + fi +} + +ok (){ + if $LOGGING || $terminal ; then + echo "" + if $terminal ; then + echo -e " [ \033[32m\033[1mOk\033[m ] $*" + else + echo " [ Ok ] $*" + fi + echo "" + fi +} + +echo_done() { + if $terminal ; then + echo -e "\033[75G[ \033[32mdone\033[m ]" + fi +} +echo_ok() { + if $terminal ; then + echo -e "\033[75G[ \033[32mok\033[m ]" + fi +} +echo_failed(){ + if $terminal ; then + echo -e "\033[75G[ \033[1;31mfailed\033[m ]" + fi +} +echo_skipped() { + if $terminal ; then + echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]" + fi +} + +trim() { + local var="$*" + var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters + var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters + echo -n "$var" +} + +blank_line() { + if $terminal ; then + echo "" + fi +} + +is_number() { + + return $(test ! -z "${1##*[!0-9]*}" > /dev/null 2>&1); + + # - also possible + # - + #[[ ! -z "${1##*[!0-9]*}" ]] && return 0 || return 1 + #return $([[ ! -z "${1##*[!0-9]*}" ]]) +} + +## - Check if a given array (parameter 2) contains a given string (parameter 1) +## - +containsElement () { + local e + for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done + return 1 +} + + +# ---------- +# - Jobhandling +# ---------- + +# - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM +# - +trap clean_up SIGHUP SIGINT SIGTERM + + +# ---------- +# - Some checks .. +# ---------- + +# - Running in a terminal? +# - +if [[ -t 1 ]] ; then + terminal=true +else + terminal=false +fi + + +# ========== +# - Begin Main Script +# ========== + +# ---------- +# - Headline +# ---------- + +if $terminal ; then + echo "" + echo -e "\033[1m----------\033[m" + echo -e "\033[32m\033[1mRunning script \033[m\033[1m$script_name\033[32m .. \033[m" + echo -e "\033[1m----------\033[m" +fi + + +# ---------- +# Read Configurations from $conf_file +# ---------- + +if [[ -f "$conf_file" ]]; then + source "$conf_file" +fi + +if [[ -z "$DEVICE_LIST" ]] ; then + + # - Try to determine network device list + # - + DEVICE_LIST="$(/sbin/ip a | grep "^[0-9]\{1,2\}:" | awk '{print$2}' | cut -d ':' -f 1)" +fi + +declare -a device_arr=() +for _device in $DEVICE_LIST ; do + [[ "$_device" = "lo" ]] && continue + + (/sbin/ip link set arp off dev $_device > $log_file 2>&1) && /sbin/ip link set arp on dev $_device > $log_file 2>&1 + + if [[ $? = 0 ]] ; then + ok "Cleaning up arp table for device $_device" + else + error "Cleaning up arp table for device $_device failed! \n\n$(cat "$log_file") " + fi + +done + +clean_up 0 diff --git a/conf/clean_up_arp_table.conf.sample b/conf/clean_up_arp_table.conf.sample new file mode 100644 index 0000000..4f90074 --- /dev/null +++ b/conf/clean_up_arp_table.conf.sample @@ -0,0 +1,15 @@ +# ------------------------------------------- +# - Settings for script clean_up_arp_table.sh +# ------------------------------------------- + +# - DEVICE_LIST +# - +# - Blank separated list of network devices +# - +# - Example: +# - DEVICE_LIST="enp0s20f1" +# - +# - If DEVICE_LIST is not given, script tries to determin devive list. +# - +#DEVICE_LIST="" +