快捷搜索:   nginx

LINUX双线自动切换脚本3.5

较之前的版本做了稍微的修改,优化了部分命令行,增加了LOG功能,方便管理员对线路状况的掌握.
特别鸣谢:天之蓝小样给予的支持.
您在使用时,请尊重别人的劳动成果,保留版本信息.谢谢.

--------------------------------------------------------------------------------

--------------------------------------------------------------------------------

#!/bin/sh
#=============================
#Script Name:Daker`s both WAN guard Shell for Linux
#Author: Daker
#Version: 3.5
#RUL:http://dakerbt.spaces.live.com
#=============================
#=============================
#Both WAN IPaddress/gateway config
tctip=""
tctgw=""
cncip=""
cncgw=""
lange=""
#==============================
#==============================
#Both WAN Log config
entry="100"
#==============================

if [ -n "`route -n | grep 0.0.0.0 | grep UG | grep $tctgw`" ]; then   
line="checl"
else   
line="cnc"
fi
while true;
do
tctok="$(ping -c 1 $tctgw|grep "100% packet loss"|wc -l)"
cncok="$(ping -c 1 $cncgw|grep "100% packet loss"|wc -l)"
if [ $tctok -eq 0 ] && [ $cncok -eq 0 ] && [ $line != "checl" ]; then
echo "start checl"
date '+#Both network connected. %D %X' >>\log\checl.log
   if [ -n "`route -n | grep 0.0.0.0 | grep UG | grep $cncgw`" ]; then
      ip r re 0/0 via $tctgw
   fi
   if [ -n "`route -n | grep $tctip | grep UGH | grep $cncgw`" ]; then
      route del -host $tctip gw $cncgw
   fi
   if [ -n "`route -n | grep $cncip | grep UGH | grep $tctgw`" ]; then
      route del -host $cncip gw $tctgw
   fi
line="checl"
elif [ $tctok -eq 1 ] && [ $line != "cnc" ]; then
echo "start cnc"
date '+#disconnected from tct. %D %X' >>\log\checl.log
     if [ -n "`route -n | grep 0.0.0.0 | grep UG | grep $tctgw`" ]; then
        ip r re 0/0 via $cncgw
     fi
     if [ -n "`route -n | grep $cncip | grep UGH | grep $tctgw`" ]; then
        route del -host $cncip gw $tctgw
     fi
     if [ -z "`route -n | grep $tctip | grep UGH | grep $cncgw`" ]; then
        route add -host $tctip gw $cncgw
     fi
line="cnc"
elif [ $cncok -eq 1 ] && [ $line != "tct" ]; then
echo "start tct"
date '+#disconnected from cnc. %D %X' >>\log\checl.log
     if [ -n "`route -n | grep 0.0.0.0 | grep UG | grep $cncgw`" ]; then
        ip r re 0/0 via $tctgw
     fi
     if [ -n "`route -n | grep $tctip | grep UGH | grep $cncgw`" ]; then
        route del -host $tctip gw $cncgw
     fi
     if [ -z "`route -n | grep $cncip | grep UGH | grep $tctgw`" ]; then
        route add -host $cncip gw $tctgw
     fi
line="tct"
fi
if [ -e \log\checl.log ];then
logok="$(grep "#" \log\checl.log|wc -l)"
   if [ $logok -ge $entry ];then
      rm \log\checl.log -f
   fi
fi
sleep "$lange"s
done

顶(0)
踩(0)

您可能还会对下面的文章感兴趣:

最新评论