故障时切换网关 (WRT)

故障时切换网关 (WRT)

我有两个 WRT 路由器,每个路由器都连接到自己的无线网关(手机和街对面的热点)。路由器 LAN 通过 WDS 桥接并位于同一子网上,因此客户端可以连接到任一网关(取决于其网络设置)。我正在尝试实现一个脚本,以便当路由器 2 的网关无法访问互联网时(当我们关闭手机上的热点或当手机不在家里时),路由器 2 的网关更改为指向路由器 1。当路由器 2 的网关恢复正常时,需要更改回来。

如此处记录的具有故障转移功能的双网关,我找到了下面的脚本,但是将其实施到启动脚本中后,当我断开主网关的连接时,它不会更改我的网关。

我已经阅读并阅读了有关多个网关以及具有不同指标的不同静态路由的信息,当其中一个无法访问时,它会默认使用另一条路由。如果是这样,并且可以在中继器桥接模式下在 WRT 路由器上实现,那么下面的脚本类型甚至是必要的,有人会指出我正确的方向吗?谢谢。

INTERVAL=6 
PACKETS=2 
USINGWAN=0 
GW=br0 
WAN1GW=192.168.3.4 
WAN2GW=192.168.3.1 
DNS1=202.88.149.25 
CH=1 

while sleep $INTERVAL 
do 
TARGET=`ip route | awk '/default via/ {print $3}'` 
if [ "$WAN1GW" = "$TARGET" ]; then 
USINGWAN=1; 
else if [ "$WAN2GW" = "$TARGET" ]; then 
USINGWAN=2; 
fi; 
fi 
RET=`ping -c $PACKETS $DNS1 2>/dev/null | awk '/packets received/ {print $4}'` 
if [ "$RET" -lt "$CH" ]; then 
if [ "$USINGWAN" = "1" ]; then 
ip route delete default via $WAN1GW dev $GW 
ip route add default via $WAN2GW dev $GW 
sleep 2 
USINGWAN=2 
echo "Changed active WAN port to 2!" 
fi 
fi 
if [ "$USINGWAN" = "2" ]; then 
WAN1STAT=`ping -c $PACKETS $DNS1 2>/dev/null | awk '/packets received/ {print $4}'` 
if [ "$WAN1STAT" = "$PACKETS" ]; then 
ip route delete default via $WAN2GW dev $GW 
ip route add default via $WAN1GW dev $GW 
sleep 2 
USINGWAN=1 
echo "Changed active WAN port to 1!" 
fi 
fi 
done; 

Below are the creator/users comments:
Used the code above with ping to only one ip, added a static route in gateway 192.168.3.1 for ping ip to be always routed through 192.168.3.4 as a result of which in case of ping failure gateway changed to secondary gateway and once ping was back live it switched to primary... Also used -lt variable to make sure that gateway changed on at least 2 ping failures and not 1.

相关内容