一、Linux上使用pppd来实现拨号上网。我们首先要确认 Linux 系统是否支持 PPP 功能。如果当前 kernel 不支持 PPP,需要重新编译生成一个支持 PPP 的 kernel。(所幸现在linux内核基本都支持PPP)。
二、若以存在pppd,下面要做的就是要用pppd来连接网络,因为pppd的选项比较复杂,所以我们可以用脚本来配置pppd,首先建立三个脚本,下面分别给出该三个脚本的基本情况:
ppp-on: 为PPP 连接脚本;该脚本放到 /usr/sbin 。
ppp-off: 为PPP 断开脚本 ;该脚本放到 /usr/sbin 。
ppp-on-dialer: 为PPP 登录会话脚本 ;该脚本放到 /etc/ppp 。
三、编写拨号程序
该程序分别在这三个脚本中,并有详细说明。编写完拨号程序之后我们就可以运行脚本程序来进行网络连接的测试。运行前先用ifconfig eth0 down关闭网路,查看出错信息用tail -f /var/log/messages。
四、配置DNS,因为 PPP 在同 ISP 连接后,ISP会自动 客户机提供 IP 地址和 DNS 服务器地址,但Linux 的 PPP 不会自动接收 DNS 地址,Linux 上的 PPP 在协议连接成功后,要手动进行 DNS 的配置。
方法是在 /etc/resolv.conf 中加入一行:
nameserver xxx.xxx.xxx.xxx
注意 nameserver 后面是 ISP 的 DNS 服务器的 IP 地址,并且这一行说明必须放在其他的nameserver 说明之前,否则, Linux 系统不会访问 ISP 的 DNS 服务器。然后,就能使用各项 Internet 服务了。
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=*99***1# # The telephone number for the connection
ACCOUNT= # The account name for logon (as in 'George Burns')
PASSWORD= # 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 /usr/sbin/pppd debug lock modem crtscts /dev/ttyS0 115200 \
asyncmap 20A0000 escape FF kdebug 0 $LOCAL_IP:$REMOTE_IP \
noipdefault netmask $NETMASK defaultroute connect $DIALER_SCRIPT
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
ppp-on-dialer
#!/bin/sh
#
# This is part 2 of the ppp-on script. It will perform the connection
# protocol for the desired connection.
#
exec chat -v \
TIMEOUT 3 \
ABORT '\nBUSY\r' \
ABORT '\nNO ANSWER\r' \
ABORT '\nRINGING\r\n\r\nRINGING\r' \
'' \rAT \
'OK-+++\c-OK' ATH0 \
TIMEOUT 30 \
OK AT+IPR=115200 \
OK AT+CGDCONT=1,"IP","CMNET" \
OK AT+CGACT=1,1 \
OK ATDT$TELEPHONE \
CONNECT '' \
若用GPRS上网则后面的用户名和密码不需要。
ogin:--ogin: $ACCOUNT \
assword: $PASSWORD