找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
广告投放联系QQ68610888
查看: 11562|回复: 74

华硕路由和软路由OpenWrt组成双机热备高可用,再也不怕折腾断网了

  [复制链接]
发表于 2021-5-16 02:28 | 显示全部楼层 |阅读模式
本帖最后由 zxslh 于 2023-9-12 00:14 编辑

买了软路由折腾,家里经常被我折腾断网,为了不被骂在网上学习好长时间搞出这么个东西。无感动态切换路由出口,怎么折腾都没事儿了。
纯脚本文件,理论上只要支持ssh就能使用,不限于OpenWrt或者asus,只是其它类型路由需要修改下参数。




需要图形界面的发消息给我。

也可以使用下面脚本,功能完全一样。

接线:华硕wan和软路由OpenWrt的wan均接入电信猫并pppoe拨号,两个路由的lan口均接入傻瓜交换机,并设置ip在同一子网。

OpenWrt请在/root/目录下新建op_float.sh文件,修改权限777,并添加下列代码(ip和子网掩码请根据自己情况修改):
  1. #/root/op_float.sh

  2. FLOAT_IP="10.10.1.1"                        #浮动ip
  3. OPENWRT_WAN="10.10.2.2"             #软路由WAN口状态探测ip
  4. ASUS_WAN="10.10.3.2"                     #华硕WAN口状态探测ip

  5. a=$(/sbin/ip addr|grep pppoe-wan)    #本机WAN口拨号状态
  6. b1=$(/sbin/ip addr|grep br-lan:1)        #浮动ip
  7. b2=$(/sbin/ip addr|grep br-lan:2)        #本机WAN口拨号状态联动ip
  8. e=$(uci get dhcp.lan.ignore)               #本机DHCP服务器状态

  9. #master=$(uci get floatip.status.switch)
  10. master=1                                           #值1为主用路由标记,至少且只能标记一个主用路由
  11. #uci set floatip.status.switch=0

  12. if [ "$a" ] ; then
  13.     if [ ! "$b2" ] ; then
  14.         /sbin/ifconfig br-lan:2 $OPENWRT_WAN netmask 255.255.252.0 up
  15.     fi
  16.     if [ "$master" = 1 ] ; then
  17.         c=0
  18.     else
  19.         c=$(/bin/ping $ASUS_WAN -w 1 -c 1|grep round-trip|wc -l)
  20.     fi
  21. else
  22.     if [ "$b2" ] ; then
  23.         /sbin/ifconfig br-lan:2 down
  24.     fi
  25.     c=$(/bin/ping $ASUS_WAN -w 1 -c 1|grep round-trip|wc -l)
  26.     if [ "$c" = 0 ] ; then
  27.         ifdown wan; sleep 1; ifup wan
  28.         c=1
  29.     fi
  30. fi

  31. if [ "$c" = 0 ] ; then
  32.     if [ ! "$b1" ] ; then
  33.         /sbin/ifconfig br-lan:1 $FLOAT_IP netmask 255.255.252.0 up
  34.         arping -c 5 -A -I br-lan $FLOAT_IP
  35.     fi
  36.     if [ "$e" = 1 ] ; then
  37.         uci set dhcp.lan.ignore="0"
  38.         uci commit dhcp
  39.         /etc/init.d/dnsmasq restart
  40.     fi
  41. else
  42.     if [ "$b1" ] ; then
  43.         /sbin/ifconfig br-lan:1 down
  44.     fi
  45.     if [ "$e" != 1 ] ; then
  46.         uci set dhcp.lan.ignore="1"
  47.         uci commit dhcp
  48.         /etc/init.d/dnsmasq restart
  49.     fi
  50. fi
复制代码
接口--LAN--DHCP服务器--高级设置--DHCP选项,添加 3,10.10.1.1和6,10.10.1.1
—————————————————————————————————————————————————

ASUS请在/jffs/目录下新建as_float.sh文件,修改权限777,并添加下列代码(ip和子网掩码请根据需要修改):
  1. #/jffs/as_float.sh

  2. FLOAT_IP="10.10.1.1"
  3. OPENWRT_WAN="10.10.2.2"
  4. ASUS_WAN="10.10.3.2"

  5. a=$(/usr/sbin/ip addr|grep ppp0)
  6. b1=$(/usr/sbin/ip addr|grep br0:1)
  7. b2=$(/usr/sbin/ip addr|grep br0:2)
  8. e=$(nvram get dhcp_enable_x)

  9. #master=$(nvram get floatip_master)
  10. master=0
  11. #nvram set floatip_master=1

  12. if [ "$a" ] ; then
  13.     if [ ! "$b2" ] ; then
  14.         /sbin/ifconfig br0:2 $ASUS_WAN netmask 255.255.252.0 up
  15.     fi
  16.     if [ "$master" = 1 ] ; then
  17.         c=0
  18.     else
  19.         c=$(/bin/ping $OPENWRT_WAN -w 1 -c 1|grep round-trip|wc -l)
  20.     fi
  21. else
  22.     if [ "$b2" ] ; then
  23.         /sbin/ifconfig br0:2 down
  24.     fi
  25.     c=$(/bin/ping $OPENWRT_WAN -w 1 -c 1|grep round-trip|wc -l)
  26.     if [ "$c" = 0 ] ; then
  27.         pppd file /tmp/ppp/options.wan0
  28.         c=1
  29.     fi
  30. fi

  31. if [ "$c" = 0 ] ; then
  32.     if [ ! "$b1" ] ; then
  33.         /sbin/ifconfig br0:1 $FLOAT_IP netmask 255.255.252.0 up
  34.         arping -c 5 -A -I br0 $FLOAT_IP
  35.     fi
  36.     if [ "$e" = 0 ] ; then
  37.         nvram set dhcp_enable_x=1
  38.         nvram commit
  39.         service restart_dnsmasq
  40.     fi
  41. else
  42.     if [ "$b1" ] ; then
  43.         /sbin/ifconfig br0:1 down
  44.     fi
  45.     if [ "$e" != 0 ] ; then
  46.         nvram set dhcp_enable_x=0
  47.         nvram commit
  48.         service restart_dnsmasq
  49.     fi
  50. fi
复制代码

内部网络--DHCP服务器,修改网关为10.10.1.1,DNS为10.10.1.1
——————————————————————————————————————————————

OpenWrt在系统--计划任务添加:
  1. * * * * * sh /root/op_float.sh
  2. * * * * * sleep 20; sh /root/op_float.sh
  3. * * * * * sleep 40; sh /root/op_float.sh
复制代码



ASUS添加计划任务:
文件位置:
/var/spool/cron/crontabs/yourname
添加代码:
  1. * * * * * sleep 10; sh /jffs/as_float.sh
  2. * * * * * sleep 30; sh /jffs/as_float.sh
  3. * * * * * sleep 50; sh /jffs/as_float.sh
复制代码

设置计划任务重启生效:
文件位置:
/jffs/scripts/init-start
添加代码:
  1. mkdir -p /jffs/scripts/crontabs && rm -rf /var/spool/cron/crontabs && ln -sf /jffs/scripts/crontabs /var/spool/cron/crontabs && (pidof crond || crond -bS)
复制代码












本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
我的恩山、我的无线 The best wifi forum is right here.
发表于 2021-5-16 06:59 | 显示全部楼层
生活是自己的,尽情打扮,尽情可爱呦
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-5-16 07:00 | 显示全部楼层
看看怎么折腾
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-5-16 07:21 来自手机 | 显示全部楼层
谢谢分享,谢谢分享
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-5-16 09:02 | 显示全部楼层
华硕路由和软路由OpenWrt组成高可用,再也不怕折腾断网了
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-5-16 09:10 | 显示全部楼层
华硕路由和软路由OpenWrt组成高可用,再也不怕折腾断网了
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-5-16 09:28 | 显示全部楼层
点开看看,,,,!!!!!!!!
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-5-16 15:01 | 显示全部楼层
看看怎么折腾
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-5-16 15:29 | 显示全部楼层
新思路,学习一下。
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-5-16 16:48 | 显示全部楼层
谢谢分享!
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-5-16 18:26 | 显示全部楼层
路由器组网高可用
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-5-16 21:38 | 显示全部楼层
本帖最后由 sy1989 于 2021-5-16 21:39 编辑

楼主脚本不错,但是应该可以直接用vrrp自动切换的吧
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-5-16 22:26 | 显示全部楼层
看看怎么折腾
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| 发表于 2021-5-16 23:07 来自手机 | 显示全部楼层
sy1989 发表于 2021-5-16 21:38
楼主脚本不错,但是应该可以直接用vrrp自动切换的吧

华硕没有vrrp
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2021-5-17 00:15 | 显示全部楼层
谢分享。看看是如何组合的,学习学习。
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

有疑问请添加管理员QQ86788181|手机版|小黑屋|Archiver|恩山无线论坛(常州市恩山计算机开发有限公司版权所有) ( 苏ICP备05084872号 )

GMT+8, 2024-4-20 08:40

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

| 江苏省互联网有害信息举报中心 举报信箱:js12377 | @jischina.com.cn 举报电话:025-88802724 本站不良内容举报信箱:68610888@qq.com 举报电话:0519-86695797

快速回复 返回顶部 返回列表