Chinaunix首页 | 论坛 | 博客
  • 博客访问: 210282
  • 博文数量: 80
  • 博客积分: 213
  • 博客等级: 入伍新兵
  • 技术积分: 435
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-27 11:13
文章分类

全部博文(80)

文章存档

2012年(46)

2011年(34)

分类:

2011-12-02 18:37:22

这是一个基于 bash 的自动判断线路故障的“伪代码”,根据自己实际需要可以再更改
脚本中用了“位”概念做判断
分别用
00 代表无网络
01 代表 GW1
10 代表 GW2
11 代表 GW1 + GW2
对应十进制分别是 0、1、2、3
先分别 ping,然后用用 case 做判断,因此简化了很多,逻辑显得更加明朗
 
 

#! /bin/bash

echo "BOTH" > /tmp/route_flag

GW1="platinum.3322.org"
GW2="platinum.net.cn"

while :;
do
        FLAG="0"

        if ping -c1 -wa $GW1 &>/dev/null; then
                FLAG=`expr 1 + $FLAG`
        fi

        if ping -c1 -wa $GW2 &>/dev/null; then
                FLAG=`expr 2 + $FLAG`
        fi

        case "$FLAG" in
                0)
                        echo "NONE" > /tmp/route_flag
                        ;;
                1)
                        if [ "`cat /tmp/route_flag`" != "GW1" ]; then
                                echo "使用 GW1 做路由"
                                echo "GW1" > /tmp/route_flag
                        fi
                        ;;
                2)
                        if [ "`cat /tmp/route_flag`" != "GW2" ]; then
                                echo "使用 GW2 做路由"
                                echo "GW2" > /tmp/route_flag
                        fi
                        ;;
                3)
                        if [ "`cat /tmp/route_flag`" != "BOTH" ]; then
                                echo "使用双路由"
                                echo "BOTH" > /tmp/route_flag
                        fi
                        ;;
        esac

        sleep 5
done

阅读(1333) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~