Linux 下 ppp 拨号
版本
版本
1.0
1.1
1.2
内容
时间
2011-07-18 添加 WCDMA 拨号
2011-07-28 添加 CDMA2000 拨号
2011-07-29 添加 TD-SCDMA 拨号
添加附录、常见问题以及常用 AT 命令
拨号流程
本节以 WCDMA 为例,详细描述了 Linux 环境下中国联通 WCDMA 的 ppp 拨号上网流程。对
中国电信 CDMA2000 和中国移动 TD-SCDMA 也给出了参考。
Linux 下用 ppp 上网需要两个程序:pppd 和 chat 。这两个程序有开源代码,可以下载后编
译得到。我们的系统中已经有了,不再赘述。ubuntu 也自带 pppd 和 chat,可以使用 man
查看其具体使用方法。
典型的 ppp 拨号需要准备几个文件
1. pppd 脚本
2. chat 脚本
3. chap-secets 文件
4. pap-secets 文件
WCDMA
本节使用的是 huawei em770w 模块,使用中国联通 WCDMA。
pppd 脚本
pppd 脚本默认放在/etc/ppp/peers/ 目录下,比如我们使用 wcdma 这个名字为这个脚本的
文件名
使用命令
#pppd call wcdma
这样就启动了 pppd 程序
pppd 程序的作用就是建立协议,生成网络接口 ppp0(如果第一个).下面是一个 pppd call 脚本
的例子:
#/etc/ppp/peers/wcdma
#This is pppd script, used Huawei EM770W(Union)
/dev/ttyUSB0
115200
crtscts
modem
debug
#nodetach
usepeerdns
defaultroute
user "3gnet"
0.0.0.0:0.0.0.0
connect '/usr/sbin/chat -s -v -f /etc/ppp/wcdma-connect-chat'
有关 pppd 更多的意义请直接使用 linux 帮助 man pppd
下面是一些重要参数的解释
参数
/dev/ttyUSB0
意义
接口
接口波特率
接口带硬件流控
这个参数使得 pppd 进程将等待模块发回的 CD (Carrier Detect)信号,与
local 真好相反
输出调试信息
不后台运行,默认是后台运行的
使用 dns
本地和远端的 ip 都设为 0 使得接入的 isp 分配本地的 ip 地址
用户名
调用连接 chat 脚本
115200
crtscts
modem
debug
nodetach
usepeerdns
defaultroute
user “3gnet”
connect ‘…’
chat 脚本
chat 脚本是真正 AT 指令的发送和接收脚本,下面是一个 chat 脚本
#/etc/ppp/wcdma-connect-chat
TIMEOUT 5
ABORT "DELAYED"
ABORT "BUSY"
ABORT "ERROR"
ABORT "NO DIALTONE"
ABORT "NO CARRIER"
#"AT
#'OK-+++\c-OK' ATH0
TIMEOUT 5
'' AT
OK AT+CGDCONT=1,"IP","3gnet",,0,0
OK ATDT*99#
CONNECT ''
chat 脚本主要有 TIMEOUT 、ABORT 和 AT 指令交互等等构成。更多帮助使用 man chat。
TIMEOUT 不解释了,ABORT 指定了 AT 指令交互时,出了什么样的错误 chat 将退出。AT 指
令交互是”接收” “发送”的形式。
比如第一行,意思是不接收任何信息,直接发送 AT
'' AT
第二行的意思是等待接收 OK,如果 OK,发送 AT+CGDCONT=1…这个命令。
如果一切正常,会看到如下 log。我们可以在 log 看到 3g 模块上 AT 指令的交互情况
~ # pppd call wcdma
timeout set to 5 seconds
abort on (DELAYED)
abort on (BUSY)
abort on (ERROR)
abort on (NO DIALTONE)
abort on (NO CARRIER)
timeout set to 5 seconds
send (^MAT^M)
expect (OK)
A^M^M
OK
-- got it
send (AT+CGDCONT=1,"IP","3gnet",,0,0^M)
expect (OK)
^M
AT+CGDCONT=1,"IP","3gnet",,0,0^M^M
OK
-- got it
send (ATDT*99#^M)
expect (CONNECT)
^M
ATDT*99#^M^M
CONNECT
-- got it
send (^M)
Serial connection established.
using channel 9
Using interface ppp0
Connect: ppp0 <--> /dev/ttyUSB0
sent [LCP ConfReq id=0x1
]
rcvd [LCP ConfReq id=0x20
]
No auth is possible
sent [LCP ConfRej id=0x20 ]
rcvd [LCP ConfAck id=0x1 ]
rcvd [LCP ConfReq id=0x21 ]
sent [LCP ConfAck id=0x21 ]
sent [CCP ConfReq id=0x1 ]
sent [IPCP ConfReq id=0x1 ]
rcvd [LCP DiscReq id=0x22 magic=0x172fd2b]
rcvd [LCP ProtRej id=0x23 80 fd 01 01 00 0f 1a 04 78 00 18 04 78 00 15 03 2f]
Protocol-Reject for 'Compression Control Protocol' (0x80fd) received
rcvd [IPCP ConfNak id=0x1 ]
sent [IPCP ConfReq id=0x2
]
rcvd [IPCP ConfNak id=0x2 ]
sent [IPCP ConfReq id=0x3
]
rcvd [IPCP ConfReq id=0x10]
sent [IPCP ConfNak id=0x10 ]
rcvd [IPCP ConfRej id=0x3 ]
sent [IPCP ConfReq id=0x4 ]
rcvd [IPCP ConfReq id=0x11]
sent [IPCP ConfAck id=0x11]
rcvd [IPCP ConfNak id=0x4 ]
sent [IPCP ConfReq id=0x5 ]
rcvd [IPCP ConfAck id=0x5
202.99.166.4>]
Could not determine remote IP address: defaulting to 10.64.64.64
not replacing existing default route via 192.168.8.254
local IP address 172.20.255.208
remote IP address 10.64.64.64
primary DNS address 202.99.160.68
secondary DNS address 202.99.166.4
chap-secets 和 pap-secets 文件
chap-secets 和 pap-secets 文件是用于 peer 需要 chap 或者 pap 认证的服务器,我们好像用
不到,有兴趣的可以验证一下。需要说明的是如果需要认证,chat 脚本中,user 3gnet 的 3gnet
就是用户名。下面是一个 chap-secets 例子
#/etc/ppp/chap-secrets
#client server secret IP address
"3gnet" * "3gnet" *
有关 CHAP 和 PAP 认证,可以参考
http://download.oracle.com/docs/cd/E19253-01/819-7059/pppsvrconfig.reference-fig-23/index.
html
CDMA2000/EV-DO
本节使用的是 huawei em660 模块,使用中国电信 CDMA2000/EV-DO。
pppd 脚本
路径 /etc/ppp/peers/cdma2000
#/etc/ppp/peers/cdma2000
#This is pppd script, used Huawei EM660
/dev/ttyUSB0
115200
crtscts
modem
debug
nodetach
usepeerdns
defaultroute
user "ctnet@mycdma.cn"
password "vnet.mobi"
#user "card"
#password "card"
0.0.0.0:0.0.0.0
connect '/usr/sbin/chat -s -v -f /etc/ppp/cdma2000-connect-chat'
chat 脚本
#/etc/ppp/cdma2000-connect-chat
TIMEOUT 5
ABORT "DELAYED"
ABORT "BUSY"
ABORT "ERROR"
ABORT "NO DIALTONE"
ABORT "NO CARRIER"
'' AT
'OK-+++\c-OK' ATH0
TIMEOUT 40
'' AT
OK ATDT#777
CONNECT ''
如果不出问题,有如下信息
~ # pppd call cdma2000
timeout set to 5 seconds
abort on (DELAYED)
abort on (BUSY)
abort on (ERROR)
abort on (NO DIALTONE)
abort on (NO CARRIER)
send (AT^M)
expect (OK)
A^M^M
OK
-- got it
send (ATH0^M)
timeout set to 40 seconds
send (AT^M)
expect (OK)
^M
ATH0^M^M
OK
-- got it
send (ATDT#777^M)
expect (CONNECT)
^M
AT^M^M
OK^M
ATDT#777^M^M
CONNECT
-- got it
send (^M)
Serial connection established.
using channel 1
Using interface ppp0
Connect: ppp0 <--> /dev/ttyUSB0
rcvd [LCP ConfReq id=0x1
]
sent [LCP ConfReq id=0x1 ]
sent [LCP ConfAck id=0x1 ]
rcvd [LCP ConfAck id=0x1 ]
rcvd
"utstar.com"]
sent
"ctnet@mycdma.cn"]
rcvd [CHAP Success id=0x2 "\000"]
CHAP authentication succeeded:
CHAP authentication succeeded
sent [CCP ConfReq id=0x1 ]
sent [IPCP ConfReq id=0x1 ]
rcvd [LCP ProtRej id=0x1 80 fd 01 01 00 0f 1a 04 78 00 18 04 78 00 15 03 2f]
Protocol-Reject for 'Compression Control Protocol' (0x80fd) received
rcvd [IPCP ConfReq id=0x3 ]
sent [IPCP ConfAck id=0x3 ]
rcvd [IPCP ConfNak id=0x1 ]
sent [IPCP ConfReq id=0x2 ]
id=0x2 , name =
[CHAP Response
[CHAP Challenge
id=0x2 <00eed0d70ca12053417660bc4c6c2bc7>, name =
rcvd [IPCP ConfAck id=0x2
]
not replacing existing default route via 192.168.8.254
local IP address 114.81.240.179
remote IP address 115.168.41.17
primary DNS address 116.236.159.8
secondary DNS address 222.66.251.8
TD-SCDMA
本节使用的是 zte mu301 模块,使用中国移动 TD-SCDMA。
mu301模块不是很稳定,比如AT+CGDCONT 和AT+CFUN命令都不返回OK,和移动TD-SCDMA
提供的AT 命令接口规范不符。
pppd 脚本
#/etc/ppp/peers/cdma2000
#This is pppd script, used Huawei EM660
/dev/ttyUSB2
115200
crtscts
modem
debug
nodetach
usepeerdns
defaultroute
user "cmnet"
connect '/usr/sbin/chat -s -v -f /etc/ppp/tdscdma-connect-chat'
chat 脚本
#/etc/ppp/tdscdma-connect-chat
TIMEOUT 5
ABORT "DELAYED"
ABORT "BUSY"