|
#!/bin/bash |
|
|
|
CHECK_DELAY=${CHECK_DELAY:-7} |
|
CHECK_IP1=${CHECK_IP1:-8.8.8.8} |
|
CHECK_IP2=${CHECK_IP2:-8.8.4.4} |
|
PRIMARY_IF=${PRIMARY_IF:-enp4s0} |
|
PRIMARY_GW=${PRIMARY_GW:-192.168.2.1} |
|
BACKUP_IF=${BACKUP_IF:-eno1} |
|
BACKUP_GW=${BACKUP_GW:-192.168.1.1} |
|
HOSTNAME=${HOSTNAME:-socky} |
|
|
|
is_gateway() { |
|
[[ "$1" = "$(ip route show default 0.0.0.0/0 | cut -d' ' -f5)" ]] |
|
} |
|
|
|
curl --silent --header 'Access-Token: ' \ |
|
--header 'Content-Type: application/json' \ |
|
--data-binary '{"body":"Socky has just started or rebooted.","title":"Socky has started","type":"note"}' \ |
|
--request POST https://api.pushbullet.com/v2/pushes > /dev/null |
|
|
|
ip rule del table t1 |
|
ip rule del table t2 |
|
|
|
ip rule add from 192.168.1.10 table t1 |
|
ip rule add from 192.168.2.10 table t2 |
|
|
|
while sleep "$CHECK_DELAY" |
|
do |
|
ip route add 192.168.1.0 dev $BACKUP_IF src 192.168.1.10 table t1 &>/dev/null |
|
ip route add 192.168.2.0 dev $PRIMARY_IF src 192.168.2.10 table t2 &>/dev/null |
|
ip route add $CHECK_IP1 via $BACKUP_GW dev $BACKUP_IF src 192.168.1.10 table t1 &>/dev/null |
|
ip route add $CHECK_IP1 via $PRIMARY_GW dev $PRIMARY_IF src 192.168.2.10 table t2 &>/dev/null |
|
ip route add $CHECK_IP2 via $BACKUP_GW dev $BACKUP_IF src 192.168.1.10 table t1 &>/dev/null |
|
ip route add $CHECK_IP2 via $PRIMARY_GW dev $PRIMARY_IF src 192.168.2.10 table t2 &>/dev/null |
|
ip route add default via $BACKUP_GW table t1 &>/dev/null |
|
ip route add default via $PRIMARY_GW table t2 &>/dev/null |
|
|
|
# ip route add 192.168.1.0/24 dev eno1 src 192.168.1.10 metric 100 &>/dev/null |
|
# ip route add 192.168.2.0/24 dev enp4s0 src 192.168.2.10 metric 100 &>/dev/null |
|
|
|
# ip rule add from 192.168.1.10 table t1 |
|
# ip rule add from 192.168.2.10 table t2 |
|
|
|
ip route add $CHECK_IP1 via $BACKUP_GW metric 1 &>/dev/null |
|
ip route add $CHECK_IP1 via $PRIMARY_GW metric 2 &>/dev/null |
|
ip route add $CHECK_IP2 via $BACKUP_GW metric 1 &>/dev/null |
|
ip route add $CHECK_IP2 via $PRIMARY_GW metric 2 &>/dev/null |
|
|
|
PRIMARY_DOWN=1 |
|
BACKUP_DOWN=1 |
|
if ping -I "$PRIMARY_IF" -c1 -W1 "$CHECK_IP1" &>/dev/null |
|
then |
|
if ping -I "$PRIMARY_IF" -c1 -W1 "$CHECK_IP2" &>/dev/nulli |
|
then |
|
PRIMARY_DOWN=0 |
|
else |
|
PRIMARY_DOWN=1 |
|
fi |
|
else |
|
PRIMARY_DOWN=1 |
|
fi |
|
|
|
if ping -I "$BACKUP_IF" -c1 -W1 "$CHECK_IP1" &>/dev/null |
|
then |
|
if ping -I "$BACKUP_IF" -c1 -W1 "$CHECK_IP2" &>/dev/null |
|
then |
|
BACKUP_DOWN=0 |
|
else |
|
BACKUP_DOWN=1 |
|
fi |
|
else |
|
BACKUP_DOWN=1 |
|
fi |
|
|
|
if [ $PRIMARY_DOWN -eq 0 ] |
|
then |
|
echo Primary Interface is up |
|
if is_gateway "$BACKUP_IF" |
|
then |
|
echo Switch back to Primary Interface |
|
ip route del default via "$BACKUP_GW" dev "$BACKUP_IF" |
|
ip route add default via "$PRIMARY_GW" dev "$PRIMARY_IF" |
|
curl --silent --header 'Access-Token: ' \ |
|
--header 'Content-Type: application/json' \ |
|
--data-binary '{"body":"Socky Primary Interface is back online","title":"Socky switched back to Primary Interface","type":"note"}' \ |
|
--request POST https://api.pushbullet.com/v2/pushes > /dev/null |
|
fi |
|
elif [ $BACKUP_DOWN -eq 0 ] |
|
then |
|
echo Primary Interface is down |
|
echo Backup Interface is up |
|
if is_gateway "$PRIMARY_IF" |
|
then |
|
echo Switch to Backup Interface |
|
ip route del default via "$PRIMARY_GW" dev "$PRIMARY_IF" |
|
ip route add default via "$BACKUP_GW" dev "$BACKUP_IF" |
|
curl --silent --header 'Access-Token: ' \ |
|
--header 'Content-Type: application/json' \ |
|
--data-binary '{"body":"Socky Primary Interface is down","title":"Socky switched to Backup Interface","type":"note"}' \ |
|
--request POST https://api.pushbullet.com/v2/pushes > /dev/null |
|
fi |
|
else |
|
echo All Interface are down |
|
fi |
|
done |