群晖 定期自动重启容器 群晖定时重启服务

家中网络连接示意图(已省略接在主路由上的光猫):

群晖 定期自动重启容器 群晖定时重启服务_重启

基本情况

  • 联通宽带(光猫改桥接,主路由拨号)
  • 主路由红米AC2100(RM2100) 老毛子系统(padavan  3.4.3.9-099_20200619)IP:10.0.0.1
  • NAS 蜗牛星际A款单口  群晖 7.0.1-42218   IP:10.0.0.2
  • 旁路由 群晖自带虚拟机 分配1C1G   iStoreOS (OpenWrt 21.02.1 2022042919) IP:10.0.0.21

旁路由服务

  • P***W***    
  • AdGuard Home     广告过滤
  • SmartDNS     配合AD

需求1、NAS每晚 11:30 自动关机,此时旁路由也会关机,因为主路由的网关和DNS都是旁路由地址,旁路由关机后,其他家人使用 WiFi 无法上网。
2、平常旁路由出现突发状况掉线,WiFi 也无法上网。

解决在主路由中创建自定义脚本,通过 Crontab 监测旁路由是否在线。
场景1:晚上 NAS 关机,旁路由关机后,主路由监测到旁路由不存在,随即将主路由的网关和 DNS 切换到自身,早上 NAS 开机后,主路由监测到旁路由上线,此时将主路由网关和 DNS 切换为旁路由地址。
场景2:主路由中实时监测旁路由是否在线,不在线时立即将主路由的网关和 DNS 切换到主路由本身,旁路由恢复在线时,随即切换回去。

Padavan自定义脚本

#!/bin/bash default_gateway='10.0.0.1' #主路由 IP auxiliary_gateway='10.0.0.21' #旁路由 IP (需设置的主路由网关 地址) up_gateway='10.0.0.21' #旁路由 IP (需设置的主路由 DNS 地址) check_ip_available(){ #使用 ping 命令检测旁路由是否在线 ping -c 3 $1 | grep packets | awk '{print $4}' } change_gateway_dns(){ #网关 DNS 切换 nvram set dhcp_gateway_x=$1 #设置网关 nvram set dhcp_dns1_x=$1 nvram set dhcp_dns2_x="" #设置 DNS nvram commit #提交修改 rc rc_service restart_net_and_phy #重启主路由 网络服务 restart_dns #重启主路由 DNS restart_dhcpd #重启主路由 DHCP radio2_restart #重启无线 radio5_restart sleep 5 ifconfig eth2 down sleep 2 ifconfig eth2 up #reboot #自动修改后存在网络故障,可能需要直接重启路由器,可取消注释,等待时间较长 } # If the gateway of the up close, the network is completely unusable res=`check_ip_available $up_gateway` #检测旁路由是否存在 current_gateway=`nvram get dhcp_gateway_x` #获取主路由当前网关 if [ $(($res)) -eq 0 ]; #检测结果等于 0,即旁路由不存在,应设置网关和 DNS 为主路由地址 then if [ "$current_gateway" != "$default_gateway" ]; then #如果当前网关地址不是主路由地址 echo "up_gateway to default" `change_gateway_dns $default_gateway` #将网关和DNS切换为主路由地址 fi echo "use default gateway , nothing changed" exit 1 fi if [ $(($res)) -ne 0 ]; #检测结果不等于 0,即旁路由存在,应设置网关和 DNS 为旁路由地址 then if [ "$current_gateway" != "$up_gateway" ]; then #如果当前网关地址不是主路由地址 echo "up_gateway to auxiliary_gateway" `change_gateway_dns $up_gateway` #将网关和DNS切换为旁路由地址 fi echo "use auxiliary_gateway , nothing changed" exit 1 fi
#!/bin/bash default_gateway='10.0.0.1' #主路由 IP auxiliary_gateway='10.0.0.21' #旁路由 IP (需设置的主路由网关 地址) up_gateway='10.0.0.21' #旁路由 IP (需设置的主路由 DNS 地址) check_ip_available(){ #使用 ping 命令检测旁路由是否在线 ping -c 3 $1 | grep packets | awk '{print $4}' } change_gateway_dns(){ #网关 DNS 切换 nvram set dhcp_gateway_x=$1 #设置网关 nvram set dhcp_dns1_x=$1 nvram set dhcp_dns2_x="" #设置 DNS nvram commit #提交修改 rc rc_service restart_net_and_phy #重启主路由 网络服务 restart_dns #重启主路由 DNS restart_dhcpd #重启主路由 DHCP radio2_restart #重启无线 radio5_restart sleep 5 ifconfig eth2 down sleep 2 ifconfig eth2 up #reboot #自动修改后存在网络故障,可能需要直接重启路由器,可取消注释,等待时间较长 } # If the gateway of the up close, the network is completely unusable res=`check_ip_available $up_gateway` #检测旁路由是否存在 current_gateway=`nvram get dhcp_gateway_x` #获取主路由当前网关 if [ $(($res)) -eq 0 ]; #检测结果等于 0,即旁路由不存在,应设置网关和 DNS 为主路由地址 then if [ "$current_gateway" != "$default_gateway" ]; then #如果当前网关地址不是主路由地址 echo "up_gateway to default" `change_gateway_dns $default_gateway` #将网关和DNS切换为主路由地址 fi echo "use default gateway , nothing changed" exit 1 fi if [ $(($res)) -ne 0 ]; #检测结果不等于 0,即旁路由存在,应设置网关和 DNS 为旁路由地址 then if [ "$current_gateway" != "$up_gateway" ]; then #如果当前网关地址不是主路由地址 echo "up_gateway to auxiliary_gateway" `change_gateway_dns $up_gateway` #将网关和DNS切换为旁路由地址 fi echo "use auxiliary_gateway , nothing changed" exit 1 fi
#!/bin/bash default_gateway='10.0.0.1' #主路由 IP auxiliary_gateway='10.0.0.21' #旁路由 IP (需设置的主路由网关 地址) up_gateway='10.0.0.21' #旁路由 IP (需设置的主路由 DNS 地址) check_ip_available(){ #使用 ping 命令检测旁路由是否在线 ping -c 3 $1 | grep packets | awk '{print $4}' } change_gateway_dns(){ #网关 DNS 切换 nvram set dhcp_gateway_x=$1 #设置网关 nvram set dhcp_dns1_x=$1 nvram set dhcp_dns2_x="" #设置 DNS nvram commit #提交修改 rc rc_service restart_net_and_phy #重启主路由 网络服务 restart_dns #重启主路由 DNS restart_dhcpd #重启主路由 DHCP radio2_restart #重启无线 radio5_restart sleep 5 ifconfig eth2 down sleep 2 ifconfig eth2 up #reboot #自动修改后存在网络故障,可能需要直接重启路由器,可取消注释,等待时间较长 } # If the gateway of the up close, the network is completely unusable res=`check_ip_available $up_gateway` #检测旁路由是否存在 current_gateway=`nvram get dhcp_gateway_x` #获取主路由当前网关 if [ $(($res)) -eq 0 ]; #检测结果等于 0,即旁路由不存在,应设置网关和 DNS 为主路由地址 then if [ "$current_gateway" != "$default_gateway" ]; then #如果当前网关地址不是主路由地址 echo "up_gateway to default" `change_gateway_dns $default_gateway` #将网关和DNS切换为主路由地址 fi echo "use default gateway , nothing changed" exit 1 fi if [ $(($res)) -ne 0 ]; #检测结果不等于 0,即旁路由存在,应设置网关和 DNS 为旁路由地址 then if [ "$current_gateway" != "$up_gateway" ]; then #如果当前网关地址不是主路由地址 echo "up_gateway to auxiliary_gateway" `change_gateway_dns $up_gateway` #将网关和DNS切换为旁路由地址 fi echo "use auxiliary_gateway , nothing changed" exit 1 fi

最终步骤

  • 将脚本置于 /etc/storage/
  • 设置脚本运行权限:chmod +x  /etc/storage/change_gatway.sh
  • 保存脚本,防止重启丢失:/sbin/mtd_storage.sh save
  • 自动定时运行脚本(每一分钟检测一次):
    crontab -e  加入 */1 * * * * /etc/storage/change_gatway.sh

现存问题

   自动切换以后,上网设备需要重新连接 WiFi ,网络才可用,希望大家多多指教

 

原文链接:https://blog.51cto.com/u_13521/11249503

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享