【原创】ppp拨号上网总结
来源:互联网 发布:2k17乔丹捏脸详细数据 编辑:程序博客网 时间:2024/04/29 16:07
【原创】ppp拨号上网总结
Author: chad
Mail: linczone@163.com
本文可以自由转载,但转载请务必注明出处以及本声明信息。
一、开发环境简述
- GPRS模块 MC52I
- SIM卡 移动
- 嵌入式开发板 AT91SAM9260EK
- linux版本 2.6.30
- 主机环境 ubuntu
- ppp-2.4.4 拨号程序源码包见本笔记附件。
- 附件内为运行正常的ppp-on与ppp-on-dialer脚本,使用时放在/sbin目录下即可。
二、主要步骤
- 内核添加PPP支持。
- 下载ppp-2.4.4源码进行编译。
- 移植到开发板进行测试。
三、配置详细说明
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 -lcrw-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 connectionTELEPHONE=*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.0REMOTE_IP=0.0.0.0 # Remote IP address if desired. Normally 0.0.0.0NETMASK=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_SCRIPTppp-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: $PASSWORDppp-off文件:#!/bin/sh######################################################################## Determine the device to be terminated.#if [ "$1" = "" ]; then DEVICE=ppp0else DEVICE=$1fi######################################################################## 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 0fi## The ppp process is not running for ppp0echo "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
奇迹出现了,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",7Serial connection established.serial connection ok!local = 0 modem = 1 ctrscts=-1numbuf = 115200Using interface ppp0Connect: ppp0 <--> /dev/ttyS1local IP address 10.124.161.52 ---------拨号成功,获取到的IPremote 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。
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原理进行综合分析,最终确认原因已经记录在配着脚本ppp-on-dialer中,如下:
# vim ppp-on-dialer 1 #!/bin/sh 2 # 3 # This is part 2 of the ppp-on script. It will perform the connection 4 # protocol for the desired connection. 5 # 6 # 7 #测试中发现该脚本总是不能正常执行,明明设计的是什么也不期望直接发送at,但实际测试发现 8 #程序实际运行的是首先期望at,如果收不到就出错退出!但是将exec去掉后运行正常,证明:exec 9 #运行时,参数中at之前的“”空被删除了,导致程序运行后直接期望的是at而不是什么不期望发送at,进而导致一直出错!!!10 #修改为如下指令后就可正常运行: 11 #exec chat -v \ 12 chat -v \ 13 ABORT BUSY \ 14 ABORT 'NO ANSWER' \ 15 ABORT RING \ 16 '' AT \ 17 OK AT+CGDCONT=1,"\"IP\"","\"CMNET\"" \ 18 OK AT+CGACT=1,1 \ 19 OK ATDT$TELEPHONE \ 20 CONNECT 21 # ogin:--ogin: $ACCOUNT \ 22 # assword: $PASSWORD
- 【原创】ppp拨号上网总结
- ppp拨号上网
- linux PPP拨号上网
- ppp拨号上网(1)
- ppp拨号上网(2)
- sim900GPRS模块ppp拨号上网
- SIM5360E模块ppp拨号上网
- Linux下PPP拨号上网设置
- GPRS模块在Linux平台上ppp拨号上网总结与心得
- GPRS模块在Linux平台上ppp拨号上网总结与心得
- GPRS模块在Linux平台上ppp拨号上网总结与心得
- GPRS模块在Linux平台上ppp拨号上网总结与心得
- GPRS模块在Linux平台上ppp拨号上网总结与心得
- GPRS模块在Linux平台上ppp拨号上网总结与心得
- GPRS模块在Linux平台上ppp拨号上网总结与心得
- GPRS模块在Linux平台上ppp拨号上网总结与心得
- 使用wvdial启动ppp协议拨号上网
- linux下ppp拨号上网
- 指定的网络名不再可用
- OC 数组排序方法论
- JAVA设计模式:观察者模式
- Java Servlet PDF文件生成
- [C/C++]_[初级]_[标准字符串std::string 所有函数的用法总结]
- 【原创】ppp拨号上网总结
- Qt4.8.5 配置mysql驱动
- 重载流插入运算符和流提取运算符
- jnlp——通过浏览器直接执行java应用程序
- [150522]Linux常用系统命令
- JAVA设计模式:工厂方法模式
- XPower 6100 Freescale T4240 6U VPX模块
- EL表达式调用函数
- 获取 鼠标在网页上的位置 以及右击事件