I took over an embedded ARM9 development board with Ethernet port, USB WiFi module, and 3G module, that is, all three methods can access the Internet. There are two methods commonly used, Ethernet and 3G, and the Ethernet Internet access method is more Stable, but the 3G dial-up method sometimes fails to succeed even all day long. I have read some blogs and suggested the following methods based on their experience:
1. Close eth0
ifconfig eth0 down
2. Delete the default route before dialing
route del default
3. Delete the default route and set ppp0 as the default route
route del default
route add default dev ppp0
The above methods have been tested and verified. The most reliable method is the first one, but the best formula is to write an infinite loop regular check in the script. Once it is detected that the Internet cannot be accessed, first shut down the pppd process, and then restart the 3G module. Finally start the pppd process again.
The following is a reference scheme
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
According to the current tracking phenomenon, the reason may still lie in the routing (kernel version linux-3.10.x, ppp2.4.7), if you have a clear brother, please let me know in the comment area, I would be very grateful!
Provide a script solution for dynamic network automatic switching, which includes some operations to delete the default route, as follows
#!/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
Attached are some reference articles:
Linux transplantation of ppp and routing settings of ppp0_tietao's column-CSDN blog_linux ppp0