4G,pppd守护进行,断线重连,掉线重连

这是一个bash脚本,用于自动连接4G网络(通过pppd调用wcdma-ec20),并进行故障检查和恢复。它会检查/dev/ttyUSB*设备,设置GPIO65的状态,等待网络连接,并在无法连接或DNS解析失败时重启pppd进程或系统。此外,脚本还会更新resolv.conf文件,并在DNS不可达时尝试切换到备用DNS。整个过程包含多个重试和延迟机制,确保网络连接的稳定。
摘要由CSDN通过智能技术生成

使用脚本,即可实现。

#!/bin/bash

#exit 0

#echo "4g test"
#ls /dev/ttyU*
#cat /dev/ttyUSB2 &
#echo -en "AT\r\n" > /dev/ttyUSB2
#killall cat
#exit 0

echo "4g pppd"

count=0

#sleep 5

#procedure='pppd'
if [ "$(ps |grep "pppd call" |grep -v grep |awk '{print $1}')" ]; then
    killall pppd #结束PPPD进程
fi

for ((i=0; i<15; i++))
do
    sleep 1
    if [ -f "/sys/class/gpio/gpio65/value" ]; then
        break
    else
        echo 65 > /sys/class/gpio/export
    fi
done
echo high > /sys/class/gpio/gpio65/direction

fun_check_usb(){
    countCheckUSB=0
    while true
    do
        usleep 500000
        ret=`ls /dev/ttyUSB*`
        if [ "$ret" ]; then
            echo $ret
            break
        fi
        let countCheckUSB++
        if [ $countCheckUSB -gt 35 ]; then
            reboot
            exit 0
        fi
    done
}
fun_check_usb

#"/sys/class/net/eth0"
#ifconfig eth0 down
#ifconfig eth1 down

#rm /etc/resolv.conf
#ln -s /etc/ppp/resolv.conf /etc/resolv.conf
cp /etc/ppp/resolv.conf /etc/resolv.conf

pppd call wcdma-ec20 >/dev/null 2>&1 &

sleep 12 #等待12秒
fun_wait_ppp0(){
    countWait=0
    while true
    do
        usleep 500000
        if [ "$(ifconfig |grep ppp0)" ]; then
            break
        fi
        let countWait++
        echo "pppd countWait=$countWait"
        if [ $countWait -gt 30 ]; then
            date
            killall pppd #结束PPPD进程
            echo "ppp0 is not existed"
            if [ $count -lt 15 ]; then
                count=15
            fi
            break
            #reboot
            #exit 0
        fi
    done
}
fun_wait_ppp0

fun_route(){
    if [ ! "$(route -e |grep default |grep ppp0 |awk '{print $1}')" ]; then
        if [ "$(ifconfig |grep ppp0)" ]; then
            route del default
            route add default dev ppp0
        fi
    fi
}
fun_route

#---auto ppp---
dns1="8.8.8.8"
dns2="114.114.114.114"
sleep 12 #等待12秒
while true
do
    ping -s 1 -c 5 $dns1 >/dev/null 2>&1 #去PING第一个DNS
    if [ "$?" != "0" ] #假如PING不通
    then
        fun_route
        ping -s 1 -c 2 $dns2 >/dev/null 2>&1 #去PING第二个DNS
        if [ "$?" != "0" ]
        then
            date
            killall pppd #结束PPPD进程
            let count++
            echo "pppd count=$count"
            if [ $count -gt 30 ]; then
                reboot
                exit 0
            elif [ $count -gt 15 ]; then
                echo 0 > /sys/class/gpio/gpio65/value
                usleep 500000
                echo 1 > /sys/class/gpio/gpio65/value
                #ttyUSB* is existed about 10 second
                #sleep 12 #等待12秒
            fi
            sleep 12 #等待12秒
            fun_check_usb
            #ifconfig eth0 down
            #ifconfig eth1 down
            #cp /etc/ppp/resolv.conf /etc/resolv.conf
            pppd call wcdma-ec20 >/dev/null 2>&1 & #再去拨号
            sleep 12 #等待12秒
            fun_wait_ppp0
            fun_route
            sleep 5
        else
            count=0
            sleep 60 #如果是PING  DNS2通的话就直接等待5秒
        fi
    else
        count=0
        sleep 120 #如果是PING  DNS1通的话就直接等待5秒(一般要设置多长时间去PING请改这里)
    fi
done


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寞水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值