转载地址:https://www.cnblogs.com/cryhuang/p/5649372.html
试验环境:Linux marsboard 3.4.90 #9 SMP PREEMPT Thu Mar 3 18:28:43 CST 2016 armv7l armv7l armv7l GNU/Linux (注:A20核心板)
通讯模块,加载成ttyUSB*设备;
目标:使用通讯模块进行3G4G拨号连接互联网
方法:使用pppd和ppp程序,进行拨号连接
前提:安装了ppp、pppd两个程序
摘要:涉及的文件ppp的chat-script拨号脚本(针对不通通讯设备情况需要有所调整,此处不考虑这些情况,大部分都可以使用)、
步骤:
1、创建ppp程序的拨号脚本,内容如下:(有两个,一个是针对移动、联通和电信4G的,一个是只针对电信3G的)
# 针对移动、联通和电信4G的,文件名chat-script
#!/bin/sh
exec chat -v \
TIMEOUT 5 \
ABORT "BUSY" \
ABORT "ERROR" \
ABORT "NO CARRIER" \
'' \rAT \
OK 'AT+CGDCONT=1,"IP","CMNET"' \
OK 'ATDT*99#' \
CONNECT '' \
-----------------不同脚本分割,请存在不通文件中-----------------------------------------
# 针对电信3G的,文件名chat-script-ct3g
#!/bin/sh
exec chat -v \
TIMEOUT 5 \
ABORT "BUSY" \
ABORT "ERROR" \
ABORT "NO CARRIER" \
'' \rAT \
OK 'AT+CGDCONT=1,"IP","CMNET"' \
OK 'ATDT#777' \
CONNECT '' \
2、创建两个拨号脚本,ppp-on用于拨号连接 ppp-off用户断开连接
# 文件 ppp-on
#! /bin/sh
# $1 串口号 $2波特率 $3chat脚本
pppd modem -d -detach lock $1 $2 kdebug 4 file /etc/ppp/options crtscts noipdefault netmask 255.255.255.0 defaultroute connect $3
------------------------------------------------------
# 文件 ppp-off
#!/bin/sh
######################################################################
#
# Determine the device to be terminated.
#
if [ "$1" = "" ]; then
DEVICE=ppp0
else
DEVICE=$1
fi
######################################################################
#
# If the ppp0 pid file is present then the program is running. Stop it.
if [ -r /var/run/$DEVICE.pid ]; then
kill -INT `cat /var/run/$DEVICE.pid`
#
# If the kill did not work then there is no process running for this
# pid. It may also mean that the lock file will be left. You may wish
# to delete the lock file at the same time.
if [ ! "$?" = "0" ]; then
rm -f /var/run/$DEVICE.pid
echo "ERROR: Removed stale pid file"
exit 1
fi
#
# Success. Let pppd clean up its own junk.
echo "PPP link to $DEVICE terminated."
exit 0
fi
#
# The ppp process is not running for ppp0
echo "ERROR: PPP link is not active on $DEVICE"
exit 1
3、使用方法
将以上的四个脚本文件赋予执行权限,并使用root权限执行
./ppp-of /dev/ttyUSB1 115200 chat-script(具体路径)
使用ifconfig查看拨号的结果
4、附上一个自己写的脚本,其中调用的以上的拨号脚本
#! /bin/sh
WR="/home/marsboard/pppd-net"
# $1 串口号 $2波特率 $3chat脚本 $4 ppp-on日志
${WR}/ppp-on $1 $2 ${WR}/$3 > ${WR}/$4 &
sleep 8
# 从ppp-on日志中获取拨号信息
INTERFACENAME="Using interface"
ADDRESSIPNAME="local IP address"
GATEWAYNAME="remote IP address "
INTERFACE=`grep "$INTERFACENAME" ${WR}/$4|awk '{print $3}'`
ADDRESSIP=`grep "$ADDRESSIPNAME" ${WR}/$4|awk '{print $4}'`
GATEWAYIP=`grep "$GATEWAYNAME" ${WR}/$4|awk '{print $4}'`
# 判断拨号是否成功
for n in `seq 3`
do
ping -c 1 ${ADDRESSIP} -I ${INTERFACE} >/dev/null 2>&1
if [ $? -eq 0 ]
then
echo "ppp Successed!!"
echo "${INTERFACE}, ${ADDRESSIP}, ${GATEWAYIP}"
return
fi
done
echo "ppp Failed!!"