version 1.1.1.1, 2009/04/24 00:12:49
|
version 1.8, 2017/03/28 21:44:24
|
Line 1
|
Line 1
|
#!/bin/bash |
#!/bin/bash |
|
|
# switchhgate 0.0.3 |
# switchhgate 0.0.5 |
# (c) 2005 under GPL by Adrian Zaugg |
# (c) 2005-2010 under GPL by Adrian Zaugg |
|
|
# switchgate pings a set of hosts to determine the state of the internet connectivity. If |
# 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. |
# the connection is down, it can change the default gateway to an alternative gateway. |
Line 16 CONFIG_PATH="/etc/switchgate"
|
Line 16 CONFIG_PATH="/etc/switchgate"
|
# the dhclient-exit-hook helper program |
# the dhclient-exit-hook helper program |
DHCP_TMP_DIR=/tmp/switchgate |
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. |
# 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. |
# Do not include any local hosts, list only hosts located behind your gateways. |
HOSTSET_FILE="$CONFIG_PATH/hostset" |
HOSTSET_FILE="$CONFIG_PATH/hostset" |
Line 24 HOSTSET_FILE="$CONFIG_PATH/hostset"
|
Line 28 HOSTSET_FILE="$CONFIG_PATH/hostset"
|
GATEWAYS_FILE="$CONFIG_PATH/gateways" |
GATEWAYS_FILE="$CONFIG_PATH/gateways" |
|
|
# path to fping |
# path to fping |
PING=/usr/sbin/fping |
PING=/usr/bin/fping |
|
|
# answer of fping to reachable hosts |
# answer of fping to reachable hosts |
ALIVE_ANSWER="is alive" |
ALIVE_ANSWER="is alive" |
Line 55 function readgwlist {
|
Line 59 function readgwlist {
|
fi |
fi |
|
|
allgates=`grep -ve "^[ ]*[\#]\+.*$" "$GATEWAYS_FILE" | xargs` |
allgates=`grep -ve "^[ ]*[\#]\+.*$" "$GATEWAYS_FILE" | xargs` |
|
alldhcpgates="" |
for gate in $allgates; do |
for gate in $allgates; do |
if [[ $(echo "$gate" | grep -c -e "^[:space:]*dhcp\..*$") -eq 1 && \ |
if [[ $(echo "$gate" | grep -c -e "^[[:space:]]*dhcp\..*$") -eq 1 && \ |
-f "$CONFIG_PATH/$gate" ]]; then |
-f "$DHCP_TMP_DIR/$gate" ]]; then |
# Read dhcp supplied gate for a specific interface |
# Read dhcp supplied gate for a specific interface |
alldhcpgates=`grep -e "^[^\#].*$" "$CONFIG_PATH/$gate" | xargs` |
alldhcpgates="$alldhcpgates `grep -e "^[^\#].*$" "$DHCP_TMP_DIR/$gate" | xargs`" |
elif [[ $(echo "$gate" | grep -c -e "^[:space:]*dhcp[:space:]*$") -eq 1 ]]; then |
elif [[ $(echo "$gate" | grep -c -e "^[[:space:]]*dhcp[[:space:]]*$") -eq 1 ]]; then |
# Read all dhcp files |
# 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`" |
alldhcpgates="$alldhcpgates `grep -e "^[^\#].*$" "$dhcpfile" | xargs`" |
done |
done |
else |
else |
Line 81 function readgwlist {
|
Line 85 function readgwlist {
|
unset alldhcpgates |
unset alldhcpgates |
done |
done |
fi |
fi |
|
|
if [ "$number_of_gws" -eq 0 ]; then |
if [ "$number_of_gws" -eq 0 ]; then |
echo "No gateways configured. Please edit $GATEWAYS_FILE." |
echo "No gateways configured. Please edit $GATEWAYS_FILE." |
exit 1 |
exit 1 |
Line 117 function checkconnection {
|
Line 121 function checkconnection {
|
if [ -n "$DEBUG" ]; then |
if [ -n "$DEBUG" ]; then |
if [ -n "$UP" ]; then |
if [ -n "$UP" ]; then |
echo "The connection is up." |
echo "The connection is up." |
else |
else |
echo "The connection is down." |
echo "The connection is down." |
fi |
fi |
fi |
fi |
Line 130 function checkgw {
|
Line 134 function checkgw {
|
echo "$NEWGW is reachable." |
echo "$NEWGW is reachable." |
fi |
fi |
else |
else |
if [ -n "$DEBUG" ]; then |
if [ -n "$DEBUG" ]; then |
echo "$NEWGW is not reachable." |
echo "$NEWGW is not reachable." |
fi |
fi |
NEWGW="" |
NEWGW="" |
Line 144 function getcurrentgw {
|
Line 148 function getcurrentgw {
|
exit 1 |
exit 1 |
else |
else |
iproute_default_gw_txt="$(echo "$iproute_default_gw_txt" | grep "default via")" |
iproute_default_gw_txt="$(echo "$iproute_default_gw_txt" | grep "default via")" |
fi |
fi |
if [ -z "$iproute_default_gw_txt" ]; then |
if [ -z "$iproute_default_gw_txt" ]; then |
# no default gateway currently set, set to the highest index, to land on GW0 |
# no default gateway currently set, set to the highest index, to land on GW0 |
CURRENTGW_ID=0 |
CURRENTGW_ID=0 |
Line 153 function getcurrentgw {
|
Line 157 function getcurrentgw {
|
echo "No default gateway currently set. Setting it now to $CURRENTGW." |
echo "No default gateway currently set. Setting it now to $CURRENTGW." |
ip route add default via $CURRENTGW |
ip route add default via $CURRENTGW |
else |
else |
CURRENTGW=`echo $iproute_default_gw_txt | sed "s/^default via \(\([0-9]\+\.\?\)\{4\}\).*\$/\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/"` |
CURRENTGW_DEV=`echo $iproute_default_gw_txt | sed "s/^default via .* dev \(eth[0-9]\+\).*\$/\1/"` |
fi |
fi |
|
|
# get index of current gateway |
# get index of current gateway |
Line 228 function switchgw {
|
Line 232 function switchgw {
|
if [ -n "$NEWGW" ]; then |
if [ -n "$NEWGW" ]; then |
if [ "$DEBUG" = "low" ]; then |
if [ "$DEBUG" = "low" ]; then |
echo -n "Switching default gateway now to $NEWGW ($NEWGW_ID)..." |
echo -n "Switching default gateway now to $NEWGW ($NEWGW_ID)..." |
fi |
fi |
|
|
iproute_msg="$(ip route change default via $NEWGW 2>&1 )" |
iproute_msg="$(ip route change default via $NEWGW 2>&1 )" |
iproute_exit=$? |
iproute_exit=$? |
|
|
Line 237 function switchgw {
|
Line 241 function switchgw {
|
sleep 1 |
sleep 1 |
GARBAGE=`$PING -q -c 5 -t100 -p50 -A -f $HOSTSET_FILE 2> /dev/null` |
GARBAGE=`$PING -q -c 5 -t100 -p50 -A -f $HOSTSET_FILE 2> /dev/null` |
sleep 1 |
sleep 1 |
|
|
if [ "$DEBUG" = "low" ]; then |
if [ "$DEBUG" = "low" ]; then |
echo "done." |
echo "done." |
fi |
fi |
Line 287 if [ "$1" = "-d" ]; then
|
Line 291 if [ "$1" = "-d" ]; then
|
fi |
fi |
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 |
# Get all gateways, we can forward traffic |
readgwlist |
readgwlist |
|
|
Line 317 if [ -z "$UP" ]; then
|
Line 327 if [ -z "$UP" ]; then
|
checkgw |
checkgw |
if [ -z "$NEWGW" ]; then |
if [ -z "$NEWGW" ]; then |
echo "Gateway $CURRENTGW is down." |
echo "Gateway $CURRENTGW is down." |
else |
else |
echo "No connection through gateway $CURRENTGW." |
echo "No connection through gateway $CURRENTGW." |
fi |
fi |
|
|
NEWGW_ID=$CURRENTGW_ID |
NEWGW_ID=$CURRENTGW_ID |
switchgw |
switchgw |
|
|
else |
else |
# If the current gateway is not the standard default gateway, try if the connection |
# If the current gateway is not the standard default gateway, try if the connection |
# through the standard default gateway is back |
# through the standard default gateway is back |
Line 341 else
|
Line 351 else
|
fi |
fi |
fi |
fi |
|
|
if [ "$DEBUG" = "low" ]; then |
# send an eMail if the connection is down or limited |
echo |
if [ -z "$UP" ]; then |
echo "Default Gateway: $CURRENTGW ($CURRENTGW_ID)" |
if [ -z "$NEWGW" ]; then |
echo |
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 |
fi |
exit 0 |
exit 0 |