歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 怎樣用Linux撥號上網

怎樣用Linux撥號上網

日期:2017/2/25 10:36:37   编辑: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=<Your_Login_Name>
PASSWORD=<Your_Login_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-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
Copyright © Linux教程網 All Rights Reserved