#!/bin/sh
change_mode=$1
#网口
wan_state=0
#4G卡
sim_state=0
#wifi口
wifi_state=0
state=0
Number=0
#当前序号
Cur_state=4
test_kill=0
get_route_first_file()
{
IP=`route |grep -i UG|awk '{print $2}' |sed -n 1p`
Iface=`route |grep -i UG|awk '{print $8}' |sed -n 1p`
route_Metric=`route |grep -i UG|awk '{print $5}' |sed -n 1p`
echo $IP
echo $Iface
test_kill=0
PING=`ping -c 3 ${IP}|grep -v grep|grep '64 bytes' |wc -l`
if [ ${Iface} == "wwan0" ];then
if [ ${route_Metric} -eq 0 ];then
echo "ping fail1111"
test_kill=1
return 1
fi
return 0
fi
if [ ${PING} -ne 0 ];then
echo "ping success"
return 0
else
echo "ping fail" >> /tmp/log/checknetwork.log
return 1
fi
}
wan_wifi_4g_file()
{
echo "wan_wifi_4g_file"
if uci get network.wan > /dev/null; then
wan_state=`ubus call network.interface.wan status|awk '$1~"\"up\":"{print $2}'`
echo $wan_state
if [[ $wan_state == "false," ]]; then
echo "1false"
else
echo "1true"
get_route_first_file
if [ $? -eq 0 ]; then
state=0
return 0
fi
fi
fi
sleep 5;
if uci get network.wwan_wifi > /dev/null; then
wifi_state=`ubus call network.interface.wwan_wifi status|awk '$1~"\"up\":"{print $2}'`
echo $wifi_state
if [[ $wifi_state == "false," ]]; then
echo "3false"
else
echo "3true"
get_route_first_file
if [ $? -eq 0 ]; then
state=1
return 0
fi
fi
fi
#sleep 10;
if uci get network.wwan_4g > /dev/null; then
sim_state=`ubus call network.interface.wwan_4g status|awk '$1~"\"up\":"{print $2}'`
echo $sim_state
if [[ $sim_state == "false," ]]; then
echo "2false"
else
echo "2true"
state=2
return 0
fi
fi
}
wan_4g_wifi_file()
{
echo "wan_4g_wifi_file"
if uci get network.wan > /dev/null; then
wan_state=`ubus call network.interface.wan status|awk '$1~"\"up\":"{print $2}'`
echo $wan_state
if [[ $wan_state == "false," ]]; then
echo "1false"
else
echo "1true"
get_route_first_file
if [ $? -eq 0 ]; then
state=0
return 0
fi
fi
fi
if uci get network.wwan_4g > /dev/null; then
sim_state=`ubus call network.interface.wwan_4g status|awk '$1~"\"up\":"{print $2}'`
echo $sim_state
if [[ $sim_state == "false," ]]; then
echo "2false"
else
echo "2true"
state=2
return 0
fi
fi
if uci get network.wwan_wifi > /dev/null; then
wifi_state=`ubus call network.interface.wwan_wifi status|awk '$1~"\"up\":"{print $2}'`
echo $wifi_state
if [[ $wifi_state == "false," ]]; then
echo "3false"
else
echo "3true"
get_route_first_file
if [ $? -eq 0 ]; then
state=1
return 0
fi
fi
fi
}
wifi_wan_4g_file()
{
echo "wifi_wan_4g_file"
if uci get network.wwan_wifi > /dev/null; then
wifi_state=`ubus call network.interface.wwan_wifi status|awk '$1~"\"up\":"{print $2}'`
#echo $wifi_state
if [[ $wifi_state == "false," ]]; then
echo "3false"
else
echo "3true"
get_route_first_file
if [ $? -eq 0 ]; then
state=1
return 0
fi
fi
fi
if uci get network.wan > /dev/null; then
wan_state=`ubus call network.interface.wan status|awk '$1~"\"up\":"{print $2}'`
#echo $wan_state
if [[ $wan_state == "false," ]]; then
echo "1false"
else
echo "1true"
get_route_first_file
if [ $? -eq 0 ]; then
state=0
return 0
fi
fi
fi
if uci get network.wwan_4g > /dev/null; then
sim_state=`ubus call network.interface.wwan_4g status|awk '$1~"\"up\":"{print $2}'`
#echo $sim_state
if [[ $sim_state == "false," ]]; then
echo "2false"
else
echo "2true"
state=2
return 0
fi
fi
}
wifi_4g_wan_file()
{
echo "wifi_4g_wan_file"
if uci get network.wwan_wifi > /dev/null; then
wifi_state=`ubus call network.interface.wwan_wifi status|awk '$1~"\"up\":"{print $2}'`
#echo $wifi_state
if [[ $wifi_state == "false," ]]; then
echo "3false"
else
echo "3true"
get_route_first_file
if [ $? -eq 0 ]; then
state=1
return 0
fi
fi
fi
if uci get network.wwan_4g > /dev/null; then
sim_state=`ubus call network.interface.wwan_4g status|awk '$1~"\"up\":"{print $2}'`
#echo $sim_state
if [[ $sim_state == "false," ]]; then
echo "2false"
else
echo "2true"
state=2
return 0
fi
fi
if uci get network.wan > /dev/null; then
wan_state=`ubus call network.interface.wan status|awk '$1~"\"up\":"{print $2}'`
#echo $wan_state
if [[ $wan_state == "false," ]]; then
echo "1false"
else
echo "1true"
get_route_first_file
if [ $? -eq 0 ]; then
state=0
return 0
fi
fi
fi
}
g_wan_wifi_file()
{
echo "g_wan_wifi_file"
if uci get network.wwan_4g > /dev/null; then
sim_state=`ubus call network.interface.wwan_4g status|awk '$1~"\"up\":"{print $2}'`
#echo $sim_state
if [[ $sim_state == "false," ]]; then
echo "2false"
else
echo "2true"
state=2
return 0
fi
fi
if uci get network.wan > /dev/null; then
wan_state=`ubus call network.interface.wan status|awk '$1~"\"up\":"{print $2}'`
#echo $wan_state
if [[ $wan_state == "false," ]]; then
echo "1false"
else
echo "1true"
get_route_first_file
if [ $? -eq 0 ]; then
state=0
return 0
fi
fi
fi
if uci get network.wwan_wifi > /dev/null; then
wifi_state=`ubus call network.interface.wwan_wifi status|awk '$1~"\"up\":"{print $2}'`
#echo $wifi_state
if [[ $wifi_state == "false," ]]; then
echo "3false"
else
echo "3true"
get_route_first_file
if [ $? -eq 0 ]; then
state=1
return 0
fi
fi
fi
}
g_wifi_wan_file()
{
echo "g_wifi_wan_file"
if uci get network.wwan_4g > /dev/null; then
sim_state=`ubus call network.interface.wwan_4g status|awk '$1~"\"up\":"{print $2}'`
#echo $sim_state
if [[ $sim_state == "false," ]]; then
echo "2false"
else
echo "2true"
state=2
return 0
fi
fi
sleep 5;
if uci get network.wwan_wifi > /dev/null; then
wifi_state=`ubus call network.interface.wwan_wifi status|awk '$1~"\"up\":"{print $2}'`
#echo $wifi_state
if [[ $wifi_state == "false," ]]; then
echo "3false"
else
echo "3true"
get_route_first_file
if [ $? -eq 0 ]; then
state=1
return 0
fi
fi
fi
if uci get network.wan > /dev/null; then
wan_state=`ubus call network.interface.wan status|awk '$1~"\"up\":"{print $2}'`
#echo $wan_state
if [[ $wan_state == "false," ]]; then
echo "1false"
else
echo "1true"
get_route_first_file
if [ $? -eq 0 ]; then
state=0
return 0
fi
fi
fi
}
read_write_file()
{
first_read=$(cat /etc/config/network_style.txt)
if [ ! -n "$first_read" ]; then
if [ ! -n "$change_mode" ]; then
change_mode=1
fi
echo $change_mode > /etc/config/network_style.txt
else
if [ ! -n "$change_mode" ]; then
change_mode=$first_read
fi
echo $change_mode > /etc/config/network_style.txt
fi
}
change_state()
{
#选择模式
#1.wan-wifi-4G
#2.wan-4G-wifi
#3.wifi-wan-4G
#4.wifi-4G-wan
#5.4G-wan-wifi
#6.4G-wifi-wan
if [ $change_mode -eq 1 ]; then
wan_wifi_4g_file
elif [ $change_mode -eq 2 ]; then
wan_4g_wifi_file
elif [ $change_mode -eq 3 ]; then
sleep 15;
wifi_wan_4g_file
elif [ $change_mode -eq 4 ]; then
sleep 15;
wifi_4g_wan_file
elif [ $change_mode -eq 5 ]; then
sleep 30;
g_wan_wifi_file
elif [ $change_mode -eq 6 ]; then
sleep 30;
g_wifi_wan_file
else
wan_wifi_4g_file
fi
if [ $? -eq 0 ]; then
case $state in
0) echo "wan "
Cur_state=`uci get network.wan.metric`
echo "$Cur_state"
if [ $Cur_state -eq 1 ]; then
return;
fi
uci set network.wan.metric=1
uci set network.wwan_4g.metric=15
uci set network.wwan_wifi.metric=10
#uci get network.wan
;;
1) echo "wwan_wifi "
Cur_state=`uci get network.wwan_wifi.metric`
echo "$Cur_state"
if [ $Cur_state -eq 1 ]; then
return;
fi
uci set network.wwan_wifi.metric=1
uci set network.wan.metric=10
uci set network.wwan_4g.metric=15
;;
2) echo "wwan_4g "
Cur_state=`uci get network.wwan_4g.metric`
echo "$Cur_state"
if [ $Cur_state -eq 1 ]; then
return;
fi
uci set network.wwan_4g.metric=1
uci set network.wan.metric=10
uci set network.wwan_wifi.metric=15
;;
esac
uci commit network;
/etc/init.d/network restart;
if [ $test_kill -eq 0 ]; then
pid=$(ps | grep at_cmd | grep -v grep |awk '{print $1}' )
if [ -n "$pid" ]; then
kill -9 $pid;
fi
fi
else
echo "fail"
fi
}
while true
do
sleep 20;
read_write_file
change_state
done
openwrt 网口-wifi-4G网络切换
于 2021-12-31 15:17:23 首次发布