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.



The script download:


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


modules=( "iproute2" )
config_eth0=( "" )
config_eth1=( "" )

In the kernel config:



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


# 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.


# nameserver

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'} = "";  # IP address of this connexion
$connexion[0]->{'gw'} = ""; # Gateway
$connexion[0]->{'name'} = "FiOS"; # Name of the connexion (for logging purposes)
$connexion[0]->{'network_string'} = ""; # 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


$connexion[1] = {};
$connexion[1]->{'if'} = "eth1";
$connexion[1]->{'weight'} = "1";
$connexion[1]->{'ip'} = "";
$connexion[1]->{'gw'} = "";
$connexion[1]->{'name'} = "Optimum";
$connexion[1]->{'network_string'} = "";
$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.


This script only works if both connections have static IPs.

Original sources


Update 01/16/2011

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

Personal tools
Google AdSense