Failover Script

Failover script for dual WAN failover setup which switches back to primary when connection restores

A dual NIC (Network Interface Card) or dual WAN (Wide Area Network) setup with a failover script allows a system to automatically switch to a backup internet connection if the primary connection fails. This ensures continuous internet access for the user. A script monitors the primary connection and, upon detection of a failure, triggers the switch to the secondary connection.

Here's a breakdown of the key components and a conceptual script outline:1. Understanding the Setup:

  • Primary and Secondary WAN:A primary internet connection (e.g., a faster, more reliable connection) and a secondary connection (e.g., a slower, backup connection) are both configured.

  • Monitoring:The script monitors the primary connection's status. This is often done by pinging a specific IP address (like a DNS server) or checking the gateway's status.

  • Failover:If the primary connection fails (e.g., ping fails or the gateway is unreachable), the script activates the secondary connection - secondary connection eth1 is up and primary connection eth0 is down. To switch back to the primary connection - eth0 up and eth1 down, do so manually with the command sudo ip link set eth0 up .

  • Failback:The script can also be configured to switch back to the primary connection once it's restored (failback). This can be done with failback_delay function.

Code:

#!/bin/bash

# Configuration
PRIMARY_INTERFACE="eth0"  # Replace with your primary interface name
SECONDARY_INTERFACE="eth1" # Replace with your secondary interface name
TARGET_IP="8.8.8.8"       # Replace with a reliable IP to ping (e.g., Google's DNS)
PING_TIMEOUT=2            # Timeout in seconds for ping
SLEEP_INTERVAL=5         # How often to check (seconds)

# Function to check if a gateway is reachable
is_gateway_reachable() {
  local interface="$1"
  local target="$2"
  local timeout="$3"
  if ping -c 1 -W "$timeout" -I "$interface" "$target" > /dev/null 2>&1; then
    return 0 # Success (reachable)
  else
    return 1 # Failure (unreachable)
  fi
}

# Function to switch to secondary interface
switch_to_secondary() {
  echo "$(date '+%Y-%m-%d %H:%M:%S') - Switching to secondary interface: $SECONDARY_INTERFACE"
  # Disable primary interface (example)
  sudo ip link set "$PRIMARY_INTERFACE" down
  # Enable secondary interface (example)
  sudo ip link set "$SECONDARY_INTERFACE" up
  # Optionally, configure routing to use the secondary gateway if necessary
  # sudo ip route add default via <secondary_gateway_ip> dev "$SECONDARY_INTERFACE"
}

# Function to switch back to primary interface
switch_to_primary() {
  echo "$(date '+%Y-%m-%d %H:%M:%S') - Switching back to primary interface: $PRIMARY_INTERFACE"   
      # Disable secondary interface (example)
  sudo ip link set "$SECONDARY_INTERFACE" down
  # Enable primary interface (example)
  sudo ip link set "$PRIMARY_INTERFACE" up
  # Optionally, configure routing to use the primary gateway if necessary
  # sudo ip route add default via <primary_gateway_ip> dev "$PRIMARY_INTERFACE"
}

# Main script logic
while true; do
  if is_gateway_reachable "$PRIMARY_INTERFACE" "$TARGET_IP" "$PING_TIMEOUT"; then
    echo "$(date '+%Y-%m-%d %H:%M:%S') - Primary interface is up"
    # Check if currently on secondary, and switch back if appropriate
    if ip link show "$SECONDARY_INTERFACE" | grep "state UP" > /dev/null; then
      switch_to_primary
    fi
  else
    echo "$(date '+%Y-%m-%d %H:%M:%S') - Primary interface is down"
    # Check if currently on primary, and switch to secondary
    if ! ip link show "$SECONDARY_INTERFACE" | grep "state UP" > /dev/null; then
      switch_to_secondary
    fi
  fi
  sleep "$SLEEP_INTERVAL"
done

Explanation:

  1. Configuration:Sets up variables for interface names, the target IP for pinging, timeout, and the interval between checks.

  2. is_gateway_reachable function:This function attempts to ping the target IP address through the specified interface. It returns 0 on success (reachable) and 1 on failure (unreachable).

  3. Systemd Timer File is not required to periodically trigger the failover script.

To use this script:

  1. Save the script: Save the script to a file, for example, wan-failover.sh

  2. Make it executable: chmod +x wan-failover.sh

Last updated