· 8 years ago · Jun 20, 2018, 04:42 PM
1#!/bin/bash
2# Get IP addresses of our devices:
3
4## ip1=`ifconfig ppp0 | grep inet | awk ‘{ print $2 }’ | awk -F: ‘{ print $2 }’`
5## ip2=`ifconfig local | grep inet | awk ‘{ print $2 }’ | awk -F: ‘{ print $2 }’`
6
7RP0=`ip addr show ppp0 | grep inet | awk {'print $4'} | cut -d '/' -f1`
8ET0=`ip addr show local | grep global | awk {'print $2'} | cut -d '/' -f1 | cut -d '.' -f1,2,3`.1
9
10### Definition of routes ###
11
12# Check if tables exists, if not -> create them:
13if [ -z "`cat /etc/iproute2/rt_tables | grep '^251'`" ] ; then
14echo “251 ppp0″ >> /etc/iproute2/rt_tables
15fi
16## if [ -z "`cat /etc/iproute2/rt_tables | grep '^252'`" ] ; then
17## echo “252 eth0″ >> /etc/iproute2/rt_tables
18## fi
19if [ -z "`cat /etc/iproute2/rt_tables | grep '^255'`" ] ; then
20echo “255 local″ >> /etc/iproute2/rt_tables
21fi
22if [ -z "`cat /etc/squid3/squid.conf | grep '^127.0.0.1'`" ] ; then
23echo “127.0.0.1 local″ >> /etc/squid3/squid.conf
24fi
25
26# Define routing tables:
27ip route add default via 10.64.64.64 table ppp0 dev ppp0 #10.64.64.64 default gateway buat ppp
28ip route add default via 127.0.0.1 table local dev local #10.126.15.3 default gateway speedy kantor
29
30# Create rules:
31ip rule add from $RP0 table ppp0
32ip rule add from $ET0 table local
33
34# If we already have a ‘nexthop’ route, delete it:
35if [ ! -z "`ip route show table main | grep 'nexthop'`" ] ; then
36ip route del default scope global
37fi
38
39# Balance links based on routes:
40## ip route add default scope global nexthop via 10.64.64.64 dev ppp0 weight 1 nexthop via 10.126.15.3 dev eth0 weight 2
41ip route add default scope global nexthop via 10.64.64.64 dev ppp0 weight 1 nexthop via 127.0.0.1 dev local weight 2
42
43# Flush cache table:
44ip route flush cache
45
46echo "------------- load Balanced $RP0 => $ET0 "
47ip route show
48route -n
49
50
51# All done…