📄 ch09_26.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 9] 9.26 Expect </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:35:36Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch09_01.htm"TITLE="9. Saving Time on the Command Line"><LINKREL="prev"HREF="ch09_25.htm"TITLE="9.25 The C Shell repeat Command "><LINKREL="next"HREF="ch10_01.htm"TITLE="10. Aliases"></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch09_25.htm"TITLE="9.25 The C Shell repeat Command "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 9.25 The C Shell repeat Command "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 9<BR>Saving Time on the Command Line</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="CHAPTER"HREF="ch10_01.htm"TITLE="10. Aliases"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 10. Aliases"BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-9912">9.26 Expect </A></H2><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="expect">expect</A><BR></TH><TDVALIGN="TOP"><ACLASS="indexterm"NAME="AUTOID-11484"></A>Expect is a program to control interactive applications such as<SPANCLASS="link"><EMCLASS="emphasis">telnet</EM> (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>and <EMCLASS="emphasis">passwd</EM>.These and many other applications interactively prompt andexpect a user to enter keystrokes in response.But you can write simple Expect scripts to automate theseinteractions.Then the Expect program can run the "interactive" programnon-interactively.Expect can also be used to automate only parts of a dialogue, sincecontrol can be passed from the script to the keyboard and vice versa.This allows a script to do the drudgery and a user to do the fun stuff.</TD></TR></TABLE><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="tcl">tcl</A><BR><ACLASS="programreference"HREF="examples/index.htm"TITLE="tk">tk</A><BR></TH><TDVALIGN="TOP">Expect programs can be written in any language, but are almost alwayswritten using Tcl.Tcl is an interpreted language that is widely usedin many other applications.<ACLASS="indexterm"NAME="AUTOID-11492"></A>If you already use a Tcl-based application, you won't have to learn anew language for Expect.</TD></TR></TABLE><PCLASS="para">Tcl is a very typical-looking shell-like language.There are commands to set variables (<EMCLASS="emphasis">set</EM>), control flow(<EMCLASS="emphasis">if</EM>, <EMCLASS="emphasis">while</EM>, <EMCLASS="emphasis">foreach</EM>, etc.),and perform the usual math and string operations.Of course, UNIX programs can be called, too.</P><PCLASS="para">Expect is integrated on top of Tcl and provides additional commandsfor interacting with programs.Expect is named after the specific command that waits for output from aprogram.The <EMCLASS="emphasis">expect</EM> command is the heart of the Expect program.The <EMCLASS="emphasis">expect</EM> command describes a list of patterns to watch for.Each pattern is followed by an action.If the pattern is found, the action is executed.</P><PCLASS="para">For example, the following fragment is from a script thatinvolves a login.When executed, the script waits for the strings <CODECLASS="literal">welcome</CODE>,<CODECLASS="literal">failed</CODE>, or <CODECLASS="literal">busy</CODE>, and then it evaluates[(executes)-<EMCLASS="emphasis">JP</EM> ]one of the corresponding actions.The action associated with busy shows how multiple commandscan be evaluated.<ACLASS="indexterm"NAME="AUTOID-11507"></A>The timeout keyword is a special pattern thatmatches if no other patterns match in a certain amount of time.</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">expect { "welcome" break "failed" abort timeout abort "busy" { puts "I'll wait - the system is busy!" continue }}</PRE></BLOCKQUOTE></P><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-9912-SECT-1.1">9.26.1 Dialback </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-11514"></A>It is surprising how little scripting is necessary to producesomething useful.Below is a script that dials a phone.It is used to reverse the charges so that long-distance phone calls arecharged to the computer.It is invoked with the phone number as its argument.</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">spawn tip modemexpect "connected"send "ATD$argv\r"# modem takes a while to connectset timeout 60expect "CONNECT"</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-11519"></A>The first line runs the <EMCLASS="emphasis">tip</EM> program so that the output of a modem canbe read by <EMCLASS="emphasis">expect</EM> and its input written by <EMCLASS="emphasis">send</EM>.Once <EMCLASS="emphasis">tip</EM> says it is connected, the modem is told to dial using thecommand <CODECLASS="literal">ATD</CODE> followed by the phone number.The phone number is retrieved from <EMCLASS="emphasis">argv</EM>, which is a variable predefinedto contain the original argument with which the script was called.</P><PCLASS="para">The fourth line is just a comment noting that the variable being setin the next line controls how long <EMCLASS="emphasis">expect</EM> will wait before giving up.At this point, the script waits for the call to complete.No matter what happens, <EMCLASS="emphasis">expect</EM> terminates.If the call succeeds, the system detects that a user is connected andprompts with <CODECLASS="literal">login:</CODE>.</P><PCLASS="para">Actual scripts do more error checking, of course.For example, the script could retry if the call fails.But the point here is that it does not take much code to produce usefulscripts.This six-line script replaced a 60Kb executable (written in C) that didthe same thing!</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-9912-SECT-1.2">9.26.2 Automating /bin/passwd </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-9912-IX-AUTOMATIC-PASSWD-COMMAND-USE"></A><ACLASS="indexterm"NAME="UPT-ART-9912-IX-PASSWD-COMMAND"></A>Earlier I mentioned some programs that cannot be automated with theshell.It is difficult to imagine why you might even want to embedsome of these programs in shell scripts.Certainly the original authors of the programs did not conceive of thisneed.As an example, consider <EMCLASS="emphasis">passwd</EM>.</P><PCLASS="para"><EMCLASS="emphasis">passwd</EM> is the command to change a password.The <EMCLASS="emphasis">passwd</EM> program does not take the new password from the command line.Instead, it interactively prompts for it - twice.Here is what it looks like when run by a system administrator.(When run by users, the interaction is slightly more complex becausethey are prompted for their old passwords as well.)</P><PCLASS="para"><BLOCKQUOTECLASS="screen"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -