--- badi/public_scripts/switchgate/switchgate 2010/01/09 04:25:42 1.5 +++ badi/public_scripts/switchgate/switchgate 2017/03/28 21:44:24 1.8 @@ -1,7 +1,7 @@ #!/bin/bash -# switchhgate 0.0.3 -# (c) 2005 under GPL by Adrian Zaugg +# switchhgate 0.0.5 +# (c) 2005-2010 under GPL by Adrian Zaugg # switchgate pings a set of hosts to determine the state of the internet connectivity. If # the connection is down, it can change the default gateway to an alternative gateway. @@ -36,7 +36,7 @@ ALIVE_ANSWER="is alive" # set to an empty string to avoid debug output # to "low" for a few output and to anything else # for verbose output -DEBUG=low +DEBUG= # -------- Do not edit below this line -------- @@ -47,61 +47,6 @@ PINGARGS="-A -p25 -t100" ## Function declarations # -# parse commandline switches -function parse_commandline { - - # check rest - while [ "$#" -gt 0 ]; do - - case "$1" in - - -d|--debug) - # debug switch (another hidden feature) - shift - if [ "$1" != "low" ]; then - DEBUG="choke" - echo "Debug mode on." - else - #echo "Info mode on." - DEBUG="low" - shift - fi - ;; - - -C) - # Parse writeable dir request (hidden feature) - echo "$DHCP_TMP_DIR" - exit 0 - ;; - - -q|--query) - # Parse query for current gw (yet another hidden feature) - shift - parse_commandline "$@" - getcurrentgw - echo "$CURRENTGW" - exit 0 - ;; - - -s|--set) - # Set gateway to number n (yet another hidden feature) - shift - set_to_gw="$1" - shift - parse_commandline "$@" - set_gw - exit 0 - ;; - - *) - echo "Invalid argument \"$1\"." - exit 1 - ;; - - esac - done -} - # Read gateways from file function readgwlist { if [ -f "$GATEWAYS_FILE" ]; then @@ -114,15 +59,15 @@ function readgwlist { fi allgates=`grep -ve "^[ ]*[\#]\+.*$" "$GATEWAYS_FILE" | xargs` + alldhcpgates="" for gate in $allgates; do - if [[ $(echo "$gate" | grep -c -e "^[:space:]*dhcp\..*$") -eq 1 && \ - -f "$CONFIG_PATH/$gate" ]]; then + if [[ $(echo "$gate" | grep -c -e "^[[:space:]]*dhcp\..*$") -eq 1 && \ + -f "$DHCP_TMP_DIR/$gate" ]]; then # Read dhcp supplied gate for a specific interface - alldhcpgates=`grep -e "^[^\#].*$" "$CONFIG_PATH/$gate" | xargs` - elif [[ $(echo "$gate" | grep -c -e "^[:space:]*dhcp[:space:]*$") -eq 1 ]]; then + alldhcpgates="$alldhcpgates `grep -e "^[^\#].*$" "$DHCP_TMP_DIR/$gate" | xargs`" + elif [[ $(echo "$gate" | grep -c -e "^[[:space:]]*dhcp[[:space:]]*$") -eq 1 ]]; then # Read all dhcp files - alldhcpgates="" - for dhcpfile in $(echo -n $(ls -1 $DHCP_TMP_DIR/dhcp* 2>/dev/null)); do + for dhcpfile in $(echo -n $(ls -1 $DHCP_TMP_DIR/dhcp* 2>/dev/null)); do alldhcpgates="$alldhcpgates `grep -e "^[^\#].*$" "$dhcpfile" | xargs`" done else @@ -196,7 +141,6 @@ function checkgw { fi } -# Get the current default gateway (or set it, if there was none set) function getcurrentgw { iproute_default_gw_txt="$(ip route show scope global)" if [ `echo $iproute_default_gw_txt | grep -c default` -gt 1 ]; then @@ -209,16 +153,13 @@ function getcurrentgw { # no default gateway currently set, set to the highest index, to land on GW0 CURRENTGW_ID=0 CURRENTGW=${GW[$CURRENTGW_ID]} + CURRENTGW_DEV="unknown" echo "No default gateway currently set. Setting it now to $CURRENTGW." ip route add default via $CURRENTGW - if [ $? -eq 2 ]; then - echo "Not able to switch default gateway. Is your interface up?" - exit 1 - fi else CURRENTGW=`echo $iproute_default_gw_txt | sed "s/^default via \(\([0-9]\+\.\?\)\{4\}\).*\$/\1/"` + CURRENTGW_DEV=`echo $iproute_default_gw_txt | sed "s/^default via .* dev \(eth[0-9]\+\).*\$/\1/"` fi - CURRENTGW_DEV=`echo $iproute_default_gw_txt | sed "s/^default via .* dev \(eth[0-9]\+\).*\$/\1/"` # get index of current gateway index=-1 @@ -260,7 +201,7 @@ function getnextgw { function getnewgw { getnextgw # Check wheter all gateways are already tested and failed - if [[ "$NEWGW_ID" -eq "$CURRENTGW_ID" && -z $set_to_gw ]]; then + if [ "$NEWGW_ID" -eq "$CURRENTGW_ID" ]; then if [ -n "$DEBUG" ]; then echo "All gateways tried." fi @@ -286,33 +227,6 @@ function getnewgw { fi } -# Switch the gateway to a desired gateway -function set_gw { - - # get name of gateway - if [[ ! "$set_to_gw" =~ ^[0-9]{1,}$ ]]; then - echo "Only positive numbers allowed. Invalid value \"$set_to_gw\"." - exit 1 - fi - - NEWGW=${GW[$set_to_gw]} - if [ -z "$NEWGW" ]; then - echo "The gateway number $set_to_gw is not in the list of available gateways." - echo "Valid gateways are:" - for ((gw=0; gw<${#GW[*]}; gw++)); do - echo -e "\t$gw (${GW[$gw]})" - done - exit 1 - fi - if [ -n "$DEBUG" -a "$DEBUG" != "low" ]; then - echo "Default gateway desired: $NEWGW ($set_to_gw)" - fi - - getcurrentgw - NEWGW_ID=$[ $set_to_gw - 1 ] - switchgw -} - function switchgw { getnewgw if [ -n "$NEWGW" ]; then @@ -360,6 +274,22 @@ function switchgw { ## MAIN # +# Parse writeable dir request (hidden feature) +if [ "$1" = "-C" ]; then + echo "$DHCP_TMP_DIR" + exit 0 +fi + +# Parse debug switch (another hidden feature) +if [ "$1" = "-d" ]; then + if [ "$2" != "low" ]; then + echo "Debug mode on." + DEBUG="choke" + else + #echo "Info mode on." + DEBUG="low" + fi +fi # Check fping existence if [ ! -x "$PING" ]; then @@ -370,8 +300,13 @@ fi # Get all gateways, we can forward traffic readgwlist -# parse commandline switches -parse_commandline "$@" +# Parse query for current gw (yet another hidden feature) +if [ "$1" = "-q" ]; then + DEBUG= + getcurrentgw + echo "$CURRENTGW" + exit 0 +fi # get current gw getcurrentgw @@ -418,11 +353,13 @@ fi # send an eMail if the connection is down or limited if [ -z "$UP" ]; then - EMAIL_BODY="Switchgate on $(hostname -f) reports: No connection!\n\nTime:\t\t$(date)\nGateway:\t$CURRENTGW ($CURRENTGW_ID)" - EMAIL_SUBJECT="Attention: $(hostname -f) has no connection!" - echo -e "$EMAIL_BODY" | mail -s "$EMAIL_SUBJECT" "$ALERT_EMAIL" - if [[ -n "$DEBUG" && "$DEBUG" != "low" ]]; then - echo "Alert message sent to $ALERT_EMAIL." + if [ -z "$NEWGW" ]; then + EMAIL_BODY="Switchgate on $(hostname -f) reports: No connection!\n\nTime:\t\t$(date)\nGateway:\t$CURRENTGW ($CURRENTGW_ID)" + EMAIL_SUBJECT="Attention: $(hostname -f) has no connection!" + echo -e "$EMAIL_BODY" | mail -s "$EMAIL_SUBJECT" "$ALERT_EMAIL" + if [[ -n "$DEBUG" && "$DEBUG" != "low" ]]; then + echo "Alert message sent to $ALERT_EMAIL." + fi fi elif [ "$CURRENTGW_ID" != 0 ]; then EMAIL_BODY="Switchgate on $(hostname -f) reports: Not operating on standard default gateway!\n\nTime:\t\t$(date)\nGateway:\t$CURRENTGW ($CURRENTGW_ID)" @@ -433,9 +370,15 @@ elif [ "$CURRENTGW_ID" != 0 ]; then fi fi -if [ "$DEBUG" = "low" ]; then - echo - echo "Default Gateway: $CURRENTGW ($CURRENTGW_ID)" - echo +if [ "$DEBUG" = "low" -o "$DEBUG" = "choke" ]; then + if [ -n "$UP" ]; then + echo + echo "Current Default Gateway: $CURRENTGW ($CURRENTGW_ID)" + echo + else + echo + echo "No connection." + echo + fi fi exit 0