#!/bin/sh # Script dual ISP and multi LAN plus loadbalancing # split 2 LAN segment to 2 ISP # using 4 nic : 2 nic to 2 isp and 2 nic to LAN and load balancing # mambang 2008 # # ISP 1 TABLE1=1 IF1=eth1 GW1=192.168.1.1 # ISP 2 TABLE2=2 IF2=eth2 GW2=192.168.1.2 splitgateway() { $1 $IF1 $TABLE1 $GW1 $1 $IF2 $TABLE2 $GW2 if [ "$1" = "start" ] then $IP route del default $IP route add default equalize nexthop via $GW1 dev $IF1 nexthop via $GW2 dev $IF2 $IPTABLES -t mangle -A PREROUTING -p ALL -j CONNMARK --restore-mark $IPTABLES -t mangle -A PREROUTING -p ALL -i $IF1 -m mark --mark 0 -j MARK --set-mark $TABLE1 $IPTABLES -t mangle -A PREROUTING -p ALL -i $IF2 -m mark --mark 0 -j MARK --set-mark $TABLE2 $IPTABLES -t mangle -A POSTROUTING -p ALL -o $IF1 -m state --state NEW -j MARK --set-mark $TABLE1 $IPTABLES -t mangle -A POSTROUTING -p ALL -o $IF2 -m state --state NEW -j MARK --set-mark $TABLE2 $IPTABLES -t mangle -A POSTROUTING -p ALL -m state --state NEW -j CONNMARK --save-mark $IPTABLES -t nat -A POSTROUTING -p ALL -o $IF1 -m mark --mark $TABLE1 -j SNAT --to-source $(find_primary_ip $IF1) $IPTABLES -t nat -A POSTROUTING -p ALL -o $IF2 -m mark --mark $TABLE2 -j SNAT --to-source $(find_primary_ip $IF2) fi } IP=/sbin/ip IPTABLES=/sbin/iptables init() { echo "10" > /proc/sys/net/ipv4/route/gc_timeout echo "1" > /proc/sys/net/ipv4/ip_forward IF=$1 TABLE=$2 FWIP=$(find_primary_ip $IF) GWIP=$3 } stop() { init $1 $2 $3 route_stop rule_stop } route_stop() { if [ ! -z "$($IP route show table $TABLE)" ] then $IP route flush table $TABLE fi if [ ! -z "$GW1" ] then $IP route del default $IP route add default via $GW1 fi $IP route flush cache } rule_stop() { $IP rule list | grep "lookup $TABLE" | grep -v "from all" | awk '{print $2" "$3" "$4" "$5" "$6" "$7}' $1 | while read RULE do $IP rule del $RULE done $IP rule list | grep "lookup $TABLE" | grep "from all fwmark" | awk '{print $4" "$5" "$6" "$7}' $1 | while read RULE do $IP rule del $RULE done } start() { init $1 $2 $3 stop $1 $2 $3 route_start rule_start } route_start() { $IP route show table main | grep -Ev ^default | grep -Ev nexthop | while read ROUTE; do $IP route add table $TABLE $ROUTE; done $IP route add table $TABLE default via $GWIP } rule_start() { $IP rule add from $FWIP lookup $TABLE $IP rule add fwmark $TABLE lookup $TABLE for IP_ALIAS in $(find_secondary_ip $IF) do $IP rule add from $IP_ALIAS lookup $TABLE done } find_primary_ip() { if [ ! -z $1 ] then echo $($IP addr show $1 | grep 'inet' | grep -v ":" | awk '{print $2}' | sed -e 's/\/.*//') fi } find_secondary_ip() { if [ ! -z $1 ] then echo $($IP addr show $1 | grep 'inet' | grep "$1:" | awk '{print $2}' | sed -e 's/\/.*//') fi } case $1 in stop) splitgateway stop ;; start) splitgateway start ;; restart) splitgateway stop splitgateway start ;; *) N=/etc/init.d/splitgateway echo "Cara Guna: $N {start|stop|restart}" >&2 exit 1 ;; esac exit 0