ppp拨号上网总结

https://blog.csdn.net/linczone/article/details/45917655

Author: chad 
Mail: [email protected]

本文可以自由转载,但转载请务必注明出处以及本声明信息。

一、开发环境简述

  • GPRS模块 MC52I
  • SIM卡 移动
  • 嵌入式开发板 AT91SAM9260EK
  • linux版本 2.6.30
  • 主机环境 ubuntu
  • ppp-2.4.4 拨号程序源码包见本笔记附件。
  • 附件内为运行正常的ppp-on与ppp-on-dialer脚本,使用时放在/sbin目录下即可。

二、主要步骤

  1. 内核添加PPP支持。
  2. 下载ppp-2.4.4源码进行编译。
  3. 移植到开发板进行测试。

三、配置详细说明

1、内核添加PPP支持,make menuconfig,按如下步骤配置: 
ppp协议支持的添加(下面所示的几个选项必选)。

Device Drivers--→
Network device support---→
<*> PPP (point-to-point protocol) support   
              [*]   PPP multilink support (EXPERIMENTAL)     
              <*>   PPP support for async serial ports      
              <*>   PPP support for sync tty ports          
              <*>   PPP Deflate compression                 
               <*>   PPP BSD-Compress compression

完成这些步骤之后,可以进行内核的交叉编译,退出make menuconfig,输入:

make zImage ARCH=arm CROSS_COMPILE=arm-linux-

编译生成的内核映像文件位于当前内核源代码文件的/arch/arm/boot目录下,将生成的内核映像文件下载到板子上。 
内核启动后,会在/dev目录下生成ppp设备节点。如:

ls /dev/ppp -l
crw-rw----  1 root   root  108, 0 Jan  1 00:00 /dev/ppp

2、交叉编译好工作源代码ppp-2.4.4,得到pppd和chat。

解压源代码包,进入目录,进行交叉编译,这里所用的板子的交叉编译器是:

#cd /home/ppp-2.4.4
#./configure
#make CC= arm-linux-gcc

拨号所用到的程序就是ppp-2.4.4/pppd下的pppd和ppp-2.4.4/chat下的chat可执行程序,将交叉编译出来的这两个应用程序拷贝到开发板 /sbin目录下,更改其属性为可执行文件。

3、配置拨号用脚本文件

拨号上网需要的配置文件有3个:ppp-on 、ppp-off 、ppp-dial-on,这三个文件可以在 ppp-2.4.4/script 文件夹内找到。

(1)修改ppp-on:

#vi ppp-on

内容如下,修改一下里面的内容

TELEPHONE=555-1212 改为 TELEPHONE=*99***1#


exec /usr/sbin/pppd debug lock modem crtscts /dev/ttyS0 38400 asyncmap 20A0000 escape FF kdebug 0 $LOCAL_IP:$REMOTE_IP noipdefault netmask $NETMASK defaultroute connect $DIALER_SCRIPT
改为
exec /sbin/pppd debug lock modem nocrtscts 你的串口设备 你的波特率 asyncmap 20A0000 escape FF kdebug 0 $LOCAL_IP:$REMOTE_IP noipdefault netmask $NETMASK defaultroute connect $DIALER_SCRIPT

ppp-on 修改完成后执行 ESC 和 :wq ! 强制保存。 
我的ppp-on 内容如下图: 
这里写图片描述

(2) 修改ppp-on-dialer

#vi ppp-on-dialer

删除最后两行关于 拨号帐户密码的。 
到此修改结束

(3)相关文件拷贝到开发板上

需要复制的文件有pppd、chat、pppdump、pppstats,这四个文件复制到开发板/sbin目录下,pppdump用来对ppp连接打印日志进行格式化处理,一般用不到。pppstats用来查看ppp连接的状态信息如网络速度等。pppd与chat是ppp连接的主程序。

ppp-on/ppp-on-dialer/ppp-off三个文件复制到/sbin目录下,保证有可执行权限。三个文件的内容如下:

ppp-on文件:
#!/bin/sh
#
# Script to initiate a ppp connection. This is the first part of the
# pair of scripts. This is not a secure pair of scripts as the codes
# are visible with the 'ps' command.  However, it is simple.
#
# These are the parameters. Change as needed.
#TELEPHONE=555-1212     # The telephone number for the connection
TELEPHONE=*99***1#      # The telephone number for the connection
#ACCOUNT=george         # The account name for logon (as in 'George Burns')
#PASSWORD=gracie                # The password for this account (and 'Gracie Allen')
LOCAL_IP=0.0.0.0        # Local IP address if known. Dynamic = 0.0.0.0
REMOTE_IP=0.0.0.0       # Remote IP address if desired. Normally 0.0.0.0
NETMASK=255.255.255.0   # The proper netmask if needed
#
# Export them so that they will be available at 'ppp-on-dialer' time.
export TELEPHONE ACCOUNT PASSWORD
#
# This is the location of the script which dials the phone and logs
# in.  Please use the absolute file name as the $PATH variable is not
# used on the connect option.  (To do so on a 'root' account would be
# a security hole so don't ask.)
#
DIALER_SCRIPT=/etc/ppp/ppp-on-dialer
#
# Initiate the connection
#
# I put most of the common options on this command. Please, don't
# forget the 'lock' option or some programs such as mgetty will not
# work. The asyncmap and escape will permit the PPP link to work with
# a telnet or rlogin connection. You are welcome to make any changes
# as desired. Don't use the 'defaultroute' option if you currently
# have a default route to an ethernet gateway.
#
exec /sbin/pppd debug lock modem nocrtscts /dev/ttyS1 115200 \
        asyncmap 20A0000 escape FF kdebug 0 $LOCAL_IP:$REMOTE_IP \
        noipdefault netmask $NETMASK defaultroute connect $DIALER_SCRIPT

ppp-on-dialer文件:
#!/bin/sh
#
# This is part 2 of the ppp-on script. It will perform the connection
# protocol for the desired connection.
#测试中发现该脚本总是不能正常执行,明明设计的是什么也不期望直接发送at,但实际测试发现
#程序实际运行的是首先期望at,如果收不到就出错退出!但是将exec去掉后运行正常,证明:exec
#运行时,参数中at之前的“”空被删除了,导致程序运行一直出错!!!
#exec chat -v                   
chat -v  \
        ABORT           BUSY                                            \
        ABORT           'NO ANSWER'                                     \
        ABORT           RING                                        \
        ''                      AT                          \
        OK                      AT+CGDCONT=1,"\"IP\"","\"CMNET\""       \
        OK                      AT+CGACT=1,1                            \
        OK                      ATDT$TELEPHONE              \
        CONNECT
#       ogin:--ogin:    $ACCOUNT                    \
#       assword:        $PASSWORD

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

四、应用测试

1)连接好GPRS模块,保证GPRS模块已经正确复位。 
2)开启syslogd服务 
保证能够查看调试日志,开启方法可以执行: 
#syslogd 
并建立/var/log 、/var/run 两个目录,否则会看不到日志,pppd程序也会报错。 
3)执行ppp-on进行拨号测试。

注意:

错误往往就在此时发生了,配置好内核中的ppp选项,编译好ppp-2.4.4并将生成的程序与脚本拷贝到目标板,配置好环境变量,基本上ppp拨号上网的基础准备就ok了,但是,由于各种各样因为脚本配置的问题都会导致ppp测试失败,有时候你废了很长时间一直找不到问题的原因,而其实原因很有可能就是脚本的配置原因。

以上脚本表面看着没问题,并且网上也有很多人安照上面的执行成功了,但是我进行测试时却废了一天多的时间一直没有调试成功,总是报出脚本连接错误之类的问题。最后实在没办法,只好在pppd与chat的源码中中添加了很多自定义的调试信息,一行行的跟踪ppp程序的运行。同时,监控GPRS串口才发现执行ppp-on后pppd程序根本没有向gprs串口发送任何AT指令,只在最后出错退出前打印了一条:exitcode 3!

在我最后实在找不到原因而从网上无意看到一句,然后灵光一闪,在命令行输入下面的指令:

$pppd connect 'chat -v "" "AT" "OK" "AT+CGDCONT=1,\"IP\",\"CMNET\"" "OK" "AT+CGACT=1,1" "OK" "ATDT*99***1#" "CONNECT"' user CARD password CARD /dev/ttyS1 115200 nodetach nocrtscts
  • 1

奇迹出现了,pppd程序正常运行了,最后pppd程序留下了下面的调试信息(我自己修改pppd源码打印出的信息,非标准):

initializer = (null)
connector = chat -v "" "AT" "OK" "AT+CGDCONT=1,\"IP\",\"CMNET\"" "OK" "AT+CGACT=1,1" "OK" "ATDT*99***1#" "CONNECT"
device_script chat -v "" "AT" "OK" "AT+CGDCONT=1,\"IP\",\"CMNET\"" "OK" "AT+CGACT=1,1" "OK" "ATDT*99***1#" "CONNECT",7
Serial connection established.
serial connection ok!
local = 0 modem = 1 ctrscts=-1
numbuf = 115200
Using interface ppp0
Connect: ppp0 <--> /dev/ttyS1
local  IP address 10.124.161.52     ---------拨号成功,获取到的IP
remote IP address 10.124.161.52

程序执行到此后永不退出,除非断线!!!!!!

在另一个控制台中执行ifconfig,结果如下:

# ifconfig
    eth0      Link encap:Ethernet  HWaddr 08:00:3E:26:1A:5B 
    inet addr:192.168.0.252  Bcast:192.168.0.255  Mask:255.255.255.0
    UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
    RX packets:986 errors:0 dropped:0 overruns:0 frame:0
    TX packets:758 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:1000
    RX bytes:65629 (64.0 KiB)  TX bytes:58426 (57.0 KiB)
    Interrupt:21 Base address:0x4000

    ppp0      Link encap:Point-to-Point Protocol 
    inet addr:10.124.161.52  P-t-P:10.124.161.52  Mask:255.255.255.255
    UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
    RX packets:6 errors:0 dropped:0 overruns:0 frame:0
    TX packets:7 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:3
    RX bytes:60 (60.0 B)  TX bytes:87 (87.0 B)

4)ping外网测试

输入命令:

ping –I ppp0 180.173.182.193

这里写图片描述

这个时候你只可以使用ip地址进行ping,如果执行ping www.baidu.com 会出现错误,原因是没有设置DNS服务器地址。为了设置DNS服务器地址,可以将主机上的/etc/resolv.conf文件直接拷贝到目标机上,如下所示: 
这里写图片描述

注意:有的文章上说建立/etc/ppp/resolv.conf文件,在此文件中指明dns服务器地址,但是实际测试发现不行,必需是/etc/resolv.conf才可以。 
设置好DNS后,直接ping域名进行测试,输入命令:

Ping –I ppp0 www.sina.com

这里写图片描述

5)telnet测试 
登录水木社区,输入命令:

telnet bbs.newsmth.net

这里写图片描述

注意:在做的时候将ARM板子上的网线拔掉!

至此,测试成功。但是前面使用脚本进行测试时为什么出错呢??

五、错误问题及原因分析

使用配置脚本的方法,为什么总是出错?为什么使用单命令行的方式却执行正确呢?

要弄明白原因,先分析下chat程序的工作原理。

1、chat 程序工作原理

chat 程序是一个自动聊天程序,程序非常简单,chat根据配置的信息主动向stderr 输出信息,同时,从stdin获得期望输入。chat程序输入端口为stdin,输入端口为stderr。所以,如果使用本程序进行拨号上网时,必需将stdin与stderr映射到对应的串口。

chat的配置信息来自两个地方: 
1. 命令行参数; 
2. chat配置脚本,使用参数-f 时指定。

chat的使用方法:

chat的参数遵循这样的模式:expect-send(期望-发送) 
例如:

ogin:-BREAK-ogin: ppp ssword: hello2u2

chat 首先会等待接收“ogin:”,如果超过一段时间没有收到,则发送 BREAK信号,然后再次等待“ogin: ”,如果收到则发送ppp。但是,如果一开始就收到了“orgin: ”则直接发送“ppp”,而不是BREAK信号。

BREAK信号是一个组合键,一般为同时按下控制键(Ctl)和Break键的组合值。chat程序会将-BREAK-替换为\K\c。

收到“ogin: ”后发送“ppp”,然后等待“ssword: ”收到后发送密码“hello2u2”,如果超过时间没有收到,则chat会打印相应的日志,然后退出程序。

需要注意的是,如果期望“ogin:”而实际收到的是“login: ”,那么chat会认为没有收到期望字符而失败!但是,如果期望“login”而收到“login:”这在不影响程序的正确执行。

#字符会被当做注释而忽略掉,如果确实需要使用#字符,可是使用'#'模式,如'#'  logout,则是期望收到#然后输出logout。
  • 1

ABORT BUSY ABORT 'NO CARRIER' '' ATZ OK ATDT5551212 CONNECT

这就话的意思是,chat什么也不期望(” 表示什么也不期望),直接发送ATZ,然后期望OK,收到OK后发送ATDT5551212,然后再期望CONNECT,但是如果收到BUSY/NO CARRIER 这一些字符则发生ABORT,即chat程序退出。

chat具体用法可以使用man chat 查看。

2、结论

根据上文chat原理进行综合分析,最终确认