0

关于投票
用wvdial和ppp轻松上网

wvdial是linux下的智能化拨号工具,利用wvdial和ppp可以实现linux下的轻松上网。在整个过程中wvdial的作用是拨号并等待提示,并根据提示输入相应的用户名和密码等认证信息;ppp的作用是与拨入方协商传输数据的方法并维持该连接。

一、wvdial及其相关配置 wvdial的功能很强大,会试探着去猜测如何拨号及登录到服务器,同时它还会对常见的错误智能的进行处理,不象chat一样,要求你去写登录脚本。wvdial只有一个配置文件 /etc/wvdial.conf。wvdial的启动过程是这样的:首先载入wvdial.conf配置文件,然后再初始化modem并拨号,拨号后等待拨入方的响应,收到拨入方响应后则启动pppd。

可以用wvdialconf程序自动生成wvdial.conf配置文件,自行该程序的格式为: wvdialconf /etc/wvdial.conf 在执行该程序的过程中,程序会自动检测你的modem的相关配置,包括可用的设备文件名,modem的波特率,初始化字符等等相关的拨号信息,并根据这些信息自动生成wvdial.conf配置文件。如果/etc/wvdial.conf文件已经存在时,再次执行该命令只会改变其中的 Modem、Band、Init等选项。一个典型的自动生成的配置文件可能是这样的:

[Dialer Defaults]

Modem = /dev/ttyS1 Baud = 115200

Init1 = ATZ

Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 S11=55 +FCLASS=0

;Phone =

;Username =

;Password =

wvdial的执行格式为: wvdial --help | --version | section 相关的说明如下: --help:显示简单的帮助信息 --version:显示wvdial的版本号 section:这里的section有点象windows里的ini文件,一个wvdial.conf配置文件可以有好多个section,每一个section由一些变量组成,即由 变量=值 的语句组成,如上所示。使用wvdialconf自动生成配置文件时将会自动生成一些常用的变量说明如下:

Inherits=InheritedSection: 使用wvdialconf自动生成配置文件时将会自动生成[Dialer Defaults],除此之外,你还 可以自定义你自己的section。程序运行时,首先载入[Dialer Defaults],然后再用指 定的section的相应选项覆盖[Dialer Defaults]的相应选项。比如,我们在 wvdial.conf中还有[Dialer Tom],假如我们运行wvdail Tom,则系统将先读入[Dialer Defaults],然后再用[Dialer Tom]覆盖[Dialer Defaults]的相应选项。如果除了以上 的section之外还有[Dialer 169]并且内容如下: [Dialer Tom] Username="tom" Password="xxx" Inherits="169" [Dialer 169] Phone="169" 如果这时候我们执行wvdial Tom则系统将先读入[Dialer Defaults],然后再用[Dialer Tom]覆盖[Dialer Defaults]的相应选项,最后再用[Dialer 169]的相应选项来覆盖前二者的相应选项。由此可见,利用wvdial,我们可以很方便地在不同的ISP或modem之间来回移动(假如你有几个ISP或medom的话)。

Medom=/dev/ttySx:用于指定是用的medom,缺省的为/dev/medom。当然,在这里我们的medom是由wvdialconf自动检测并配置的,所以我们可以忽略该变量。

Band=57600:wvdial与modem通信的波特率,同上可以忽略。

Area Code="xxxx" : 设置区号

Dial Prefix="x:" 假如你正在使用分机,拨外线需拨9时,可设该值为9。

Username=xxxx:登录时的用户名

Passwd=xxxxxx:登录密码

Phone=xxxxx: 所拨的号码

PPPP Path=":设置pppd所在的路径",缺省为/usr/sbin/pppd

Force Address="x".x.x.x :设置静态ip,一般的isp都会为你分配动态的ip地址。

New PPPD= 1 or 0: pppd 2.3.0及其以上版本需要/etc/ppp/peers/wvdial文件,如果你的pppd是2.3.0以上版本请设为1.

Auto Reconnect="on" :断线时是否自动重新连接,缺省设为是。

以上只是wvdial.conf中的常用选项,具体情参考wvdial手册。

二、pppd及其相关配置 pppd的配置选项相对要复杂得多,你可以用命令行的形式引用有关的选项,也可以把要引用的选项写到/etc/ppp/options中进行引用。 下面的示例文件包含了最常用的选项及其相关的说明: # /etc/ppp/options

# 主机名称 mng.null.edu.cn

# 如果没有给定本地ip, pppd 将使用主机的第一个ip地址; # 如果指定了"noipdefault" 选项, pppd将使用拨入方提供的ip地址

noipdefault

# 选定该选项, pppd 将接受拨入方提供的ip地址

ipcp-accept-local

# 选定该选项, pppd 将接受拨入方自己的ip地址

ipcp-accept-remote

# 设置缺省网关 defaultroute

# 在传输数据包之前,让拨入方先自我认证,注意一般的ISP(如169、163)都不包含该机 # 制,故应选中noauth noauth

# 使用硬件流控制 crtscts

#将拨号信息作日志 debug

以上只是options中的常用选项,具体情参考pppd手册。

三、一个实例 下面让我们来看一个具体的应用,在该应用中我们实现了拨号和断开连接的自动化。涉及到的配置文件有: /etc/wvdial.conf :wvdial的配置文件 /etc/ppp/option : pppd的配置文件 /etc/ppp/ppp-on :拨号自动化脚本 /etc/ppp/ppp-of : 断开连接自动化脚本

步骤一:确保modem已经正确连接,linux已检测并自动配置了该modem

步骤二:运行wvdialconf,生成 /etc/wvdial.conf 文件并修改如下: [Dialer Defaults]

Modem = /dev/ttyS1 Baud = 115200

Init1 = ATZ

Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 S11=55 +FCLASS=0

New PPPD= 1 [Dialer Tom] Phone = 169 Username = tom Password = *******

步骤三:编辑并配置/etc/ppp/options,如下所示: noipdefault

ipcp-accept-local

ipcp-accept-remote

defaultroute

noauth

crtscts

debug

步骤四:编写自动化脚本 1.拨号脚本 /etc/ppp/ppp-on: #!/bin/sh # This script initiates the ppp connections by wvdial wvdial tom &

2.断开连接自动化脚本/etc/ppp/ppp-off: #!/bin/sh

#!stop wvdial killall wvdial # If the ppp0 pid file is present then the program is running. Stop it

if [ -r /var/run/ppp0.pid ]; then

kill -INT `cat /var/run/ppp0.pid`

echo "PPP link to ppp0 terminated."

else

echo "ERROR: PPP link is not active on ppp0"

exit 0

fi

exit 1

编辑完后,执行 chmod u+x /etc/ppp/ppp* 改变文件权限。到此大功告成,以后只要运行 /etc/ppp/ppp-on 便自动拨号,执行 /etc/ppp/ppp-of 便自动断开连接。

系统分类: 自由话题
用户分类: UNIX
标签: LINUX
来源: 转贴
发表评论 阅读全文(104) | 回复(0)

0

关于投票
如何用LINUX拨号上网
一、Linux 上的 PPP
Linux 中已经包括了点对点协议PPP,其主体是 pppd ,如果你已经使用过 Windows95 或 NT 的拨号网络,
那么你已经具备了 PPP 的基本经验了。在 Windows95 的 PPP 在同 ISP 连接后,视 ISP 情况不同,会自
动为 Windows 客户机提供 IP 地址和 DNS 服务器地址,而 Linux 的 PPP 不会自动接收 DNS 地址,这是 
Linux 与 Windows 的一点区别,所以,Linux 上的 PPP 在协议连接成功后,要多一步 DNS 的配置。 

二、Kernel 的准备
你的 Linux 系统必须在 kernel 中配置支持 PPP 功能。如果你的 kernel 目前不支持 PPP,你需要重新生
成一个支持 PPP 的 kernel。重新编译 kernel 的方法可看相关 Howto。 

三、用辅助脚本配置 PPP 拨号网络
我一开始想运行 pppd 来连接拨号网络,但很快发现这是太麻烦了,因为选项太多。后来我发现,其实在 
Linux 上已经提供了几个 PPP 拨号程序模版,只要对这些模板略作修改,就可轻松联上 PPP 了。根据我
的经验,如果顺利的话,10分钟就能完成配置。 

(1)拨号程序所在的目录 

对于 RedHat 5.0,这几个拨号程序(Script)在 /usr/doc/ppp-2.2.0f/scripts 下,如果要使用这些脚本,
需要把它们拷贝到正确的目录中,具体如下: 
ppp-on: PPP 连接脚本 拷贝到 /usr/sbin 
ppp-off: PPP 断开脚本 拷贝到 /usr/sbin 
ppp-on-dialer: PPP 登录会话脚本 拷贝到 /etc/ppp 
ppp-redialer: 可轮循多个电话号码的登陆脚本 拷贝到 /usr/sbin (本文不用) 
对于 Slackware 的用户,这些脚本已经安装到正确的目录了,无需做什么。
假定你在你的系统上找不到上述文件,可直接在本文后面的附录中cut。 

(2)创建自己的拨号程序 

假定现在我们要用 PPP 连接 163 网,我们就可以利用上述脚本来作很简单的配置。虽然我们可以直接在这
些模板上做修改,我还是建议为自己拷贝一份模板,在新的拷贝上作修改比较好。所以,我们先做如下拷贝: 

cd /usr/sbin
cp ppp-on ppp.163
chmod +x ppp.163
cd /etc/ppp
cp ppp-on-dialer dialer.163
chmod +x dialer.163

此外,要保证 ppp-off 也是可执行的 

(3)定制自己的拨号程序 

首先编辑你的 ppp.163,其中多数内容都不必改动,只要修改以下几处: 

TELEPHONE=163
ACCOUNT=
PASSWORD=
DIALER_SCRIPT=/etc/ppp/dialer.163

另外,在最后的 exec 启动 pppd 一行里,注意提供你的 Modem 所在的串口设备文件(通常是
 COM1 /dev/cua0 或 /dev/ttyS0,COM2 /dev/cua1 或 /dev/ttyS1),并设置串口的通信速度(缺省是 38400,
 对于现在的高速 Modem,这个速度太低了,可设置成 57600以上)。完成后的 ppp.163 内容是: 

#!/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=163 # 拨号号码
ACCOUNT=YourUserName # 用户名 
PASSWORD=YourPassword # 口令,注意限制其他人读取本文件!
LOCAL_IP=0.0.0.0 # 本地IP地址,0.0.0.0 表示动态分配
REMOTE_IP=0.0.0.0 # 原端IP地址,一般是 0.0.0.0
NETMASK=255.255.255.0 # 子网掩码
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/dialer.163 # 拨号登录脚本文件名
#
# 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/ttycua0 57600 \
asyncmap 20A0000 escape FF kdebug 0 $LOCAL_IP:$REMOTE_IP \
noipdefault netmask $NETMASK defaultroute connect $DIALER_SCRIPT

然后,检查一下你的登录会话程序 /etc/ppp/dialer.163,在我所在的 163 ISP,这个从模板拷贝过来的
会话程序可直接使用,如果有以下情况,你需要对这个会话程序略作改动: 

(1)如果拨号提示输入用户名的提示符不是 login: 
(2)如果登录成功后还要用户再交互打入命令 ,才启动 ppp 协议
本文后面的附录三是一个根据上述要求略加修改后的登录脚本。 
建议:假如你有多个 ISP 可连接,就创建多个拨号 PPP 连接脚本和相应的 PPP 登录脚本。 

四、启动 PPP 和断开 PPP
启动你的 PPP 连接唯一要做的就是开启 Modem,运行刚才写好的 ppp.163。你的 ppp.163 脚本将自动拨
号、送出登录信息、完成协议认证,等你看到你的 Modem 上的数据传输指示灯不再闪动并保持连接状态,
PPP 连接就完成了,你可以打命令: 

netstat -r 

查看当前的路由表,如果 PPP 协议连接成功的话,路由表里就会有连接你的 ISP 的缺省路由。你的 PPP
 已经配置成功了。 

如果没有成功,注意看一下 /etc/ppp 下的 PPP 出错记录文件。 

要断开 PPP 连接,运行 ppp-off。 

五、DNS 的配置
PPP 连接成功后,如果要使用 WWW 浏览器、Ftp、Telnet 等服务,必须正确配置 DNS 域名解析。所以,
一旦 PPP 连接完毕,应该在 /etc/resolv.conf 中加入一行: 

nameserver  

注意 nameserver 后面的是你的 ISP 的 DNS 服务器的 IP 地址,并且这一行说明必须放在其他的 
nameserver 说明之前,否则,你的 Linux 系统不会访问你的 ISP 的 DNS 服务器。然后,你就能使用各
项 Internet 服务了。 

六、自动配置 DNS 解析
本节内容并不是配置 PPP 必需的,但能对 PPP 的使用提供一些方便。 

由于你的 Linux 并不总是连在 Internet 上,所以在不连接 PPP 时,需要的 resolv.conf 是不同的。
所以,我们希望有一种办法能根据不同的情况,在 Linux 系统上自动设置合适的 resolv.conf 。 

在 /etc/ppp 下,有两个根据 PPP 连接状态自动运行的脚本,ip-up 和 ip-down,分别在 PPP 建立连接
和断开连接时运行,如果用户希望在 PPP 连接或断开时执行某些命令,可以在 /etc/ppp 目录下建立脚
本文件 ip-up.local 和 ip-up.local,在这两个文件中放入要执行的命令。当 PPP 建立连接后,ip-up 
会调用 ip-up.local,执行用户命令;当 PPP 断开连接后,ip-down 会调用 ip-down.local ,执行用户
命令。 

所以我们可以利用这个机制实现 DNS 解析的自动配置,方法如下: 

(1)在 /etc 下编辑分别适用于 PPP 连接状态和正常本地状态的 DNS 解析配置文件,我们假定这两个
文件是 resolv.ppp 和 resolv.normal。 

(2)在 /etc/ppp 下建立 ip-up.local,设置其执行权限,并在文件中加入: 

cp /etc/resolv.ppp /etc/resolv.conf 

(3)在 /etc/ppp 下建立 ip-down.local,设置其执行权限,并在文件中加入: 

cp /etc/resolv.normal /etc/resolv.conf 

这样设置后,每当 PPP 连接成功后,/etc/resolv.conf 的内容就是 resolv.ppp,而 PPP 断开后,
/etc/resolv.conf 被恢复成 resolv.normal 的内容。 

附录一:PPP 连接脚本模板 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
ACCOUNT=george # The account name for logon
PASSWORD=gracie # The password for this account
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 38400 \
asyncmap 20A0000 escape FF kdebug 0 $LOCAL_IP:$REMOTE_IP \
noipdefault netmask $NETMASK defaultroute connect $DIALER_SCRIPT

附录二:PPP 登录脚本模板 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 ATDT$TELEPHONE \
CONNECT '' \
ogin:--ogin: $ACCOUNT \
assword: $PASSWORD

附录三:一个稍作修改的 PPP 登录脚本
#!/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 ATDT$TELEPHONE \
CONNECT '' \
sername:--sername: $ACCOUNT \
assword: $PASSWORD \
nnex: ppp

说明:(1)用户登录提示符从由 login: 改为 username: 

(2)用户登录后,访问服务器出现提示符 annex: ,需要手工输入 ppp 命令 

附录四:可轮询拨号的 PPP 连接脚本模板 ppp-redialer

#!/bin/sh
###################################################################
#
# These parameters control the attack dialing sequence.
#
# Maximum number of attempts to reach the telephone number(s)
MAX_ATTEMPTS=10
# Delay between each of the attempts. This is a parameter to sleep
# so use "15s" for 15 seconds, "1m" for 1 minute, etc.
SLEEP_DELAY=15s
###################################################################
#
# This is a list of telephone numbers. Add new numbers if you wish
# and see the function 'callall' below for the dial process.
PHONE1=555-1212
PHONE2=411
###################################################################
#
# If you use the ppp-on script, then these are passed to this routine
# automatically. There is no need to define them here. If not, then
# you will need to set the values.
#
ACCOUNT=my_account_name
PASSWORD=my_password
###################################################################
#
# Function to initialize the modem and ensure that it is in command
# state. This may not be needed, but it doesn't hurt.
#
function initialize
{
chat -v TIMEOUT 3 '' AT 'OK-+++\c-OK'
return
}
###################################################################
#
# Script to dial a telephone
#
function callnumber
{
chat -v \
ABORT '\nBUSY\r' \
ABORT '\nNO ANSWER\r' \
ABORT '\nRINGING\r\n\r\nRINGING\r' \
'' ATDT$1 \
CONNECT '' \
ogin:--ogin: $ACCOUNT \
assword: $PASSWORD
#
# If the connection was successful then end the whole script with a
# success.
#
if [ "$?" = "0" ]; then
exit 0
fi
return
}
###################################################################
#
# Script to dial any telephone number
#
function callall
{
# echo "dialing attempt number: $1" >/dev/console
callnumber $PHONE1
# callnumber $PHONE2
}
###################################################################
#
# Initialize the modem to ensure that it is in the command state
#
initialize
if [ ! "$?" = "0" ]; then
exit 1
fi
#
# Dial telephone numbers until one answers
#
attempt=0
while : ; do
attempt=`expr $attempt + 1`
callall $attempt
if [ "$attempt" = "$MAX_ATTEMPTS" ]; then
exit 1
fi 
sleep "$SLEEP_DELAY"
done

附录五:PPP 断开脚本 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
系统分类: 自由话题
用户分类: UNIX
标签: LINUX
来源: 转贴
发表评论 阅读全文(44) | 回复(0)
总共 , 当前 /