Для сохранности оставлю здесь скрипт автопереключалки инета для трех провайдеров с проверкой доступности ресурсов. Правда, в рабочем порядке он еще не испробован, поэтому применять с осторожностью.
##
## /etc/iproute2/rt_tables:
## 101 fc
## 102 adsl
## 103 gsm
##
##
##Localnet
L1_IF="eth77"
L1_IP="192.168.77.250"
L1_NET="192.168.77.0/24"
##FC
P1_IF="eth77"
P1_GW="192.168.77.159"
P1_TBL="fc"
##ADSL
P2_IF="ppp200"
P2_GW="`ifconfig ppp200 |egrep -o 'addr:[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | awk -F: '{ print $2}'`"
P2_TBL="adsl"
##Cellular 3G
P3_IF="eth77"
P3_GW="192.168.77.200"
P3_TBL="gsm"
## hosts to ping
TEST1="77.88.8.8"
TEST2="8.8.8.8"
##Default for fallback feature
DEF=${P1_GW}
## fullfill iproute2 tables
function fill_p1
{
/bin/ip r flush table ${P1_TBL}
/bin/ip r a default via ${P1_GW} table ${P1_TBL}
/bin/ip r a ${L1_NET} dev ${L1_IF} src ${L1_IP} scope link table ${P1_TBL}
/bin/ip rule a fwmark 1 lookup ${P1_TBL}
}
function fill_p2
{
/bin/ip r flush table ${P2_TBL}
/bin/ip r a default dev ${P2_IF} table ${P2_TBL}
/bin/ip r a ${L1_NET} dev ${L1_IF} src ${L1_IP} scope link table ${P2_TBL}
/bin/ip rule a fwmark 2 lookup ${P2_TBL}
}
function fill_p3
{
/bin/ip r flush table ${P3_TBL}
/bin/ip r a default via ${P3_GW} table ${P3_TBL}
/bin/ip r a ${L1_NET} dev ${L1_IF} src ${L1_IP} scope link table ${P3_TBL}
/bin/ip rule a fwmark 3 lookup ${P3_TBL}
}
## checking internet availability via different providers
function get_status
{
CUR_GW=`/bin/ip r sh | grep def | egrep -o [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+`
/bin/ping -W 1 -i 0.5 -c 2 -m 1 ${TEST1} > /dev/null
P1_STAT="$?"
/bin/ping -W 1 -i 0.5 -c 2 -m 2 ${TEST1} > /dev/null
P2_STAT="$?"
/bin/ping -W 1 -i 0.5 -c 2 -m 3 ${TEST1} > /dev/null
P3_STAT="$?"
if [ "$P1_STAT" -ne "0" ] && [ "$P2_STAT" -ne "0" ] && [ "$P3_STAT" -ne "0" ]
then
echo "${TEST1} failed, trying ${TEST2}"
/bin/ping -W 1 -i 0.5 -c 2 -m 1 ${TEST2} > /dev/null
P1_STAT="$?"
/bin/ping -W 1 -i 0.5 -c 2 -m 2 ${TEST2} > /dev/null
P2_STAT="$?"
/bin/ping -W 1 -i 0.5 -c 2 -m 3 ${TEST2} > /dev/null
P3_STAT="$?"
fi
}
##main
echo "filling rt_tables"
fill_p1
fill_p2
fill_p3
echo "cheking gateways"
get_status
echo "P1: $P1_STAT P2: $P2_STAT P3: $P3_STAT"
echo "action: "
if [ "$P1_STAT" -eq "0" ] && [ "$CUR_GW" = "$DEF" ]
then
echo "do nothing"
exit 0
fi
if [ "$P1_STAT" -eq "0" ] && [ "$CUR_GW" != "${DEF}" ]
then
echo "restoring to $P1_TBL"
/sbin/ip r ch default via ${P1_GW}
exit 0
fi
if [ "$P2_STAT" -eq "0" ] && [ -n "$P2_GW" ]
then
echo "switching to $P2_TBL"
/bin/ip r ch default via ${P2_GW}
exit 0
fi
if [ "$P3_STAT" -eq "0" ]
then
echo "switching to $P3_TBL"
/bin/ip r ch default via ${P3_GW}
exit 0
else
echo "swag!"
fi
Комментариев нет:
Отправить комментарий