- Блог пользователя administrator
- Комментировать
- 156260 reads
Метки: Centos
#!/bin/sh -x
# LAN interface
IF0="eno50332176"
# WAN interface 1
IF1="eno33554952"
# WAN interface 2
IF2="eno16777728"
IP1="`ifconfig $IF1 | grep inet | awk '{print $2}'`"
IP2="`ifconfig $IF2 | grep inet | awk '{print $2}'`"
# gateway 1
P1="192.168.156.56"
# gateway 2
P2="192.168.1.1"
# LAN netmask
P0_NET="10.10.0.0/24"
# WAN1 netmask
P1_NET="192.168.156.0/24"
# WAN2 netmask
P2_NET="192.168.1.0/24"
TBL1="provider1"
TBL2="provider2"
CHKIP1="8.8.8.8"
CHKIP2="8.8.4.4"
# Realtive weight of channels bandwidth
W1="2"
W2="1"
[root@router02 balance]#
[root@router02 balance]# cat ./routing-boot
#!/bin/sh -x
. /etc/balance/vars
ip route add $P1_NET dev $IF1 src $IP1 table $TBL1
ip route add default via $P1 table $TBL1
ip route add $P2_NET dev $IF2 src $IP2 table $TBL2
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 route add default via $P2 table $TBL2
ip route add $P0_NET dev $IF0 table $TBL1
ip route add $P2_NET dev $IF2 table $TBL1
ip route add 127.0.0.0/8 dev lo table $TBL1
ip route add $P0_NET dev $IF0 table $TBL2
ip route add $P1_NET dev $IF1 table $TBL2
ip route add 127.0.0.0/8 dev lo table $TBL2
ip route add $CHKIP1 dev $IF1 via $P1
ip route add $CHKIP2 dev $IF2 via $P2
ip rule add from $IP1 table $TBL1
ip rule add from $IP2 table $TBL2
[root@router02 balance]#
[root@router02 balance]# cat ./check
#!/bin/sh
. /etc/balance/vars
OLDIF1=0
OLDIF2=0
. /etc/balance/routing
while true; do
ping -c 3 $CHKIP1 -I $IF1 > /dev/null
if [ $? -ne 0 ]; then
echo "failed IF1!"
NEWIF1=0
else
NEWIF1=1
fi
ping -c 3 $CHKIP2 -I $IF2 > /dev/null
if [ $? -ne 0 ]; then
echo "failed IF2!"
NEWIF2=0
else
NEWIF2=1
fi
if (( ($NEWIF1!=$OLDIF1) || ($NEWIF2!=$OLDIF2) )); then
echo "changing routes"
if (( ($NEWIF1==1) && ($NEWIF2==1) )); then
echo "both channels"
ip route change default via $P1 dev $IF1
ip route flush cache
elif (( ($NEWIF1==1) && ($NEWIF2==0) )); then
echo "first channel"
ip route change default via $P1 dev $IF1
ip route flush cache
elif (( ($NEWIF1==0) && ($NEWIF2==1) )); then
echo "second channel"
ip route change default via $P2 dev $IF2
ip route flush cache
elif (( ($NEWIF1==0) && ($NEWIF2==0) )); then
echo "nothing on two channels"
ip route change default via $P1 dev $IF1
ip route flush cache
fi
else
echo "not changed"
fi
OLDIF1=$NEWIF1
OLDIF2=$NEWIF2
sleep 5
done
[root@router02 balance]#
[root@router02 balance]# cat /etc/rc.d/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In constrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
/etc/balance/routing-boot
/etc/balance/iptables start
/etc/balance/check &
[root@router02 balance]#