All pastes #988096 Raw Edit

Anonymous

public text v1 · immutable
#988096 ·published 2008-04-17 07:04 UTC
rendered paste body
#!/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
#andaian ip eth1=192.168.2.2 dan eth2=192.168.3.2 dan ip eth0=192.168.1.1
#http request akan di forwardkan ke GW2

TABLE1=1			
   IF1=eth1			
   GW1=192.168.2.1		

# ISP 2
TABLE2=2			
   IF2=eth2			
   GW2=192.168.3.1

#Network LAN
IF3=eth0
IP3=192.168.1.1
LAN1=192.168.1.0/24


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 weight 1 nexthop via $GW2 dev $IF2 weight 1

	 $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)
	 $IPTABLES -t nat -A PREROUTING -i $IF3 -s $LAN1 -p tcp -m tcp --dport http -j DNAT --to $GW2
	 $IPTABLES -t nat -A POSTROUTING -d $LAN1 -p tcp -m tcp --dport http -j SNAT --to $GW2
 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