Failover floating static route

I was trying to figure this out using:

But I did not seem to understand or figure out how to get this done in /etc/rc.conf

So I want to have a static route pointing to one gateway in day to day situations but I want another static route to take over in case the primary route path is unavailable.

Usually I have done this with higher or lower metric added to static routes in cisco but I was hoping there was something similar in FreeBSD.

I understand up to the point on how to create a static route on rc.conf but adding metrics I could not find an example of it.

Does anyone know if this is capable? If so, may you kindly point to a resource that I can read that shows how it is done.

Thank you all for your time.

Best,
T
 
afaik without a routing protocol/dynamic routing you can't
what you can do is ping your default gw from a script and if it fails issue a route change default
if it succeeds and you are on the secondary route change to the default gw

this works pretty well but does not cover the case when the remote gw is up and its uplink is down
to fix this you can add a route to known reliable host/ip (that you don't really use) thru your primary gateway (like 1.1.1.1)
and ping that instead your primary gw

drawback is that if you want to access that host when the primary gw is down you wont be able to
 
I understand up to the point on how to create a static route on rc.conf but adding metrics I could not find an example of it.
Metrics are useless on static routing. They only have a purpose with dynamic routing protocols like BGP or OSPF.
 
afaik without a routing protocol/dynamic routing you can't
what you can do is ping your default gw from a script and if it fails issue a route change default
if it succeeds and you are on the secondary route change to the default gw

this works pretty well but does not cover the case when the remote gw is up and its uplink is down
to fix this you can add a route to known reliable host/ip (that you don't really use) thru your primary gateway (like 1.1.1.1)
and ping that instead your primary gw

drawback is that if you want to access that host when the primary gw is down you wont be able to
Ah! Darn, I was worried about that. Thank you for the suggestion covacat .
I am new to bash scripting, do you happen to know a good guide for setting up what you suggested? And where/directory I should activate the script?

Thank you for the great guidance.
 
Code:
#!/bin/sh
#
NORMAL_ROUTE="10.10.10.10"
BACKUP_ROUTE="192.168.8.20"
ADMIN=it@some-domain.com
chroute()
{
route change default $1
if [ $? -eq 0 ]
then
#  logger -t "CH-ROUTE"  "Route changed to $2 ($1)"
#  echo "Route changed to $2 ($1)"|mail -s "Route changed to ($2) $1" $ADMIN
#  echo "$2 ($1)" > /home/sites/server-status/r.txt
else
# panic should change route but couldn't
fi
}

CURRENT_ROUTE=$(route -4 -n get default|grep gate|cut -w  -f 3)
ping -q -c 3 $NORMAL_ROUTE >/dev/null 2>&1
OK=$?
if [ $OK -eq 0 ]
 then
  [ "$CURRENT_ROUTE" = "$NORMAL_ROUTE" ] && exit 0
  chroute $NORMAL_ROUTE NORMAL_ROUTE
  else
  [ "$CURRENT_ROUTE" = "$BACKUP_ROUTE" ] && exit 0
  chroute $BACKUP_ROUTE BACKUP_ROUTE
 fi
if you have 2 ISPs then make sure your dns is set to a public DNS like 8.8.8.8 or 1.1.1.1 because you may not be able to query a specific ISP's DNS with an IP from another ISP
you can run it from cron every 2 minutes or so
if you need faster route change put everything in a while true and add a sleep of several second at the loops end and run it from /etc/rc.local
 
Code:
#!/bin/sh
#
NORMAL_ROUTE="10.10.10.10"
BACKUP_ROUTE="192.168.8.20"
ADMIN=it@some-domain.com
chroute()
{
route change default $1
if [ $? -eq 0 ]
then
#  logger -t "CH-ROUTE"  "Route changed to $2 ($1)"
#  echo "Route changed to $2 ($1)"|mail -s "Route changed to ($2) $1" $ADMIN
#  echo "$2 ($1)" > /home/sites/server-status/r.txt
else
# panic should change route but couldn't
fi
}

CURRENT_ROUTE=$(route -4 -n get default|grep gate|cut -w  -f 3)
ping -q -c 3 $NORMAL_ROUTE >/dev/null 2>&1
OK=$?
if [ $OK -eq 0 ]
 then
  [ "$CURRENT_ROUTE" = "$NORMAL_ROUTE" ] && exit 0
  chroute $NORMAL_ROUTE NORMAL_ROUTE
  else
  [ "$CURRENT_ROUTE" = "$BACKUP_ROUTE" ] && exit 0
  chroute $BACKUP_ROUTE BACKUP_ROUTE
 fi
if you have 2 ISPs then make sure your dns is set to a public DNS like 8.8.8.8 or 1.1.1.1 because you may not be able to query a specific ISP's DNS with an IP from another ISP
you can run it from cron every 2 minutes or so
if you need faster route change put everything in a while true and add a sleep of several second at the loops end and run it from /etc/rc.local
This is awesome! Thank you very much this is a great start. I will update on how I was able to implement it. :)
 
Back
Top