使用脚本,即可实现。
#!/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