Code:
#!/bin/bash
#
# bal_local Load-balance internet connection over two local links
#
# Version: 1.0.0 - Fri, Sep 26, 2008
#
# Author: Niels Horn <[email protected]>
#
# Modified: Motor Mafia <[email protected]> for Ubuntu 18.04 16.Sept 2018
#
# Set devices:
#
#
DEV1=`route -n | grep '^0.0.0.0'|awk '{print $NF}'|awk 'NR==1'`
DEV2=`route -n | grep '^0.0.0.0'|awk '{print $NF}'|awk 'NR==2'`
# Get IP addresses of our devices:
ip1=`ifconfig $DEV1 | grep inet |grep -v inet6 | sed -e 's/^[[:space:]]*//' |cut -d' ' -f2`
ip2=`ifconfig $DEV2 | grep inet |grep -v inet6 | sed -e 's/^[[:space:]]*//' |cut -d' ' -f2`
# Get default gateway for our devices:
gw1=`route -n | grep $DEV1 | grep '^0.0.0.0' | awk '{ print $2 }'`
gw2=`route -n | grep $DEV2 | grep '^0.0.0.0' | awk '{ print $2 }'`
echo "$DEV1: IP=$ip1 GW=$gw1"
echo "$DEV2: IP=$ip2 GW=$gw2"
### Definition of routes ###
# Check if tables exists, if not -> create them:
if [ -z "`cat /etc/iproute2/rt_tables | grep '^251'`" ] ; then
echo "251 rt_dev1" >> /etc/iproute2/rt_tables
fi
if [ -z "`cat /etc/iproute2/rt_tables | grep '^252'`" ] ; then
echo "252 rt_dev2" >> /etc/iproute2/rt_tables
fi
# Define routing tables:
ip route add default via $gw1 table rt_dev1
ip route add default via $gw2 table rt_dev2
# Create rules:
ip rule add from $ip1 table rt_dev1
ip rule add from $ip2 table rt_dev2
# If we already have a 'nexthop' route, delete it:
if [ ! -z "`ip route show table main | grep 'nexthop'`" ] ; then
ip route del default scope global
fi
# Balance links based on routes:
ip route add default scope global nexthop via $gw1 dev $DEV1 weight 1 nexthop via $gw2 dev $DEV2 weight 1
# Flush cache table:
ip route flush cache
# All done...
ISPs in Pee Sai these days got offer 2X1Gbps fiber broadband. You got 2 wifi routers. You can plug in another 1000BT NIC card into your PC, so 2 LAN cables into your PC, 1 from each router. Then this script RUN ONCE AS ROOT will keep a connection based balanced internet loading for you. I found it written by some Chow Ang Moh, won't work for Ubuntu. I modified 1st 4 lines to get it working.
Why is it good to use 2 WANs? You got 2 public IP addresses instead of 1. So like I am doing some VPN tests or Servers/Clients tests, that need to be realistically simulated, I can put server on 1 WAN, client on the other WAN, I can be fully sure of my tests because they are really 2 separated connections on the real internet. I can test side by side on same desk and be truly sure it will be same when I moved one side to elsewhere on internet.
For this script I tested on firefox opening 10 tabs of youtube full HD videos at the same time. Both WANs loads about 4MB/s. Roughly balanced. Perfect balance can not be expected. if you 2 WANs are different speed / performance or your want to load them at a certain raio, edit the WEIGHTS at Line #53.
Running the script automatically can be done by calling it at /etc/rc.local
https://askubuntu.com/questions/228304/how-do-i-run-a-script-at-start-up
Otherwise the balance setup will be lost after reboot when route tables is rebuilt by system.
I don't start it automatically because I do tests that prefers without balanced WANs some times.