📄 ch09_26.htm
字号:
><PRECLASS="screen"># <CODECLASS="userinput"><B>passwd libes</B></CODE>Changing password for libes on thunder.New password:Retype new password:</PRE></BLOCKQUOTE></P><PCLASS="para">This is fine for a single password.But suppose you have accounts of your own on a number of unrelatedcomputers and you would like them all to have the same password.Or suppose you are a system administrator establishing 1000 accounts atthe beginning of each semester.All of a sudden, an automated <EMCLASS="emphasis">passwd</EM> makes a lot of sense.Here is an Expect script to do just that:automate <EMCLASS="emphasis">passwd</EM> so that it can be called from a shell script.</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">spawn passwd [lindex $argv 0]set password [lindex $argv 1]expect "password:"send "$password\r"expect "password:"send "$password\r"expect eof</PRE></BLOCKQUOTE></P><PCLASS="para">The first line starts the <EMCLASS="emphasis">passwd</EM> program with the username passed asan argument.The next line saves the password in a variable for convenience.As in shell scripts, variables do not have to be declared in advance.</P><PCLASS="para">In the third line, the <EMCLASS="emphasis">expect</EM> command looks for the pattern<CODECLASS="literal">password:</CODE>.<EMCLASS="emphasis">expect</EM> waits until the pattern is found before continuing.</P><PCLASS="para">After receiving the prompt, the next line sends a password to thecurrent process.The <EMCLASS="emphasis">\r</EM> indicates a carriage-return.(Most of the usual C string conventions are supported.)There are two <EMCLASS="emphasis">expect</EM>-<EMCLASS="emphasis">send</EM> sequences because<EMCLASS="emphasis">passwd</EM> asks the password tobe typed twice as a spelling verification.There is no point to this in a non-interactive <EMCLASS="emphasis">passwd</EM>,but the script has to do it because <EMCLASS="emphasis">passwd</EM> assumes it isinteracting with a human who does not type consistently.</P><PCLASS="para">The final command <CODECLASS="literal">expect eof</CODE> causes the script to wait for theend-of-file in the output of <EMCLASS="emphasis">passwd</EM>.Similar to <EMCLASS="emphasis">timeout</EM>, <EMCLASS="emphasis">eof</EM> is another keyword pattern.This final <EMCLASS="emphasis">expect</EM> effectively waits for <EMCLASS="emphasis">passwd</EM> tocomplete execution before returning control to the script.</P><PCLASS="para">Take a step back for a moment.Consider that this problem could be solved in a different way.You could edit the source to <EMCLASS="emphasis">passwd</EM> (should you be so lucky as tohave it) and modify it so that given an optional flag, it reads itsarguments from the command line just the way that the Expectscript does.If you lack the source and have to write <EMCLASS="emphasis">passwd</EM> from scratch, ofcourse, then you will have to worry about how to encrypt passwords,lock and write the password database, etc.In fact, even if you only modify the existing code, you may find itsurprisingly complicated code to look at.The <EMCLASS="emphasis">passwd</EM> program does some very tricky things.If you do get it to work, pray that nothingchanges when your system is upgraded.If the vendor adds NIS, Kerberos, shadow passwords, a differentencryption function, or some other new feature, you will have torevisit the code.</P><PCLASS="para">Expect comes with many example scripts that demonstrate how you cando many things that are impossible with traditional shells.For example, the <EMCLASS="emphasis">passmass</EM> script lets you update your password onmany unrelated machines simultaneously.The <EMCLASS="emphasis">rftp</EM> script provides your regular <EMCLASS="emphasis">ftp</EM> client withadditional commands to do recursive FTP in either direction.The <EMCLASS="emphasis">cryptdir</EM> script encrypts all the files in a directory.And an amusing script is provided that lets two <EMCLASS="emphasis">chess</EM>processes play each other.Expect has no limit to the number of interactive programs it can driveat the same time.[The UNIX system may limit Expect, though, by controlling themaximum number of processes or other system resources available. -<EMCLASS="emphasis">JP</EM> ]<ACLASS="indexterm"NAME="AUTOID-11583"></A><ACLASS="indexterm"NAME="AUTOID-11584"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-9912-SECT-1.3">9.26.3 Testing: A Story </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-11588"></A>Many people use Expect for testing.You can test interactive programs as easily as you can automate them.And hardware lends itself to testing with Expect, too.For example, we solved a thorny problem when we had to deal with anunreliable bank of modems.We were receiving dozens of calls each week reporting "the modem is hung."No indication of which modem, of course.And it was always too late for us to ask the user to try somethingto investigate the problem.The connection was gone by then.Our solution was an Expect script that hourly connected to each modemand exercised it.Any problems were recorded so that we had a clear and full history ofeach modem's behavior.As soon as a defective or hung modem was encountered, theExpect script would send email to the system administrator.With this script in place, reports of modem problems from our usersdropped to zero.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-9912-SECT-1.4">9.26.4 Other Problems </A></H3><PCLASS="para">These are just a few of the problems that can be solved with Expect.And as with all Expect solutions, recompilation of the originalprograms is unnecessary.You don't even need the source code!Expect handles many other problems as well.For example, Expect can wrap existing interactive tools with GUI wrappers.This means you can wrap interactive programs with Motif-like frontendsto control applications by buttons, scrollbars, and other graphicelements.And Expect scripts work great as CGI scripts or from<SPANCLASS="link"><EMCLASS="emphasis">cron</EM> (<ACLASS="linkend"HREF="ch40_12.htm"TITLE="Periodic Program Execution: The cron Facility ">40.12</A>)</SPAN>or <EMCLASS="emphasis">inetd</EM>[the daemon that controls Internet services provided by asystem-<EMCLASS="emphasis">JP</EM> ].Finally, learning Expect may be easier than you think.Expect can watch you interact and then produce an Expect script for you.Interaction automation can't get much easier than this!</P><PCLASS="para">More information on Expect is available in <EMCLASS="emphasis">Exploring Expect</EM>, byDon Libes, from O'Reilly & Associates.</P></DIV><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">DL</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><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"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></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><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">9.25 The C Shell repeat Command </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">10. Aliases</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -