在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
相关文章
- vue 使用vue-i18n实现中英文语言切换,以及动态添加中英文「建议收藏」
- 纯CSS实现“精灵图”动态特效
- cglib动态代理实现原理_jdk cglib 动态代理 区别
- Pytest测试框架一键动态切换测试环境实现思路及方案
- 库Linux系统中实现动态加载SO库的方法(linux动态加载so)
- 使用web页面实现oracle的安装和测试详解编程语言
- Linux实现对键盘输入的动态监听(linux监听键盘输入)
- 如何在Linux中实现动态加载库(linux动态加载库)
- 龄SQL Server计算人员年龄的实现方法(sqlserver得到年)
- 连接 MysqlJava实现长连接的简单方法(mysqljava长)
- 阿里云实现一键预装MSSQL(阿里云预装mssql)
- C语言实现TCP连接MySQL的方法(c tcp连接mysql)
- 实现高效登录系统使用Redis缓存技术(登录系统使用redis)
- 动起来用Redis实现动态切换(动态切换redis)
- oracle中余数进一法实现精准结果(oracle 余数进一)
- 借助Redis实现高性能付款(redis高并发付款)
- Oracle中动态取值的实现(oracle中动态值)
- asp由动态网页转变为静态网页的实现代码
- asp.net网页动态查询条件的实现
- ASP.NETGridView实现课程表显示(动态合并单元格)实现步骤
- js动态添加表格数据使用insertRow和insertCell实现
- Android实现动态切换组件背景的方法