免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3541 | 回复: 7
打印 上一主题 下一主题

求完整shell编程_网卡状态侦测及故障自动切换 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-05-09 11:19 |只看该作者 |倒序浏览
20可用积分
检测状态指令
1)ping 222.134.53.102 -c5                                          
     PING 222.134.53.102 (222.134.53.102) 56(84) bytes of data.
     64 bytes from 222.134.53.102: icmp_seq=0 ttl=61 time=2.23 ms
     64 bytes from 222.134.53.102: icmp_seq=1 ttl=61 time=2.10 ms
     64 bytes from 222.134.53.102: icmp_seq=2 ttl=61 time=2.91 ms
     64 bytes from 222.134.53.102: icmp_seq=3 ttl=61 time=2.91 ms
     64 bytes from 222.134.53.102: icmp_seq=4 ttl=61 time=2.91 ms
            --- 222.134.53.102 ping statistics ---
     5 packets transmitted, 5 received, 0% packet loss, time 3999ms

   #如果time<200  并且 packet loss < 20%那么statusA =1
    否则statusA =0

2)ping 219.146.0.130 -c5
    ping 219.146.0.130
    PING 219.146.0.130 (219.146.0.130) 56(84) bytes of data.
    64 bytes from 219.146.0.130: icmp_seq=0 ttl=243 time=6.3 ms
    64 bytes from 219.146.0.130: icmp_seq=1 ttl=243 time=6.4 ms
    64 bytes from 219.146.0.130: icmp_seq=2 ttl=243 time=6.2 ms
    64 bytes from 219.146.0.130: icmp_seq=3 ttl=243 time=6.5 ms
    64 bytes from 219.146.0.130: icmp_seq=4 ttl=243 time=6.5 ms

         --- 219.146.0.130 ping statistics ---
      5 packets transmitted, 5 received, 0% packet loss, time 3999ms

        #如果time<200  并且 packet loss < 20%那么statusB =1
    否则statusB =0
路由表脚本
    default           #默认路由表
      ip route replace 222.240.0.0/13 via 222.174.245.5
      ip route replace 0/0 via 119.185.0.3

      chgcnc              #因为电信不正常,切换至网通路由
      ip route replace 222.240.0.0/13 via 119.185.0.3
     

      chgtel              #因为电信正常,切换电信路由
      ip route replace 222.240.0.0/13 via 222.174.245.5
   
     chgcncdf           #更改默认路由至网通
     ip route replace 0/0 via 119.185.0.3

     chgteldf          #更改默认路由至电信
     ip route replace 0/0 via 119.185.0.3

    网关文件   gatecnc  gate tel
      cat   gatecnc
      119.185.0.3
      
      cat   gatetel
      222.174.245.5
   
检测路由状态指令
    route -F
   121.204.0.0     119.185.0.3    255.252.0.0     UG    0      0        0 eth1
   222.240.0.0     119.185.0.3    255.224.0.0     UG    0      0        0 eth1
   60.160.0.0      119.185.0.3    255.224.0.0     UG    0      0        0 eth1
   default         119.185.0.3    0.0.0.0         UG    0      0        0 eth1


假如  statusA =1 并且 statusB =1 那么检测当前路由表状态
       如果grep得到  222.240.0.0 的 默认路由是 gatetel 并且 default 的默认路由是 gatecnc 那么什么也不做
                否则  假如  grep得到  222.240.0.0 的 默认路由是 gatecnc  那么 切换电信路由 sh chgtel  
                                假如  grep得到  0.0.0.0 的 默认路由是 gatetel  那么 切换默认路由 sh  chgcncdf
假如  statusA =1 并且 statusB =0那么检测当前路由表状态
       如果grep得到  222.240.0.0 的 默认路由是 gatecnc 并且 default 的默认路由是 gatecnc 那么什么也不做
                否则  假如  grep得到  222.240.0.0 的 默认路由是 gatetel  那么 切换电信路由 sh chgtel  
                                假如  grep得到  0.0.0.0 的 默认路由是 gatetel  那么 切换默认路由 sh  chgteldf

假如  statusA =0 并且 statusB =1那么检测当前路由表状态
       如果grep得到  222.240.0.0 的 默认路由是 gatetel 并且 default 的默认路由是 gatetel 那么什么也不做
                否则  假如  grep得到  222.240.0.0 的 默认路由是 gatecnc  那么 切换电信路由 sh chgcnc  
                                假如  grep得到  0.0.0.0 的 默认路由是 gatecnc  那么 切换默认路由 sh  chgcncdf


                                
                                 
难点 1)如何确定 statusA  statusb值  
     2)检测当前路由表状态
         设置变量 routetochg        routedefault
                 route -F | grep 222.240.0.0
                         route -F | grep 222.240.0.0 得到的结果
                          222.240.0.0     119.185.0.3    255.248.0.0     UG    0      0        0 eth1

                         route -F | grep 0.0.0.0
                         route -F | grep 0.0.0.0 得到的结果
                         default         119.185.0.93    0.0.0.0         UG    0      0        0 eth1

                 如果这样的结果的话routetochg = 119.185.0.3  routedefault= 119.185.0.3

                 route -F | grep 222.240.0.0
                         route -F | grep 222.240.0.0 得到的结果
                          222.240.0.0     222.174.245.5    255.248.0.0     UG    0      0        0 eth1

                         route -F | grep 0.0.0.0
                         route -F | grep 0.0.0.0 得到的结果
                         default         119.185.0.93    0.0.0.0         UG    0      0        0 eth1

                 如果这样的结果的话routetochg = 222.174.245.5  routedefault= 222.174.245.5
               
         那么请问如何用awk检测并赋值?

       3)完整程序怎么写

论坛徽章:
0
2 [报告]
发表于 2009-05-09 11:27 |只看该作者

有大哥给了这段指令 ,执行结果不正确

#!/bin/sh


ping 202.102.152.3 -c 5 > /dev/null

if [[ "$?" == "1" ]]; then
    statusA=1;
    echo $statusA
else
    statusA=0;
    echo $statusA

fi

论坛徽章:
0
3 [报告]
发表于 2009-05-09 11:37 |只看该作者

另类解决办法

 逻辑连接中断(比如网关的线路出了问题等)
  可以通过每10s发送一个小的ping包到网关,看是否有回应,一般网关都是允许ping的。
  这个可以通过一个expect程序(netwatch.exp)来完成,下面这个脚本发送一个字节为1的ping包给网关,并分析ping的输出信息,如果为 0% packet loss,表示正常,以0退出。如果超时(1秒钟),以1退出。
  #!/usr/bin/expect
  set target [lindex $argv 0]
  set timeout 1
  spawn ping -c 1 -s 1 $target
  expect {
  " 0% packet loss" {
  exit 0
  }
  timeout {
  exit 1
  }
  }
  比如网关1为192.168.1.1,网关2为192.168.2.1,那么要监测这两个网关的话,可以这样:
  #!/bin/sh
  netwatch.exp 192.168.1.1
  result1=$?
  netwatch.exp 192.168.2.1
  result2=$?
  if [ "$result1" eq 0 ] && [ "$result2" eq 0 ] ; then
  # 如果上次检测时连接丢失,而此次恢复正常
  if [ -e /var/run/lost-connection ] ; then
  do some recovery action here ...
  rm /var/run/lost-connection
  fi
  # 如果网关1不可达
  elif [ "$result1" ne 0 ] ; then
  change default route to 192.168.2.1
  do some action here, such as iptables NAT
  # 创建连接丢失标识
  touch /var/run/lost-connection
  # 如果网关2不可达
  elif [ "$result2" ne 0 ] ; then
  change default route to 192.168.1.1
  do some action here, such as iptables NAT
  # 创建连接丢失标识
  touch /var/run/lost-connection
  fi

我的调试环境里边没有 #!/usr/bin/expect ,无法调试,有大哥知道吗

[ 本帖最后由 razar 于 2009-5-9 11:42 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2009-05-09 13:17 |只看该作者
  1. ping 172.24.149.1 -c 5 | grep 'packets' | awk -F"[,%]" '{len=length($NF);print $3,$NF,len,substr($NF,6,len-7)}'
复制代码

其中print $3就是loss的x%的x
substr($NF,6,len-7)就是time xms的X

论坛徽章:
0
5 [报告]
发表于 2009-05-09 13:23 |只看该作者
  1. #!/bin/bash
  2. t_ipA="222.134.53.102"
  3. t_ipB="219.146.0.130 "
  4. ping $t_ipA -c5 >/tmp/pingA.log
  5. ping $t_ipB -c5 >/tmp/pingB.log
  6. lossA=`awk '/packets transmitted/{print $6+0}' /tmp/pingA.log`
  7. timeA=`awk '/packets transmitted/{sub(/ms/,"",$NF);print $NF}' /tmp/pingA.log`
  8. lossB=`awk '/packets transmitted/{print $6+0}' /tmp/pingB.log`
  9. timeB=`awk '/packets transmitted/{sub(/ms/,"",$NF);print $NF}' /tmp/pingB.log`
  10. rm -rf /tmp/pingA.log /tmp/pingB.log

  11. if [ $timeA -lt 200 ] && [ $lossA -lt 20 ];then
  12.   statusA=1
  13. else
  14.   statusA=0
  15. fi
  16. if [ $timeB -lt 200 ] && [ $lossB -lt 20 ];then
  17.   statusB=1
  18. else
  19.   statusB=0
  20. fi

  21. route -F >/tmp/route.log
  22. r_222=`awk '$1=="222.240.0.0"{print $2}' /tmp/route.log`
  23. r_de=`awk '$1=="default"{print $2}' /tmp/route.log`
  24. r_0=`awk '$1=="0.0.0.0"{print $2}' /tmp/route.log`
  25. g_cnc=`cat gatecnc`
  26. g_tel=`cat gatetel`
  27. rm -rf /tmp/route.log

  28. if [ $statusA -eq 1 ] && [ $statusB -eq 1 ];then
  29.   if [ "$r_222" = "g_tel" ] && [ "$r_de" = "$g_cnc" ];then
  30.     exit 0
  31.   else
  32.     [ "$r_222" = "$g_cnc" ] && sh chgtel
  33.     [ "$r_0" = "$g_tel" ] && sh chgcncdf
  34.   fi
  35. fi
  36. #后两个自己写
复制代码

[ 本帖最后由 ywlscpl 于 2009-5-9 14:21 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2009-05-09 13:28 |只看该作者
  1. [root@mip-123456 ~]# statusA=$(ping 172.24.149.1 -c 5 | grep 'packets' | awk -F"[,%]" '{len=length($NF);print $3,substr($NF,6,len-7)}' | awk '$2<20&&$NF<200{print "1"}{print "0"}')
  2. [root@mip-123456 ~]# echo $statusA
  3. 0
复制代码

论坛徽章:
0
7 [报告]
发表于 2009-05-09 14:47 |只看该作者
我越来越喜欢awk了
向ywlscpl 哥哥学习,也谢谢大家
我去试试

论坛徽章:
0
8 [报告]
发表于 2009-12-25 22:58 |只看该作者
问题解决了?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP