树莓派EC20拨号脚本
1. ppp拨号脚本2. qmi_wwan拨号脚本3. 注意说明1. ppp拨号脚本
拨号脚本比较简单,主要是拨号和重连,主要分为以下步骤
先用lsusb检查驱动
lsusb | grep Quectel这样查询结果中包含Quectel才会显示,若没有则不显示
可以看出两个命令的区别
开始拨号
echo "raspberry" | sudo wvdial ec20_ppp&脚本中使用 sudo wvdial ec20_ppp& 考虑到使用root权限输入密码则需要使用这条命令,”raspberry“为密码
检查路由列表,并添加默认路由
若有ppp0网卡,则添加为默认路由route -n | grep ppp0echo "raspberry" | sudo route add default dev ppp0若超时未检测到路由,则重启模块
ping百度,检查网络连通
ping -I ppp0 -c 1 若无法ping通,超时则重启
上面主要是对脚本的拨号内容想法说明
创建脚本
//先创建脚本文件mkdir ec20-ppp.sh//编写脚本...//脚本运行,后台运行bash ec20-ppp.sh&
脚本开头必须#! /bin/bash
#! /bin/bash#运行步骤变量初始化ec20_step=0#超时计数初始化over_time=0#循环while [ 1 ]do#第一步先检查驱动if [ $ec20_step -eq 0 ]; then#使用lsusb查看是否有ec20驱动 grep查询结果是否包含Quectelresult=$(lsusb | grep Quectel)if [[ $result =~ "EC25" ]]; thenec20_step=1elseec20_step=0fi#延时2ssleep 2#第二步 开始使用wvdial拨号elif [ $ec20_step -eq 1 ]; thenecho "raspberry" | sudo wvdial ec20_ppp&ec20_step=2sleep 2#第三步 查询路由是否包含ppp0网卡,拨号成功则会包含有ppp0网卡elif [ $ec20_step -eq 2 ]; thenresult=$(route -n | grep ppp0)if [[ $result =~ "ppp0" ]]; then#若包含网卡,则添加默认路由echo "raspberry" | sudo route add default dev ppp0ec20_step=3over_time=0else#超时计数let over_time++fi#若一分钟都没有路由网卡则说明没有拨号成功if [ $over_time -eq 12 ]; thenover_time=0#超时拨号则跳入重启步骤ec20_step=4fisleep 5#第四步 通过ping命令检查网络状态elif [ $ec20_step -eq 3 ]; thenresult=$(ping -I ppp0 -c 1 )if [[ $result =~ "1 received" ]]; thenover_time=0else#let over_time++fi#超时则杀掉拨号线程,并进入重启步骤if [ $over_time -eq 6 ]; thenover_time=0ec20_step=4echo "raspberry" | sudo pkill wvdialfiecsleep 5#第五步重启模块elif [ $ec20_step -eq 4 ]; thenecho -e "AT+CFUN=1,1\r\n" > /dev/ttyUSB2ec20_step=0#重启命令后延时稍微长一点sleep 15fidoneexit 0
若要开机启动,则加入启动项
sudo vim /etc/rc.local//在文件exit 0前加入此句,:wq保存,重启即可生效bash /home/pi/ec20/ec20-ppp.sh
2. qmi_wwan拨号脚本
基本上与上面一样,注意脚本里面quectel-CM的路径,还有不一样的前两节拨号里面已经讲过了
#! /bin/bashec20_step=0over_time=0while [ 1 ]doif [ $ec20_step -eq 0 ]; thenresult=$(lsusb | grep Quectel)if [[ $result =~ "EC25" ]]; thenec20_step=1elseec20_step=0fisleep 2elif [ $ec20_step -eq 1 ]; thenecho "raspberry" | sudo /home/pi/ec20/quectel-CM/./quectel-CM&ec20_step=2sleep 2elif [ $ec20_step -eq 2 ]; thenresult=$(ip route)if [[ $result =~ "wwan0" ]]; thenec20_step=3over_time=0elselet over_time++fiif [ $over_time -eq 12 ]; thenover_time=0ec20_step=4echo "raspberry" | sudo pkill quectel-CMfisleep 5elif [ $ec20_step -eq 3 ]; thenresult=$(ping -I wwan0 -c 1 )if [[ $result =~ "ttl" ]]; thenover_time=0elselet over_time++fiif [ $over_time -eq 4 ]; thenover_time=0ec20_step=4echo "raspberry" | sudo pkill quectel-CMfisleep 5elif [ $ec20_step -eq 4 ]; thenecho -e "AT+CFUN=1,1\r\n" > /dev/ttyUSB2ec20_step=0sleep 15fidoneexit 0
3. 注意说明
上面两个脚本的内容可根据自己的想法去编写,上面只是一个最简单使用示例,检测效率并不是很高,实际生产使用,还需要考虑更为全面一些。
编写脚本时多查询一些脚本语言的格式和使用方式。