在MT7628实现wan、wwan与4G动态切换
经过一番调试,发现当网线热插拔时,系统并没有触发链路断开事件通知netifd进程来关闭网络,导致其接口的配置残留在路由表中,从而影响其他接口搜索默认网关。
根本原因是MT7628系列设备具有内置交换机功能,由于交换机和CPU端口之间始终连接,因此本地以太网接口永远不会看到链路断开事件,这意味着netifd将不会关闭并取消配置关联的网络,导致热插拔无效。
解决方法
既然是硬件原因导致无法使用热插拔事件,那只好通过轮询方式来检测网络。定期对外网进行ping测试,如果测试正常,则无需切换网络;如果测试失败,则自动切换到另一个网络接口,以此循环。通过以下脚本,能实现网络接口的动态切换。
1.网络测试及切换脚本
通过以下脚本,可以实现周期ping测试和网络接口切换功能。
脚本名称:/etc/pingtest
#!/bin/sh state=0 IP="www.baidu.com" ping_status() { for var in 1 2 3; do if ping -c 1 $IP > /dev/null; then #echo "$IP Ping is successful." return 0 else #echo "$IP Ping is failure" if [ $var -eq 3 ]; then return 1 fi fi done } while true do ping_status if [ $? -ne 0 ]; then case $state in 0) #echo "wan to wwan" if uci get network.wwan > /dev/null; then uci set network.wwan.metric=0 fi if uci get network.wan > /dev/null; then uci set network.wan.metric=20 fi if uci get network.4G > /dev/null; then uci set network.4G.metric=10 fi ;; 1) #echo "wwan to 4G" if uci get network.wwan > /dev/null; then uci set network.wwan.metric=20 fi if uci get network.wan > /dev/null; then uci set network.wan.metric=10 fi if uci get network.4G > /dev/null; then uci set network.4G.metric=0 fi ;; 2) #echo "4G to wan" if uci get network.wwan > /dev/null; then uci set network.wwan.metric=10 fi if uci get network.wan > /dev/null; then uci set network.wan.metric=0 fi if uci get network.4G > /dev/null; then uci set network.4G.metric=20 fi ;; esac uci commit network; /etc/init.d/network reload; let state++ if [ ${state} -ge 3 ]; then state=0 fi fi sleep 10 done
-
进行对外网进行一轮ping测试,最多测试3次,当有一次测试通过则代表成功,不作网络处理;如果3次测试失败,则表示网络已断。
-
如果网络已断,通过UCI命令修改network文件中各接口的默认网关优先级参数metric(值越小优先级越高),使用uci commit network命令保存修改,并重新加载配置文件:/etc/init.d/network reload。
-
完成一轮检测后,10秒后再次检测,以此循环。如果当前使用的网络接口断了,脚本就会自动切换到适合的下一个网络接口。
2.开机自启脚本
在/etc/init.d/目录下编写开机启动的shell脚本,该脚本运行时,在后台启动/etc/pingtest脚本。
脚本名称:/etc/init.d/interface_check
#!/bin/sh /etc/rc.common START=98 STOP=99 USE_PROCD=1 start_service() { procd_open_instance procd_set_param respawn . /etc/pingtest & procd_close_instance } stop_service() { return; }
-
START=98,STOP=99分别是启动和关闭和优先级
-
USE_PROCD=1表示使用进程,配合procd_open_instance和
procd_close_instance命令启动新进程,让/etc/pingtest脚本在进程中运行。
3.开启启动脚本软链接
在/etc/rc.d目录下新建开机启动脚本/etc/init.d/interface_check的软链接,下次开机脚本就会自动运行。
ln -s /etc/init.d/interface_check /etc/rc.d/S98interface_check
S98是脚本启动的优先级链接:https://www.jianshu.com/p/f058229a42d8
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具