--- badi/public_scripts/switchgate/switchgate 2009/04/24 00:12:49 1.1.1.1 +++ 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. @@ -16,6 +16,10 @@ CONFIG_PATH="/etc/switchgate" # the dhclient-exit-hook helper program DHCP_TMP_DIR=/tmp/switchgate +# eMail address to send alert messages, if not operating on the +# standard default gateway +ALERT_EMAIL="adi@ente.limmat.ch" + # Host set to ping. A file containing IP addresses, each on a single line. # Do not include any local hosts, list only hosts located behind your gateways. HOSTSET_FILE="$CONFIG_PATH/hostset" @@ -24,7 +28,7 @@ HOSTSET_FILE="$CONFIG_PATH/hostset" GATEWAYS_FILE="$CONFIG_PATH/gateways" # path to fping -PING=/usr/sbin/fping +PING=/usr/bin/fping # answer of fping to reachable hosts ALIVE_ANSWER="is alive" @@ -55,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 @@ -81,7 +85,7 @@ function readgwlist { unset alldhcpgates done fi - + if [ "$number_of_gws" -eq 0 ]; then echo "No gateways configured. Please edit $GATEWAYS_FILE." exit 1 @@ -117,7 +121,7 @@ function checkconnection { if [ -n "$DEBUG" ]; then if [ -n "$UP" ]; then echo "The connection is up." - else + else echo "The connection is down." fi fi @@ -130,7 +134,7 @@ function checkgw { echo "$NEWGW is reachable." fi else - if [ -n "$DEBUG" ]; then + if [ -n "$DEBUG" ]; then echo "$NEWGW is not reachable." fi NEWGW="" @@ -144,7 +148,7 @@ function getcurrentgw { exit 1 else iproute_default_gw_txt="$(echo "$iproute_default_gw_txt" | grep "default via")" - fi + fi if [ -z "$iproute_default_gw_txt" ]; then # no default gateway currently set, set to the highest index, to land on GW0 CURRENTGW_ID=0 @@ -153,8 +157,8 @@ function getcurrentgw { echo "No default gateway currently set. Setting it now to $CURRENTGW." ip route add default via $CURRENTGW 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/"` + 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 # get index of current gateway @@ -228,8 +232,8 @@ function switchgw { if [ -n "$NEWGW" ]; then if [ "$DEBUG" = "low" ]; then echo -n "Switching default gateway now to $NEWGW ($NEWGW_ID)..." - fi - + fi + iproute_msg="$(ip route change default via $NEWGW 2>&1 )" iproute_exit=$? @@ -237,7 +241,7 @@ function switchgw { sleep 1 GARBAGE=`$PING -q -c 5 -t100 -p50 -A -f $HOSTSET_FILE 2> /dev/null` sleep 1 - + if [ "$DEBUG" = "low" ]; then echo "done." fi @@ -287,6 +291,12 @@ if [ "$1" = "-d" ]; then fi fi +# Check fping existence +if [ ! -x "$PING" ]; then + echo -e "Error: Ping program not executable or not found at $PING.\nPlease configure the full path in $0." + exit 1 +fi + # Get all gateways, we can forward traffic readgwlist @@ -317,13 +327,13 @@ if [ -z "$UP" ]; then checkgw if [ -z "$NEWGW" ]; then echo "Gateway $CURRENTGW is down." - else + else echo "No connection through gateway $CURRENTGW." fi - + NEWGW_ID=$CURRENTGW_ID switchgw - + else # If the current gateway is not the standard default gateway, try if the connection # through the standard default gateway is back @@ -341,9 +351,34 @@ else fi fi -if [ "$DEBUG" = "low" ]; then - echo - echo "Default Gateway: $CURRENTGW ($CURRENTGW_ID)" - echo +# send an eMail if the connection is down or limited +if [ -z "$UP" ]; then + 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)" + EMAIL_SUBJECT="Attention: $(hostname -f) not operating on standard default gateway!" + 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 + +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