使用道具 举报
可以通过每10s发送一个小的ping包到网关,看是否有回应,一般网关都是允许ping的。 这个可以通过一个expect程序(netwatch.exp)来完成,下面这个脚本发送一个字节为1的ping包给网关,并分析ping的输出信息,如果为 0% packet loss,表示正常,以0退出。如果超时(1秒钟),以1退出。 CODE:[Copy to clipboard]#!/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,那么要监测这两个网关的话,可以这样: CODE:[Copy to clipboard]#!/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
本版积分规则 发表回复 回帖并转播 回帖后跳转到最后一页
GMT+8, 2025-2-12 04:12 , Processed in 0.028377 second(s), 15 queries .
© 2001-2025 Discuz! Team. Powered by Discuz! X3.5.