Load Balance your traffic with a script for linux | Linux Operating System

Load Balance your traffic with a script for linux

via Load Balance your traffic with a script for linux | Linux Operating System.

When you have two ISPs you may want to make your linux machine balance the load of your internet access, you can use ip route

The package needed for this is iproute, if you are using debian you can install it with

apt-get install iproute

or download the source of iproute

Let’s use this Load Balance Script which work for Centos, Fedora, Ubuntu, Debian and should work for all distributions.

———–cut here————–

#!/bin/bash

#IPs of the def gateways

IP1=192.168.1.1

IP2=192.168.2.1

REDLOCAL=10.1.1.0/24

IPLOCAL=10.1.1.1

# NICs

IF1=eth0

IF2=eth1

ip rule add from $IP1 lookup T1

ip route add $REDLOCAL via $IPLOCAL table T1

ip route add 0/0 via $IP1 table T1

ip route add from $IP2 lookup T2

ip route add $REDLOCAL via $IPLOCAL table T2

ip route add 0/0 via $IP1 table T1

ip route default equalize nexthop via $IP1 dev $IF1 nexthop via $IP2 dev $IF2

————–cut here ———————

that should do the job, off course make the changes on the variables according to your needs.

[More…]

More Complex Example:

#!/bin/bash -v

#IPs

IP1=190.84.224.246

IP2=190.156.148.160

#ahora los Gateways

P1=190.84.224.1

P2=190.156.148.1

#Las redes

P0_NET=192.168.0.0/16

P1_NET=190.84.224.0/24

P2_NET=190.156.148.0/23

# NICs

IF1=eth0

IF2=eth1

ip route add $P1_NET dev $IF1 src $IP1 table T1

ip route add default via $P1 table T1

ip route add $P2_NET dev $IF2 src $IP2 table T2

ip route add default via $P2 table T2

ip route add $P1_NET dev $IF1 src $IP1

ip route add $P2_NET dev $IF2 src $IP2

#ip route add default via $P1

ip rule add from $IP1 table T1

ip rule add from $IP2 table T2

ip route add $P0_NET dev $IF0 table T1

ip route add $P2_NET dev $IF2 table T1

ip route add 127.0.0.0/8 dev lo table T1

ip route add $P0_NET dev $IF0 table T2

ip route add $P1_NET dev $IF1 table T2

ip route add 127.0.0.0/8 dev lo table T2

route del -net 0.0.0.0 netmask 0.0.0.0 dev $IF1

ip route add default scope global nexthop via $P1 dev $IF1 weight 1 nexthop via $P2 dev $IF2 weight 1

Bad Behavior has blocked 149 access attempts in the last 7 days.