📄 ch43.htm
字号:
speed 38400
# Reset the modem and terminal line.
reset
# Prepare for dialing.
send ATZ1\r
wait OK 4
if $errlvl != 0 goto error
dial 666-0999 ## Change to your server's number!
if $errlvl != 0 goto error
wait CONNECT 60
if $errlvl != 0 goto error
# We are connected. Log into the system.
login:
sleep 3
send \r\n\r\n
wait merlin> 20 ## Change to your server's prompt
if $errlvl != 0 goto error
send login\n
wait name: 10 ## Wait username: prompt
if $errlvl != 0 goto erro
send login_name\n ## Change to your own
wait ord: 10 ## Wait password prompt
if $errlvl != 0 goto error
send my_password\n ## Change to your own!
wait merlin> 10
if $errlvl != 0 goto error
send slip\n ## Change to suit your server
wait SLIP 30 ### Wait for SLIP prompt
if $errlvl != 0 goto error
get $local remote 10 ## Assumes the server sends your IP..
if $errlvl != 0 goto error ## address as soon as you enter slip.
get $remote merlin ## slip server address from /etc/hosts
done:
print CONNECTED to $remote with address $rmtip we are $local
default
mode SLIP
goto exit
error:
print SLIP to $host failed.
exit:
# End dip script
</FONT></PRE>
<P>Several different variations of the chat scripts are currently available, including
a few on most CD-ROM distributions. If you have access to the Internet, you can find
them on some FTP sites or posted on a Linux newsgroup. (See Appendix A, "FTP
Sites and Newsgroups.")
<H3 ALIGN="CENTER"><A NAME="Heading16<FONT COLOR="#000077">Setting Up PPP</FONT></H3>
<P>PPP is a more talented protocol than SLIP and is preferable for most uses. However,
the configuration is a little more complex than SLIP's. Linux divides the PPP functions
into two parts, one for the High-Level Data Link Control (HLDC) protocol which helps
define the rules for sending PPP datagrams between the two machines, and one for
the PPP daemon, called <TT>pppd</TT>, which handles the protocol once the HLDC system
has established communications parameters. In addition, Linux uses a program called
<TT>chat</TT>, which calls the remote system.</P>
<P>As with SLIP, PPP establishes a modem link between the two machines, and then
hands over the control of the line to PPP. You, as the user, never get to see a shell
prompt or login, because PPP handles it all for you. Both HLDC and <TT>pppd</TT>
are complex utilities with many options available to fine-tune their behavior. Because
you may never use most of these options, you can safely ignore all but the most important
aspects necessary to get a PPP connection up and running properly.</P>
<P>Prior to establishing a PPP link, you must have a loopback driver established.
You should also have a name resolution system in operation, even if it's the <TT>/etc/hosts</TT>
file or a simple DNS cache-only name server.
<H4 ALIGN="CENTER"><A NAME="Heading17<FONT COLOR="#000077">Setting Up a PPP
Account</FONT></H4>
<P>It is best to use PPP with a special user account called PPP for optimum protection
and behavior. This is not necessary, and you can easily use PPP from any user account,
but for more secure operation, you should consider creating a PPP user. The process
is simple. First, you need to add a new user to the <TT>/etc/passwd</TT> file. You
can use whatever script your system normally uses to add new users, or edit the <TT>/etc/passwd</TT>
file yourself. (See Chapter 41, "Users," for more information about users
and <TT>/etc/passwd</TT>.)</P>
<P>A sample <TT>/etc/passwd</TT> entry for the <TT>ppp</TT> account (with UID set
to <TT>201</TT> and GID set to <TT>51</TT>) looks like this:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">ppp:*:201:51:PPP account:/tmp:/etc/ppp/pppscript
</FONT></PRE>
<P>In this case, the account is set with no password (so no one can log into the
account) and a home directory of <TT>/tmp</TT> (because no files are created). The
startup program is set to <TT>/etc/ppp/pppscript</TT>, a file you create with the
configuration information in it (you can use any filename, of course). A sample contents
for the <TT>pppscript</TT> file looks like this:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">#!/bin/sh
mesg n
stty -echo
exec pppd -detach silent modem crtscts
</FONT></PRE>
<P>The first line forces execution of the script into the Bourne shell. The second
command turns off all attempts to write to the <TT>ppp</TT> account's <TT>tty</TT>.
The <TT>stty</TT> command is necessary to stop everything the remote sends being
echoed again. Finally, the <TT>exec</TT> command runs the <TT>pppd</TT> daemon (which
handles all PPP traffic). You will see the <TT>pppd</TT> daemon and the options later
in this section.
<H4 ALIGN="CENTER"><A NAME="Heading18<FONT COLOR="#000077">Dialing Out: chat</FONT></H4>
<P>PPP requires you to establish a modem connection to the remote machine before
it can take over and handle the communications. There are several utilities available
to do this, the most commonly used of which is <TT>chat</TT>. The <TT>chat</TT> program
is popular because it uses a scripting style similar to that used by UUCP. (See Chapter
44, "UUCP.")</P>
<P>To use <TT>chat</TT>, you have to assemble a command line that looks almost the
same as a UUCP <TT>/etc/Systems</TT> file entry. For example, to call a remote machine
with a Hayes-compatible modem (using the <TT>AT</TT> command set) at the number 555-1234,
you use the following command. It is all formatted as a <TT>chat</TT> script, UUCP
style:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">chat "" ATZ OK ATDT5551234 CONNECT "" ogin: ppp word: secret1
</FONT></PRE>
<P>If you read the UUCP chapter, this may look familiar. If not, all the entries
are in a "send-expect" format, with what you send to the remote specified
after what you receive from it. The <TT>chat</TT> script always starts with an <TT>expect</TT>
string, which you must set to be empty because the modem won't talk to you without
any signal to it. After the empty string, send the <TT>ATZ</TT> (reset) command,
wait for an <TT>OK</TT> back from the modem, and then send the <TT>dial</TT> command.
Once a <TT>CONNECT</TT> message is received back from the modem, the <TT>login</TT>
script for the remote machine is executed--you send a blank character, wait for the
<TT>ogin:</TT> (login) prompt, send the login name <TT>ppp</TT>, wait for the <TT>word:</TT>
(password) prompt, and then send your password. After the login is complete, <TT>chat</TT>
terminates but leaves the line open.</P>
<P>If you want to see all the activity that goes on with the <TT>chat</TT> program,
you can force all messages to the <TT>syslog</TT> daemon for future study. The <TT>-v</TT>
option forces the messages to <TT>syslog</TT>, so the command would be as follows:<FONT
COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">chat -v "" ATZ OK ATDT5551234 CONNECT "" ogin: ppp word: secret1
</FONT></PRE>
<P>If the other end of the connection doesn't answer with a <TT>login</TT> script
as soon as its modem answers, you may have to force a <TT>Break</TT> command down
the line to jog the remote end. This is done in the same way as with UUCP:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">chat -v "" ATZ OK ATDT5551234 CONNECT "" ogin:-BREAK-ogin: ppp word: secret1
</FONT></PRE>
<P>There's a security problem with this type of <TT>chat</TT> entry, because any
user doing a <TT>ps -ef</TT> command will see the entire command line (with its passwords).
If you are the only user of your system, this isn't a concern, but to save yourself
any problems, you can embed the script portion of the command in a file and read
the file into <TT>chat</TT>. Then, the script will not appear on a <TT>ps</TT> output.
To call a file for use with <TT>chat</TT>, use the <TT>-f</TT> option:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">chat -f chat_file
</FONT></PRE>
<P>The <TT>chat_file</TT> will contain the string:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">"" ATZ OK ATDT5551234 CONNECT "" ogin: ppp word: secret1
</FONT></PRE>
<P>The <TT>chat</TT> script can help you detect common error conditions, such as
a line busy or no connection established. The messages from your modem (Hayes uses
<TT>BUSY</TT> and <TT>NO</TT> <TT>CARRIER</TT> respectively) are embedded in the
<TT>chat</TT> script with the <TT>ABORT</TT> option, which enables you to exit from
the <TT>chat</TT> script gracefully if one of these error conditions occurs.</P>
<P>To handle these abort conditions, you embed the <TT>chat</TT> keyword <TT>ABORT</TT>
followed by the message that should trigger an abort, prior to your normal <TT>chat</TT>
script. For example, to modify the <TT>chat</TT> script above to abort on a <TT>BUSY</TT>
or <TT>NO</TT> <TT>CARRIER</TT> message from the modem, the script would look like
this:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">ABORT BUSY ABORT `NO CARRIER' "" ATZ OK ATDT5551234 CONNECT "" ogin: ppp word: secret1
</FONT></PRE>
<P>We needed two <TT>ABORT</TT> commands because each takes only one argument. The
rest of the <TT>chat</TT> script is as usual. Note the need to put quotation marks
around the <TT>NO CARRIER</TT> message because the space in the middle would confuse
the script otherwise.
<H4 ALIGN="CENTER"><A NAME="Heading19<FONT COLOR="#000077">Running pppd</FONT></H4>
<P>To set up a PPP connection, you need to invoke the <TT>pppd</TT> daemon. If you
have a PPP connection already established and your machine is logged into a remote
using the <TT>ppp</TT> account, you can start the <TT>pppd</TT> daemon. If you assume
your local machine is using the device <TT>/dev/cua1</TT> for its PPP connection
at 38,400 baud, you would start up the <TT>pppd</TT> daemon with this command:<FONT
COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">pppd /dev/cua1 38400 crtscts defaultroute
</FONT></PRE>
<P>This command tells the Linux kernel to switch the interface on <TT>/dev/cua1</TT>
to PPP, and establish an IP link to the remote machine. The <TT>crtscts</TT> option,
which is usually used on any PPP connection above 9600 baud, switches on hardware
handshaking. The IP address the local system will use is taken from the local hostname,
unless one is specified on the <TT>pppd</TT> command line (which you will seldom
need to do, as the local host IP address should be correct for the PPP line).</P>
<P>If you want to force the local or remote IP addresses to be something other than
the machine's default values, you can add the addresses with an option to <TT>pppd</TT>.
The general format is to specify the local IP address, a colon, and then the remote
IP address. For example, when the option<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">147.23.43.1:36.23.1.34
</FONT></PRE>
<P>is added to the <TT>pppd</TT> command line, it sets the local IP address as <TT>147.23.43.1</TT>
and the remote IP address to <TT>36.23.1.34</TT>, regardless of what the local values
are. If you only want to modify one IP address, leave the other portion blank. The
command<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">147.23.43.1:
</FONT></PRE>
<P>just sets the local IP address and accepts the remote IP address as whatever the
machine sends.</P>
<P>Because you need <TT>chat</TT> to establish the connection in the first place,
you can embed the <TT>chat</TT> command as part of the <TT>pppd</TT> command if you
want. This is best done when reading the contents of the <TT>chat</TT> script from
a file (using the <TT>-f</TT> option). For example, you could issue the following
<TT>pppd</TT> command:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">pppd connect "chat -f chat_file" /dev/cua1 38400 -detach crtscts modem defaultroute
</FONT></PRE>
<P>Notice a few modifications to the <TT>pppd</TT> command other than the addition
of the <TT>chat</TT> command in quotation marks. The <TT>connect</TT> command specifies
the dialup sc
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -