You are not logged in.

#1 2020-09-03 01:45:23

Strykar
Member
Registered: 2018-02-17
Posts: 50

Looking for feedback on multi-wan script

This script is based of https://gist.github.com/Apsu/5021255 and some comments there, my full version is at https://gist.github.com/Strykar/fce98ce … db53ab513c.
I am thinking of calling this via .bashrc and nohup

I am wondering if there is a more efficient way of invoking it since it will run all the time on a slightly old machine, any tips are appreciated.

#!/bin/bash

# Start user config.
# Define network interfaces
wan0='enp2s0' # Primary WAN interface
wan1='enp0s29u1u2u1' # Backup WAN interface
# Set defaults if not provided by environment
CHECK_DELAY='5'
CHECK_IP0='8.8.8.8'
CHECK_IP1='8.8.4.4'
PRIMARY_IF=$wan0
PRIMARY_GW=$wan0_gw
BACKUP_IF=$wan1
BACKUP_GW=$wan1_gw
# End user config

# Grok gateway addresses, this assumes both gateway defined above are up
wan0_gw=$( route -n | awk -v wan0="$wan0" '$4 == "UG" && $NF ~ wan0 { print $2 }' )
wan1_gw=$( route -n | awk -v wan1="$wan1" '$4 == "UG" && $NF ~ wan1 { print $2 }' )

# Compare arg with current default gateway interface for route to healthcheck IP
gateway_if() {
[[ "$1" == "$(ip r g "$CHECK_IP1" | sed -rn 's/^.dev ([^ ]).*$/\1/p')" ]]
}
ip r d "$CHECK_IP0/32"
ip r a "$CHECK_IP0/32" via "$PRIMARY_GW" dev "$PRIMARY_IF"
# Cycle healthcheck continuously with specified delay
while sleep "$CHECK_DELAY"
do
# If healthcheck succeeds from primary interface
	if ping -I "$PRIMARY_IF" -c1 "$CHECK_IP0" &>/dev/null
		then
# Are we using the backup?
	if gateway_if "$BACKUP_IF"
		then # Switch to primary
ip r d default via "$BACKUP_GW" dev "$BACKUP_IF"
ip r a default via "$PRIMARY_GW" dev "$PRIMARY_IF"
	fi
	else
# Are we using the primary?
	if gateway_if "$PRIMARY_IF"
		then # Switch to backup
ip r d default via "$PRIMARY_GW" dev "$PRIMARY_IF"
ip r a default via "$BACKUP_GW" dev "$BACKUP_IF"
		fi
	fi
done

Offline

#2 2020-09-03 06:50:20

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,224
Website

Re: Looking for feedback on multi-wan script

Strykar wrote:

I am thinking of calling this via .bashrc and nohup

That sounds like a terrible idea smile

Create a systemd service: https://wiki.archlinux.org/index.php/Sy … unit_files

Offline

Board footer

Powered by FluxBB