模块: 上海移远的 UC15 模块.
环境: Linux 系统.
一般情况下, 创建一个 PPP 连接, 可以执行如下命令:
root@~# pppd call connect_name
在这里, connect_name
是 PPP 拨号脚本, 拨号脚本包含三个脚本, 一般命令为 "myppp", "myppp-chat-connect", "myppp-chat-disconnect", 看文件名也能大概知道, 这三个文件分别是用来配置, 连接和断开连接的脚本.
1. 创建配置脚本
文件名1: myppp
#/etc/ppp/peers/file_name
# Usage: root> pppd call file_name
# 调试时隐藏密码
hide-password
# 该手机不需要身份验证
noauth
#############################################
# 注意: 这里要知道chat安装的位置, 如果不在环境变量
# 里面, 则要指定位置(如 /usr/sbin/chat -s -v -f)
#############################################
# 用于呼叫控制脚本
connect 'chat -s -v -f /etc/ppp/peers/myppp-chat-connect'
# 断开连接脚本
disconnect 'chat -s -v -f /etc/ppp/peers/myppp-chat-disconnect'
# 调试信息
debug
# 模块串口设备
/dev/ttyUSB3
# 串口波特率
115200
# 使用默认路由
defaultroute
# 不能指定默认IP
noipdefault
# No ppp compression
novj
novjccomp
noccp
ipcp-accept-local
ipcp-accept-remote
local
# 最好锁定串行总线
lock
dump
nodetach
# Keep pppd attached to the terminal
# 用户名 密码
# user root
# password
# 硬件控制流
crtscts
remotename 3gppp
ipparam 3gppp
# 向对方请求最多2个DNS服务器地址
usepeerdns
文件名2: myppp-chat-connect
TIMEOUT 15
# 连续15秒, 收到以下字符, 退出执行
ABORT "BUSY"
ABORT "NO CARRIER"
ABORT "NO DIALTONE"
ABORT "ERROR"
ABORT "NO ANSWER"
TIMEOUT 40
# 40秒内没有收到指定字符, 退出
# 如: OK \rATZ, 发送 ATZ, 希望收到的是 OK.
"" AT
OK \rATZ
# 建立连接: 这里连接的是联通3G, 可以根据需要修改 3gnet.
OK \rAT+CGDCONT=1,"IP","3gnet",,0,0
# 拨号
OK-AT-OK ATDT*99#
CONNECT \d\c
文件名3: myppp-chat-disconnect
ABORT "ERROR"
ABORT "NO DIALTONE"
SAY "\NSending break to the modem\n"
""\k"
""+++ATH"
SAY "\nGood bye !\n"
2. 放置文件, 运行 pppd 拨号
创建好这三个文件之后, 将他们放到 /etc/ppp/peers
目录下, 然后执行命令进行拨号:
root@~# pppd call myppp & # & 后台运行
debug # (from /etc/ppp/peers/myppp)
nodetach # (from /etc/ppp/peers/myppp)
dump # (from /etc/ppp/peers/myppp)
noauth # (from /etc/ppp/peers/myppp)
user root # (from /etc/ppp/peers/myppp)
password ?????? # (from /etc/ppp/peers/myppp)
remotename 3gppp # (from /etc/ppp/peers/myppp)
/dev/ttyUSB3 # (from /etc/ppp/peers/myppp)
115200 # (from /etc/ppp/peers/myppp)
lock # (from /etc/ppp/peers/myppp)
connect chat -s -v -f /etc/ppp/peers/myppp-chat-connect # (from /etc/ppp/peers/myppp)
disconnect chat -s -v -f /etc/ppp/peers/myppp-chat-disconnect # (from /etc/ppp/peers/cellul)
local # (from /etc/ppp/peers/myppp)
hide-password # (from /etc/ppp/peers/myppp)
novj # (from /etc/ppp/peers/myppp)
novjccomp # (from /etc/ppp/peers/myppp)
ipcp-accept-local # (from /etc/ppp/peers/myppp)
ipcp-accept-remote # (from /etc/ppp/peers/myppp)
ipparam 3gppp # (from /etc/ppp/peers/myppp)
noipdefault # (from /etc/ppp/peers/myppp)
defaultroute # (from /etc/ppp/peers/myppp)
usepeerdns # (from /etc/ppp/peers/myppp)
noccp # (from /etc/ppp/peers/myppp)
abort on (BUSY)
abort on (NO CARRIER)
abort on (NO DIALTONE)
abort on (ERROR)
abort on (NO ANSWER)
timeout set to 40 seconds
send (AT^M)
expect (OK)
^M
OK
-- got it
send (^MATZ^M)
expect (OK)
^M
^M
OK
-- got it
send (^MAT+CGDCONT=1,"IP","3gnet",,0,0^M)
expect (OK)
^M
^M
OK
-- got it
send (ATDT*99#^M)
expect (CONNECT)
^M
^M
CONNECT
-- got it
send (\d)
Script chat -s -v -f /etc/ppp/peers/myppp-chat-connect finished (pid 1395), status = 0x0
Serial connection established.
using channel 2
Using interface ppp0
Connect: ppp0 <--> /dev/ttyUSB3 # 设备端口, 拨号端口
sent [LCP ConfReq id=0x1 <asyncmap 0x0> <magic 0xaf2749e0> <pcomp> <accomp>]
rcvd [LCP ConfReq id=0x2 <asyncmap 0x0> <auth chap MD5> <magic 0x19a6ae9> <pcomp> <accomp>]
sent [LCP ConfAck id=0x2 <asyncmap 0x0> <auth chap MD5> <magic 0x19a6ae9> <pcomp> <accomp>]
rcvd [LCP ConfAck id=0x1 <asyncmap 0x0> <magic 0xaf2749e0> <pcomp> <accomp>]
rcvd [LCP DiscReq id=0x3 magic=0x19a6ae9]
rcvd [CHAP Challenge id=0x1 <003eb1ea60ea5d1a2652c60bd4423b09>, name = "UMTS_CHAP_SRVR"]
sent [CHAP Response id=0x1 <e38c482f6418284e8c7473efddcfe0fd>, name = "root"]
rcvd [CHAP Success id=0x1 ""]
CHAP authentication succeeded
CHAP authentication succeeded
sent [IPCP ConfReq id=0x1 <addr 0.0.0.0> <ms-dns1 0.0.0.0> <ms-dns2 0.0.0.0>]
rcvd [proto=0x3] 01 00 04
Unsupported protocol 0x3 received
sent [LCP ProtRej id=0x2 00 03 01 00 04]
rcvd [IPCP ConfReq id=0x2]
sent [IPCP ConfNak id=0x2 <addr 0.0.0.0>]
rcvd [IPCP ConfNak id=0x1 <addr 10.30.86.71> <ms-dns1 58.22.96.66> <ms-dns2 218.104.128.106>]
sent [IPCP ConfReq id=0x2 <addr 10.30.86.71> <ms-dns1 58.22.96.66> <ms-dns2 218.104.128.106>]
rcvd [IPCP ConfReq id=0x3]
sent [IPCP ConfAck id=0x3]
rcvd [IPCP ConfAck id=0x2 <addr 10.30.86.71> <ms-dns1 58.22.96.66> <ms-dns2 218.104.128.106>]
Could not determine remote IP address: defaulting to 10.64.64.64
local IP address 10.30.86.71
remote IP address 10.64.64.64
primary DNS address 58.242.250.6
secondary DNS address 218.104.78.2
成功拨号之后, 将返回两个 DNS 服务器地址:
primary DNS address 58.242.250.6
secondary DNS address 218.104.78.2
3. 修改 DNS 服务器地址
然后将返回的 DNS 地址设置到 /etc/resolv.conf
文件里就可以了:
nameserver 58.242.250.6
nameserver 218.104.78.2
4. 最后, 添加路由 (如果本来没有其他网卡和路由表 [default], 可以直接上网, 不需要这步)
需要添加路由, 是因为, pppd 拨号时虚拟出了一个网卡, 我们要通过这个网卡来上网, 使用 ifconfig
命令, 可以看到多了一个网卡, 一般是 ppp0
:
ppp0 Link encap:Point-to-Point Protocol
inet addr:10.186.110.45 P-t-P:10.64.64.64 Mask:255.255.255.255
我们要通过它来上网, 就要把它加到路由表里, 默认路由如:
root@ROUTER:~# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.64.64.64 * 255.255.255.255 UH 0 0 0 ppp0
然后, 我们把默认路由添加进来, 如果已经存在默认路由 default
, 先删除它:
root@ROUTER:~# route del default
root@ROUTER:~# route add default dev ppp0
root@ROUTER:~# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default * 0.0.0.0 U 0 0 0 ppp0
10.64.64.64 * 255.255.255.255 UH 0 0 0 ppp0
然后可 ping
一下试试:
root@ROUTER:~# ping www.baidu.com
PING www.a.shifen.com (163.177.151.109) 56(84) bytes of data.
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=1 ttl=53 time=159 ms
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=2 ttl=53 time=157 ms
^C
这就可以上网了 !
[reference]
[1] WE4U220099. pppd拨号脚本说明文档 Baidu文库. 2017-09-26
网友评论