I put together a very crude ISP failover script for a small office
running an OpenBSD firewall and 2 broadband Internet connections.

It's run every minute from root's crontab.

Comments welcome, keep in mind that I am not a programmer. And I know
the "echo > /dev/null" lines are ugly, and I even know how to fix it, I
just didn't do it yet.

Enjoy!

-Jason

=====

$ cat failover
#!/bin/sh

EMAIL=insert_your_INTERNAL_email_account_here
PRIMARY_IP=static_ip_of_primary_connection
PRIMARY_GW=ip_of_primary_gateway
BACKUP_IP=static_ip_of_backup_connection
BACKUP_GW=ip_of_backup_gateway

DEFAULT_GW=`cat /etc/mygate`

# echo "Begin Default GW: $DEFAULT_GW"

# test if primary is up
if { ping -c 5 -w 2 -I $PRIMARY_IP $PRIMARY_GW > /dev/null; } then
        # primary up, check default gateway
        if [ $PRIMARY_GW == $DEFAULT_GW ]; then
                # primary gateway equals default gateway: exit
                # echo "Primary up, no change"
                echo > /dev/null
        else
                # set default gateway to primary
                logger -s -t "Failover" "Restoring PRIMARY connection."
                echo -n $PRIMARY_GW > /etc/mygate
                route change default $PRIMARY_GW
        fi
elif { ping -c 5 -w 2 -I $BACKUP_IP $BACKUP_GW > /dev/null; } then
        # primary down, backup up: test default gateway
        if [ $BACKUP_GW == $DEFAULT_GW ]; then
                # secondary is already default: exit
                # echo "Secondary up, no change"
                echo > /dev/null
        else
                # switch default gateway to backup
                logger -s -t "Failover" "Switching to BACKUP
connection."
                echo -n $BACKUP_GW > /etc/mygate
                route change default $BACKUP_GW
        fi
else
        # both are down
        logger -s -t "Failover" "Both Internet gateways are DOWN!"
        echo "Both Internet gateways are DOWN!" | mail -s "Failover
warning!" $EMAIL
fi

# echo "End Default GW: $DEFAULT_GW"

=====

Reply via email to