Welcome to Yumao′s Blog.
OpenWRT PPPoE重連之後無網絡的解決
, 2021年03月07日 , Linux , 评论 在〈OpenWRT PPPoE重連之後無網絡的解決〉中留言功能已關閉 ,

自動更新了OP1907之後
經常發生PPPoE連接5760分鍾的自動重連後
發現沒有網絡可用
接口顯示已經獲取到IP地址
然後查詢路由表發現默認路由未添加
只添加了網關的路由
那就直接加一個cron狗來保證網絡正常

> nano /root/pppdog.sh
#!/bin/sh
. /lib/netifd/netifd-proto.sh

INTERFACE=wan
DEFAULTROUTE=`route -n |grep ^0.0.0.0 |grep pppoe-$INTERFACE$`
PINGIP=1.2.4.8

if [ -z "$DEFAULTROUTE" ]
then
        if [ -n "$GATEWAYIP" ]
        then
                echo "`date '+%Y-%m-%d %H:%M:%S'` - PPPDog No Route Reload Route" >> /root/pppdog.log
                # IPv4 Route
                ip route add `ifstatus wan |jsonfilter -e '@["route"][0].nexthop'` dev pppoe-$INTERFACE
                ip route add default via $GATEWAYIP dev pppoe-$INTERFACE
                # IPv6 Init
                json_init
                json_add_string name "${INTERFACE}_6"
                json_add_string ifname "@${INTERFACE}"
                json_add_string proto dhcpv6
                json_add_string zone "wan"
                json_close_object
                ubus call network add_dynamic "$(json_dump)"
                sleep 10s
                # IPv6 Route
                ip -6 route add `ifstatus wan_6 |jsonfilter -e '@["route"][0].target'`/`ifstatus wan_6 |jsonfilter -e '@["route"][0].mask'` dev pppoe-$INTERFACE proto static metric `ifstatus wan_6 |jsonfilter -e '@["route"][0].metric'` pref medium
                ip -6 route add default from `ifstatus wan_6 |jsonfilter -e '@["route"][1].source'` via `ifstatus wan_6 |jsonfilter -e '@["route"][1].nexthop'` dev pppoe-$INTERFACE proto static metric `ifstatus wan_6 |jsonfilter -e '@["route"][1].metric'` pref medium
                ip -6 route add default from `ifstatus wan_6 |jsonfilter -e '@["route"][2].source'` via `ifstatus wan_6 |jsonfilter -e '@["route"][2].nexthop'` dev pppoe-$INTERFACE proto static metric `ifstatus wan_6 |jsonfilter -e '@["route"][2].metric'` pref medium
        else
                echo "`date '+%Y-%m-%d %H:%M:%S'` - PPPDog No Route Reset Interface" >> /root/pppdog.log
                ifdown $INTERFACE
                ifup $INTERFACE
        fi
else
        PINGTEST=`ping $PINGIP -c 1`
        PINGEXIT="$?"
        if [ "$PINGEXIT" == "1" ]
        then
                echo "`date '+%Y-%m-%d %H:%M:%S'` - PPPDog Ping Error Reset Interface" >> /root/pppdog.log
                ifdown $INTERFACE
                ifup $INTERFACE
        else
                PINGLOSS=`ping $PINGIP -c 4 |grep 100%`
                if [ -n "$PINGLOSS" ]
                then
                        echo "`date '+%Y-%m-%d %H:%M:%S'` - PPPDog Ping Loss Reset Interface" >> /root/pppdog.log
                        ifdown $INTERFACE
                        ifup $INTERFACE
                fi
        fi
fi

exit 0

> nano /etc/crontabs/root
0 * * * * /root/pppdog.sh

> /etc/init.d/cron restart

每小時檢測一次網關是否存在
不存在的話添加
存在的話測試網絡(Ping)是否通暢
不通暢則重啓目標接口

评论已关闭