Perl Internet failover script

From Mike A. Leonetti

Jump to: navigation, search

This script was from a failover project on Gentoo using iproute2 to allow two internet connections (dual connections) to either be load balanced or just strictly failover. Right now a Perl script gets forked into the background via an init script. The Perl script also sets up the routes after the interfaces are initialized. Any suggestions/commets/etc are appreciated. I know this has probably been done before, and somebody mentioned once on the Gentoo Forums to me that this could also be achieved with bonding. However, despite reinventing the wheel, here is the script.

Contents

Download

The script download: http://www.mikealeonetti.com/files/failover

Configurations

Of course, the IPs you see below are 10.1.1.34 and 192.168.2.77 which you could replace with INTERNET_IP1 and INTERNET_IP2 based on your network configurations.

/etc/conf.d/net

modules=( "iproute2" )
config_eth0=( "10.1.1.34/24" )
config_eth1=( "192.168.2.77/24" )

In the kernel config:

CONFIG_IP_ADVANCED_ROUTER=y
CONFIG_IP_MULTIPLE_TABLES=y
CONFIG_IP_ROUTE_MULTIPATH=y

/etc/iproute2/rt_tables

#
# reserved values
#
255 local
254 main
253 default
0 unspec
#
# local
#
#1 inr.ruhep
100 dual
101 route1
102 route2

/etc/init.d/multinet

#!/sbin/runscript
# Copyright 1999-2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

depend() {
after net
}

start() {
ebegin "Adding routes and starting failover daemon..."
/usr/local/bin/failover > /dev/null
eend $?
}

stop() {
ebegin "Flushing rules and stopping script"
killall -15 failover
eend $?
}

You may need to change the DNS servers that your machine resolves from because if you switch to another IP that is not allowed to query off of one DNS server you will get horrible timeouts. You can safely use a public DNS server to fix this issue.

/etc/resolv.conf

# Opendns.com nameserver
nameserver 208.67.222.222

You'll now have to open up the failover script and edit the first bunch of lines that look like this:

$connexion[0] = {};
$connexion[0]->{'if'} = "eth0"; # Interface for the connexion
$connexion[0]->{'weight'} = "4"; # Weight of the connexion. A higher weight means the loadbalance will favor it
$connexion[0]->{'ip'} = "10.1.1.34";  # IP address of this connexion
$connexion[0]->{'gw'} = "10.1.1.1"; # Gateway
$connexion[0]->{'name'} = "FiOS"; # Name of the connexion (for logging purposes)
$connexion[0]->{'network_string'} = "10.1.1.0/24"; # In the format "network/netmask"
$connexion[0]->{'dns'} = ""; # A space separated list of DNS servers to change resolv.conf to upon switch, leave blank to leave resolv.conf unmodified

And

$connexion[1] = {};
$connexion[1]->{'if'} = "eth1";
$connexion[1]->{'weight'} = "1";
$connexion[1]->{'ip'} = "192.168.2.77";
$connexion[1]->{'gw'} = "192.168.2.1";
$connexion[1]->{'name'} = "Optimum";
$connexion[1]->{'network_string'} = "192.168.2.0/24";
$connexion[1]->{'dns'} = "";

Again, these IPs, names, and other information are for my network. You will need to put in your network information instead.

Lastly just add the multinet script to the startup (On Gentoo rc-update add multinet default) and you should be good to go.

Contact me if you have any issues.

Caveats

This script only works if both connections have static IPs.

Original sources

Updates

Update 01/16/2011

Added DNS switching and capabilities to set the iface IP right from the script (thanks Noumes).


Personal tools
Google AdSense