接手一嵌入式ARM9开发板,带以太网口、USB WiFi模块、3G模块,也即三种方式均可上网,平时用的较多的有两种方式,以太网和3G,以太网上网方式较为稳定,但3G拨号方式,有时甚至整天都不能成功,看过一些博客,有根据他们的经验主要建议以下几种方法:
1. 关闭eth0
ifconfig eth0 down
2. 拨号前删除默认route
route del default
3. 删除默认路由,设置ppp0为默认路由
route del default
route add default dev ppp0
以上方法均做过测试验证,最可靠的做法还是第一种,但最佳配方还是在脚本中写个无限循环定期检查,一旦检测到无法上网,则先关闭掉pppd进程,再重启3G模块,最后再次启动pppd进程。
下方是一种参考方案
while true
do
# whatever eth or wlan or ppp
ping -s 1 -c 1 $server > /dev/null
if [ "$?" != "0" ]
then
#echo "OFF TIME is `date +%Y-%m-%d,%H:%M:%S`" >> /path/offinfo
killall pppd
# function for restart 3g module
restart_3gmodule
sleep 15s
# function for start pppd,shut eth0 down here
pppd_call
fi
sleep 300s
done
根据当前追踪的现象,原因可能还是在于路由这块(内核版本linux-3.10.x, ppp2.4.7),有清楚的兄台麻烦评论区告知一二,在下不胜感激!
提供一份动态网络自动切换的脚本方案,这里面做了删除默认路由的一些操作,如下
#!/bin/sh
#check the wired and wireless network reachable
wired=eth0
#wired=wlan0
wireless=ppp0
needRetryTimes=10
needRestartUdhcpc=0
flag=0
while true; do
Gateway=`route|grep 'default'|grep $wired|awk '{print $2}'`
if [ "$Gateway" != "*" ] && [ -n "$Gateway" ]; then
ping -s1 -w1 $Gateway > /dev/null
else
sleep 2s
continue
fi
if [ "$?" != "0" ]; then
let flag++
if [ $flag -lt $needRetryTimes ]; then
echo "we will retry once.." > /dev/null
continue
fi
flag=0
#if wired if default,delete it
if [ -n "`route|grep 'default'|grep $wired`" ];then
echo "route del default $wired" > /dev/null
route del default $wired
fi
if [ $needRestartUdhcpc -eq 1 ]; then
echo "$wired not reachable,so only restart udhcpc once" > /dev/null
kill -9 `ps|grep 'udhcpc'|grep -v grep|awk '{print $1}'`
sleep 1s
/sbin/udhcpc -i $wired -F ZigbeeLinuxGateway -b > /dev/null &
sleep 2s
needRestartUdhcpc=0
fi
#if wireless is not default,add it
if [ -z "`route|grep 'default'|grep $wireless`" ];then
echo "route add default $wireless" > /dev/null
route add default $wireless
fi
echo "wait 2s and retry" > /dev/null
sleep 2s
else
sleep 2s
needRestartUdhcpc=1
fi
done
附带一些参考文章: