时间:2023-06-02 01:42:01 | 来源:网站运营
时间:2023-06-02 01:42:01 来源:网站运营
merlin-梅林路由器自动切换网关:#!/bin/bashdefault_gateway='192.168.50.1'auxiliary_gateway='192.168.50.2'up_gateway='192.168.1.1'check_ip_available(){ ping -c 3 $1 | grep packets | awk '{print $4}'}# If the gateway of the up close, the network is completely unusableres=`check_ip_available $up_gateway`echo $resif [ $((res)) -eq 0 ]; then echo "up_gateway unusable" exit 1ficur_gateway=`/bin/nvram get dhcp_gateway_x` # get current gatewayif [ "$cur_gateway" = "$default_gateway" ]; then echo "cur is default_gateway" res=`check_ip_available $auxiliary_gateway` if [ $(($res)) -eq 0 ]; then echo "d-exit" exit 1 fi echo "switch to auxiliary" # to switch /bin/nvram set dhcp_gateway_x=$auxiliary_gateway /bin/nvram set dhcp_dns1_x=$auxiliary_gateway /bin/nvram set dhcp_dns2_x="" /bin/nvram commit /sbin/rc rc_service restart_net_and_phy exit 0fiif [ "$cur_gateway" = "$auxiliary_gateway" ]; then echo "cur is auxiliary_gateway" res=`check_ip_available $auxiliary_gateway` if [ $(($res)) -gt 0 ]; then echo "a-exit" exit 1 fi echo "switch to default" # to switch /bin/nvram set dhcp_gateway_x=$default_gateway /bin/nvram set dhcp_dns1_x=119.29.29.29 /bin/nvram set dhcp_dns2_x=223.5.5.5 /bin/nvram commit /sbin/rc rc_service restart_net_and_phy exit 0fiecho "nothing change"
/sbin/rc rc_service restart_net_and_phy,这句命令,其实就是路由器修改本地网关后,会触发的命令。所以这个命令,和网页手动触发效果是一样的LAN代码。这样不用调用reboot命令重启路由器#!/bin/bashwhile truedosleep 1;ping -c 1 192.168.1.1 > /dev/null #判断路由器的WAN是否连接上了if [ $? -ne 0 ]; then continuefiecho "* * * * * /bin/bash /jffs/scripts/auto_switch_gateway.sh" >> /var/spool/cron/crontabs/xxx# 这里需要把XXX改成你的路由账号名breakdone
把上面两个脚本都放在/jffs/scripts/目录下,然后我们看下init-start配置#!/bin/sh/bin/bash /jffs/scripts/create_crontab_auto_switch_gateway.sh
如上,就配置好了网关定时任务切换echo '0 4 * * * service restart_httpd' >> /var/spool/cron/crontabs/xxx
如果你和我一样,是用广电盒子,广电盒子上网重启后比较坑,需要开广电的U点盒子开电视后,才能上网的。echo '* * * * * /usr/sbin/curl http://192.168.1.1/cgi-bin/luci' >> /var/spool/cron/crontabs/xxx
所以,我的init-start是这样的#!/bin/sh/bin/bash /jffs/scripts/create_crontab_auto_switch_gateway.shecho '0 4 * * * service restart_httpd' >> /var/spool/cron/crontabs/xxxecho '* * * * * /usr/sbin/curl http://192.168.1.1/cgi-bin/luci' >> /var/spool/cron/crontabs/xxx
关键词: