# ./configure && make && make install
# ethtool eth0
....
Link detected: yes
#!/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
}
}
#!/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