VPN S2S Failover Script with BASH

Job ID: 34078549

Budget: $30 – $250 USD

I have a case in a network that is connected to a VPN s2s, the deployment is :

IP a connect to IP c as active - (over vpn s2s)
IP b connect to IP c as backup - (over vpn s2s)

where what we want is when IP a fails to connect to the internet the s2s VPN connection from IP a to IP c breaks, then we make the script below, which is run on crontab/cronjob on unix every 1 minute:

if ping -I WAN -c 3 8.8.8.8 &> 0
then
clish -c "set static-route 3 disabled true"

else
clish -c "vpn tu del x.x.x.x"
clish -c "vpn tu del x.x.x.x"
clish -c "vpn tu del x.x.x.x"
clish -c "set static-route 3 disabled false"
fi

The weakness of this script is that if the condition fails it will do an else which will repeat itself. which makes every 1 minute run clish -c "vpn tu del x.x.x.x", now what we want is clish -c "vpn tu del x.x.x.x" done only once as long as IP a fails to connect to the internet or 8.8.8.8

explanation:
if ping -I WAN -c 3 8.8.8.8 &> 0 -> to check whether ip a can still connect to the internet
if successfully connected do this ->
clish -c "set static-route 3 disabled true"

if failed to connect do this ->
else
clish -c "vpn tu del x.x.x.x"
clish -c "vpn tu del x.x.x.x"
clish -c "vpn tu del x.x.x.x"
clish -c "set static-route 3 disabled false"