interactivelysendmail

sendmail  时间:2021-02-23  阅读:()
-14-January21,1992DonLibesNationalInstituteofStandardsandTechnologyphotoDonLibesreceivedaB.
A.
inMathematicsfromRutgersUniversityandanM.
S.
inComputerSci-encefromtheUniversityofRochester.
CurrentlyattheNationalInstituteofStandardsandTechnology,DonisengagedinresearchthatwillhelpU.
S.
industrymeasurethestandardhack.
Unfortunately,NISTdoesnothaveaverygoodsenseofhumor,sohewasforcedtowritehisrstbook"LifeWithUNIX"throughPrentice-Hall.
-13-January21,1992expectisdesignedtoworkwithprogramsastheyare.
Programsneednotbechangedorrede-signed,nomatterhowpoorlywritten.
Understandably,themajorityofsystemadministratorsarereluctanttomodifyaprogramthatworksandthattheyhavenotwrittenthemselves.
MostpreferwritingshellscriptsusingtheclassicUNIXtoolsphilosophy.
expecthandlestheseproblems,solvingthemdirectlyandwithelegance.
expectscriptsaresmallandsimpleforproblemsthataresmallandsimple.
Whilenotallexpectscriptsaresmall,thescriptsscalewell.
Theyarecomparableinstyletoshellscripts,beingtask-oriented,andprovidesynergywithshellscripts,bothbecausetheycancallshellscriptsandbecalledbythem.
Usedjudiciously,expectisawelcomenewtooltotheworkbenchofallUNIXsystemadministrators.
AcknowledgmentsThisworkwasjointlyfundedbytheNISTAutomatedManufacturingResearchFacility(AMRF,project734-3385)andScienticandTechnicalResearchServices(STRS,project734-3106).
ThecallbackscriptwaswrittenbyScottPaisley.
WalterRowewrotethesendmailversion-checkingscriptmentionedinthepaper.
ThankstoWalterRoweandKenManheimerfortheirhelpfulcommentsonthispaper.
SueMulroneywashelpfulincorrectingmybadlygrammar.
AvailabilitySincethedesignandimplementationofexpectwaspaidforbytheU.
S.
government,itisinthepublicdomain.
However,theauthorandNISTwouldlikecreditifthisprogram,documentationorportionsofthemareused.
expectmaybeftp'daspub/expect/expect.
shar.
Zfromftp.
cme.
nist.
gov.
expectwillbemailedtoyou,ifyousendthemailmessagesendpub/expect/expect.
shar.
Ztolibrary@cme.
nist.
gov.
References[1]DonLibes,"expect:CuringThoseUncontrollableFitsofInteraction",ProceedingsoftheSummer1990USENIXConference,Anaheim,CA,June10-15,1990.
[2]DonLibes,"TheexpectUserManual–programmaticdialoguewithinteractiveprograms",toappearasaNISTIR,NationalInstituteofStandardsandTechnology,November,1990.
[3]JohnOusterhout,"Tcl:AnEmbeddableCommandLanguage",ProceedingsoftheWinter1990USENIXConference,Washington,D.
C.
,January22-26,1990.
[4]JohnOusterhout,"tcl(3)–overviewoftoolcommandlanguagefacilities",unpublishedmanualpage,UniversityofCaliforniaatBerkeley,January1990.
[5]AT&T,UNIXProgrammer'sManual,Section8.
[6]LarryWall,"Perl–PracticalExtractionandReportLanguage",unpublishedmanualpage,March1990.
-12-January21,1992includesahigh-levellanguagethatisinterpretedandbearsastrongsimilaritytotheshellandalsotoC.
Inthatsense,Iseelittletoargueaboutsinceexpectcandoshell-likefunctions.
Inaprevi-ouspaper[1],Ihavesuggestedtheadditionofexpect'sfeaturestotheshell.
Noonewantstolearnyetanothershell,andthereisnoreasonwhythesecapabilitiescannotbeaddedtotheshell.
PerlAmoreinterestingcomparisoniswithPerl,alanguageclaimed(bytheauthor)[6]toembodythebestaspectsoftheshell,C,awk,sed,andanumberofotherUNIXtools.
HavingspentsometimeprogramminginPerl,thereisnoquestioninmymindthatPerliscapableofsolvingthesametasksthatIhavedescribedinthispaper.
Pseudo-ttypackagesforPerlhavebeenwrittenandsend/expectutilitiescouldbewrittenalso.
Perlisaverypowerfullanguage.
Itismuchricherthanthelanguageusedbyexpect(oranyshellforthatmatter).
Thishasadvantagesanddisadvantages.
ThemostobviousdisadvantageisthatPerl'soverabundanceofoptionsandfeaturessimplyaren'tnecessaryforthetasksthatexpectaddresses.
Perl'scomplexityisreectedinitsdiskspace.
Thecomputeronmydesk,aSun3,requires270KtostorePerlandhasasignicantstartuptime.
expect,ontheotherhand,is70Kwithessentiallynostartuptime.
ThereareotherreasonsthatPerlisnotwidelyappliedtocertainproblems,butcompletingthediscussiondeservesapaperofitsown.
InsteadIwillsummarizebysayingthatexpectisappropriatetoonlyafractionofthesystemadministrationproblemsthatPerlsolves.
Thisisintentionallyso.
expectwaswrittentosolveaveryspecicproblem,anditdoesthatconciselyandefciently.
IthinkthatittswellwiththeUNIXphilosophyofsmalltools,unlikePerlwhichdemandsasignicantinvestmentinmasteringitscomplexity.
Giventhechoice,Ipredictthatmostsystemadministratorswouldchooseatoollikeexpectthattakesverylittleefforttolearn,ratherthanenteringtheworldofPerl.
EmacsEmacsisanalogoustoPerlinmanyways,includingitsexibilityandoverabundanceoffunction-ality.
Similarly,Emacscanbeusedtosolvethesesameproblems.
Andformuchthesamerea-sonsasIgaveabove,EmacsisinappropriatefortheclassofproblemsIhavesuggestedinthispaper.
Indeed,consideringthatEmacshasbeenavailableforoveradecade,andI'veneverheardofanyoneusingitthisway,I'llprofferthatEmacsissoinappropriatefortheseproblems,thatitisnotsurprisingthisusagehasneverevenoccurredtoanyone.
ConclusionUNIXshellsareincapableofcontrollinginteractiveprocesses.
Thishasbeenattherootofmanydifcultiesautomatingsystemadministrationtasks.
WhiletheUNIXcommunityisgraduallyprovidingbetterdesignedtoolsanduserinterfaces,evenmoreprogramsarebeingwrittenwithembarrassinglypooruserinterfacesatthesametime.
Thisisunderstandablebecausesystemadministratorsgivemoreprioritytosolvingaproblemsotheycangotothenextone,thangoingbacktoprettyupanoldandworkingsolution.
-11-January21,1992systems.
HowmuchmoreworkcoulditpossiblybeforyoutoadministerjustonemoresystemOh,anditrunsVMS.
")SecuritySeveraloftheexamplespresentedhavepromptedforpasswordsthataredifferentthantheusualUNIXstyle.
Normally,UNIXpromptsforpasswordsdirectlyfrom/dev/tty.
Thishastheunfor-tunatedrawbackthatyoucannotredirectstdin.
Wehaveshownhowtogetaroundthatbyusingexpect.
Ofcourse,doingthisreopensapossiblesecurityhole.
Unprivilegeduserscandetectpasswordspassedasargumentsbyusingps.
Ifpasswordsarestoredinles,lapsesinsecuritycanmakeplaintextpasswordsevidenttopeoplebrowsingthroughyourles.
Publicly-readablebackupmediaareoneofthesimplestsuchsecuritylapses.
Ifyouareatallinterestedinsecurity,Idonotrecommendstoringplaintextpasswordsinles.
Thelikelihoodofsuchapasswordbeingdiscoveredandabusedisjusttoohigh.
Ourusersstorepasswordsinles,butonlyforhighlyrestrictedaccounts,suchasfordemosoranonymousftp.
Thechancesofleakingapasswordthroughpsarelower,andcanbeloweredfurtherstillbyusingthesmallestpossiblescriptaroundthepasswordpromptingprogram.
Suchawindowisextremelysmall.
Nonetheless,securesitesshouldnottakeeventhischance.
Analternativeistohaveexpectinteractivelypromptforpasswords.
Ifyouhaveanexpectscriptthatisdoingacomplicatedseriesoftelnets,ftpsandotherthings,thescriptscanencodeevery-thingbutthepasswords.
Uponrunningsuchascript,theuserwillbeonlybepromptedonceforapassword,andnothingelse.
Thenexpectwillusethatpasswordwhenevernecessary,andcom-pletealltheotherdialoguefromdatapre-storedinles.
Insummary,expectneednotweakensecurity.
Usedwisely,expectcanevenenhancesecurity.
However,youmustusecommonsensewhenwritingscripts.
ComparisontoothersystemadministrationtoolsThissectionofthepapercanbeconsideredcontroversyorheresy,asyouwish.
Itissomewhatreligiousinthattheargumentscanonlyberesolvedbyphilosophicalchoiceratherthanlogic.
IhavekeptitdowntoaveryfewreasonstogiveyouonlythebarestfeelingsforwhatIconsiderisimportanttounderstandwhenchoosingexpectoverothersystemadministrationtools.
Asshouldbeobvious,Ithinkthereareveryfewalternativestousingexpect.
Traditionally,thepopularchoiceshavebeen1)avoidanceand2)Cprogramming.
Thesearenownolongertheonlychoices.
ShellTheshellisincapableofcontrollinginteractiveprocessesinthewaythatexpectcan.
Nonethe-less,certaincomparisonsbetweenexpectandtheshellareinevitable.
Inparticular,expect-10-January21,1992youcantransferahierarchynomatterwhatitlookslikeorhowdeepitis.
expectsupportsrecur-siveprocedures,makingthistaskashortscript.
Mysiteregularlyretrieveslargedistributions(e.
g.
,Gnu,X)thisway.
Assistingadbandother"dumb"programsQuiteoften,vendorsprovideinstructionsformodifyingsystemsintheformofadbinstructions,wheresomeinstructionsmaydependontheresultsofearlierones(i.
e.
,"eachtime_maxusersisincremented,youmustadd16to_nle").
adbhasnospecialscriptinglanguagethatsupportssuchinteraction,nordoestheshellprovidethiscapability.
expectcanperformthisinteraction,playingthepartoftheuser,bydirectlylookingattheresultsofoperations,justasauserwould.
Thistechniquecanbeappliedtoanyprogram.
Infact,expectcanactasanintermediarybetweentheuserandprogramswithpoorly-writtenuserinterfaces.
expectnormallyshowstheentiredia-loguebutcanbetoldnotto.
Thenexpectcanprompttheuserforcommandssuchasshow_maxusersinsteadofadb'snativebutcryptic_maxusers/d.
Translationscanalsobeper-formedinthereversedirection.
Ashortexpectscriptcouldlimitthedifcultyofsystemadminis-tratorswhohavenointerestinmasteringadb.
Inaddition,theabilityofsystemadministratorstoaccidentallycrashthesystembyafewerrantkeystrokeswouldbedramaticallylessened.
GreppingmonsterloglesAcommoncommandsequenceinvolveslookingatalogwith,say,grep,andtheninterruptingit(with^C)afterthelineofinterestappears.
Unfortunately,grepandotherprogramsarelimitedtotheamountofprogrammabilitytheyhave.
Forexample,grepcannotbedirectedtostopsearch-ingaftertherstmatch.
Ashortexpectscriptcansendaninterrupttogrepafterseeingtherstlinejustasiftheuserwereactuallyatthekeyboard.
Withprogramsthatgenerateloglesaslargeasagigabyte,thisisarealproblem.
Withoutexpect,theonlysolutionsaretoletgrepcontinuerunningoverthewholele,ortodedicateahumantothetaskofpressing^Cattherighttime.
expectcancutofftheprocessassoonaspos-sible,mailingtheresultsbackthesystemadministratorifnecessary.
Ingeneral,expectisusefulforsendingoddcharacterstoaprocessthatcannotbeembeddedinashellscript.
expectcanalsoexecutejobcontrolcommands(bg,fg,etc.
)inordertomediatebetweenprocessesthatwereneverdesignedtocommunicatewitheachother.
Again,thiscanrelieveahumanfromthetedioustaskofinteractivelymonitoringprograms.
Administeringnon-UNIXsystemsexpectisaUNIXprogram,yetitcanbeusedtoadministernon-UNIXsystems.
Howisthispos-sibleRunningtelnet(tip,kermit,etc.
)toanon-UNIXhost,itcanloginandperformsend/ex-pectsequencesontheremotecomputer.
Theoperatingsystemorenvironmentoftheremotecomputeriscompletelyirrelevanttoexpect,sinceallofthisisisolatedtotheexpectscriptitself.
ThisisveryusefulforsystemadministratorsthatalreadyhaveaUNIXcomputerontheirdeskbutareforcedbymanagementtoadministeranothercomputer.
("Youalreadyadminister20UNIX-9-January21,1992su,passwd,cryptandotherpassword–eatersProgramsthatreadandwrite/dev/ttycannotbeusedfromshellscriptswithouttheshellscriptaccessing/dev/tty.
Anearlierexampleshowedhowtoforcepasswdnottoreadfrom/dev/tty.
Withthistechnique,youcanchangeitsinputsourcetostdin,aparameter,orevenanenvironmentvariable.
Asanotherexample,supposeyouhavetypedacommandthatfailsbecauseyouweren'troot.
Thetypicalreactionistotypesuandthenreenterthecommand.
Unfortunately,historywon'tworkinthissituationas!
-2willjustevoketheerror-1:Eventnotfound.
Theproblemisthatyouwanttorefertoacommandthatisnowinadifferentshellinstantiation,andthereisnowaytogetbacktoit.
Asolutionistopassthefailedcommandasanargument(via!
!
)toanexpectscriptthatwillpromptyoufortherootpassword,invokesu,andthenfeedtheoriginalfailedcommandtotheresultingsuperusershell.
Iftheexpectscriptexecutesinteractasitslastaction,youwillhavetheoriginalcommandexecutedforyou(noretyping),plusyouwillgetanewsuperusershell.
Thereisnowaytodothiswithsuexceptbyresortingtotemporarylesforyourhistoryandalotofretyping.
Amorepainfulexampleisnewgrp.
Unlikesu,newgrpdoesnotallowadditionalargumentsonthecommandlinetobepassedtothenewshell.
Youmustinteractivelyenterthemafternewgrpbeginsexecuting.
Ineithercase,bothsuandnewgrpareessentiallyuselessinshellscripts.
Security–Thegoodnewsis…Earlier,Imentionedhowtobuildascriptthatwouldforceuserstochoosegoodpasswordswith-outrewritingpasswd.
Allotheralternativeseitherrewritethepasswdprogramorasktheusertoberesponsibleforchoosingagoodpassword.
Ontheoppositesideofthecoin,expectcanbeusedtotestothersitesforsecurelogins(ortobreakin,Isuppose).
Tryingtologinasrootusing,say,allthewordsinanon-linedictionary,atallthelocalhostsatasitewouldbeprohibitivelyexpensiveforahumantodo.
expectwouldworkatitrelentlessly,eventuallyndinganinsecureroot,orshowingthesitetobeprotectedbygoodpasswords.
QuestionsatboottimeWhilebooting,itisusefultovalidateimportantsystemfacts(e.
g.
,date)beforecomingupalltheway.
Ofcourse,ifnooneisstandinginfrontoftheconsole(e.
g.
,thesystembootedduetoapowerfailure)thecomputershouldcomeupanyway.
Writingsuchascriptusingtheshellispain-ful,primarilybecausearead-with-timeoutisnotdirectlyimplementedintheshell.
Inexpect,allreadstimeout.
expectcanpromptandreadfromthekeyboardjustaseasilyasfromaprocess.
TransferringhierarchieswithftpAnonymousftpisverypainfulwhenitcomestodirectoryhierarchies.
Sincethereisnorecursivecopycommand,youmustexplicitlydocdsandgets.
Youcanautomatethisinashellscript,butonlyifthehierarchyisknowninadvance.
expectcanexecuteanlsandlookattheresultssothat-8-January21,1992Inthissection,moreexampleswillbediscussed.
Becauseofspacelimitations,scriptswillnotbeshown,butallofthemhavebeenwrittenandarebeingused.
RegressiontestingTestingnewreleasesofinteractivesoftware(tip,telnet,etc.
)requiresahumantopresskeysandwatchforcorrectresponses.
Doingthismorethanafewtimesbecomesquitetiresome.
Natu-rally,peoplearemuchlesslikelytorunthoroughregressiontestsaftermakingsmallchangesthattheythinkprobablydon'taffectotherpartsofaprogram.
Regressiontestingcanalsobeusefulforyourentireinstallation.
Youcanmakeascriptthattestsallyoursite'slocalapplications,andrunitataftereachsystemupgradeorcongurationchange.
AutomatingloginsManyprogramshaveafrequentlyrepeated,well-denedsetofcommandsandanothersetthatarenotwell-dened.
Forexample,atypicaltelnetsessionalwaysbeginswithalogin,afterwhichtheusercandoanything.
Toautomatethis,expecthastheabilitytopasscontrolfromthescripttotheuser.
Atanytime,theusercanreturncontroltothescripttemporarilytoexecutesequencesofcommonlyrepeatedcommands.
Atmysite,expectisheavilyusedtoautomatetheprocessoflogginginthroughmultiplefront-endsandcommunicationswitches.
Infact,theoriginalreasonexpectwaswrittenwastocreatesixwindows,eachofwhichautomaticallyloggedintoanotherhosttorunademo.
Thegeneralideaofautomatingtelnet,ftp,andtipisveryusefulwhendealingwithhoststhatdonotsupportrloginandrcp.
ButthetechniqueisalsousefulwithnativeUNIXcommandslikesu,login,orrlogin.
expectscriptscancallanyofthem,sendingpasswordsasappropriateandthencontinuingactionsasdesired.
Whileanyofthesecommandscanbeembeddedinashellscript,theshellhasnowayoftakingcontroloverwhathappensinsideoftheseprograms.
Subsequentcommandsfromtheshellscriptdonotgetsenttothenewcontext,butareheldupuntiltheprevi-ouscommandhascompletedsothattheycanbesenttotheoriginalcontext.
expecthasnosuchproblemsswitchingcontextstocontinuecontrollinganyofthesesessions.
telnet–It'snotjustforbreakfastanymoretelnetalsofunctionsasaninterfacetotheexcitingworldofTCPsockets.
telnetcanbeusedtoaccessnon-telnetsocketsandqueryotherhostsfortheirdate(port13),time(port37),listofactiveusers(port11),userinformation(port43),networkstatus(port15),andallsortsofothergoodiesthatyoumightonlybeabletogetifyouhadpermissiontologin.
Forexample,oursiteregularlyrunsascriptthatchecks(port25)whatversionofsendmail.
cfeachofourlocalhostsisactuallyusing.
Ifwedidthisbyreadingles,wewouldneedpermissiontologin,orremotelymountlesystemsandreaddirectoriesandlesonseveralhundredhosts.
Usingtelnetismucheasier,albeitalittlestrange.
-7-January21,1992isimportantisthatexpectscriptsaresmallandsimpleforproblemsthataresmallandsimple.
expectobviatestheneedforresortingtoCjustbecauseoflimitationsonthepartoftheshell.
Example–IntelligentftpOneofoursiteadministratorswantedtospoollesinadirectory.
Later,asecondcomputerwoulduseftptopickthemupandthendeletethemfromtherstcomputer.
Hisrstattemptwastousemget*followedbymdelete*.
Unfortunately,thisdeleteslesthatarriveinthewin-dowbetweenwhenthemgetstartsandthemdeletestarts.
ThescriptfragmentinListing5solvedtheproblem.
Thescriptbeginsbyspawningftp.
Ihaveomittedseverallinesthatopenaconnectionfollowedbysendingandconrmingtheuserandpasswordinformation.
Thenextlinesendsanftpcom-mandtostorethelistofremotelesinalocallecalledlsFile.
Thiscommandisterminatedbyasemicolon,allowingtheresponsetobeveriedwithanexpectcommandonthesamelineofthescript.
exec–ExecuteaUNIXcommand.
execexecutesaUNIXcommandandsimplywaitsforittocomplete,justasifitwereinashellscript.
Inlinefour,catreturnsthelistofles,andtheirnamesarestoredinthevariable,lsVar.
execisusedagaininthenextline,thistimetodeletethelocalle,lsFile.
TheremainderofthescriptmerelyiteratesthroughthevariablelsVar,sendinggetcommandsfol-lowedbydeletecommandsforeachlefoundintheearlierls.
Otherexamplessolvedexpectaddressesasurprisinglylargeclassofsystemadministrationproblemswhichbeforenowhaveeitherbeensolvedbyavoidanceorspecialkludges.
Atthesametime,expectdoesnotattempttosubsumefunctionsalreadyhandledbyotherutilities.
Forexample,thereisnobuilt-inletransfercapability,becauseexpectcanjustcallaprogramtodothat.
Andwhiletheshellisprogrammable,itcannotinteractwithotherinteractiveprocessesanditcannotsolveanyoftheexamplesinthispaper.
Listing5Fragmentofftpspoolscript.
spawnftp.
.
.
sendls*lsFile\r;expect*success*ftp>*setlsVar[execcatlsFile]execrmlsFile\rsetlen[length$lsVar]for{seti0}{$i*senddelete$file\r;expect*success*ftp>*}-6-January21,1992Ifthescriptdoesnotmatchoneoftheprespeciedanswers,thelastcasematches.
(Theisnecessarytopreventthescriptfromtriggeringbeforetheentirequestionarrives.
)Theinteractactionpassescontrolfromthescripttothekeyboard(actuallystdin)sothatahumancananswerthequestion.
interact–Passcontrolfromscripttouserandback.
Duringinteract,theusertakescontrolfordirectinteractions.
Controlisreturnedtothescriptafterpressingtheoptionalescapecharacter.
Inthisscript,+ischosenastheescapecharacterbypassingitastheargumenttointeract.
Arealexpectscriptforfsckwoulddoseveralotherthings.
Forexample,fsckusesseveralstati-cally-sizedtables.
Forthisreason,fsckislimitedtothenumberoferrorsofonetypethatcanbexedinasinglepass.
Thismayrequirefsckberunseveraltimes.
Whilethemanualsaysthis,fsckdoesn't,andfewsystemadministratorsknowfsckthatintimately.
Whenrunfromashellscript,thislackofprogrammabilitywillcausethesystemtocomeupallthewaywithacorruptlesystem(ifthereturncodeisn'tchecked)orbeunnecessarilyrebootedseveraltimes(ifthereturncodeischecked).
Example–CallbackThescriptinListing4waswrittenbyauserwhowantedtodialupthecomputer,andtellittocallhimback.
Sincehelivedoutofthelocalcallingarea,thiswouldgetthecomputertopickuphislong-distancephonebillsforhim.
Therstlinespawnstipwhichopensaconnectiontoamodem.
Next,expectwaitsfortiptosayitisconnectedtothemodem.
Theuser'sphonenumber,passedastherstargumenttothescript,isthenfetchedandaddedtoacommandtodialaHayes-compatiblemodem.
Acarriage-returnisappendedtomakeitappearasifauserhadtypedthestring,andthemodembeginsdialing.
Thethirdlineassigns60tothevariabletimeout.
expectactuallylooksatthisvariableinordertotellithowmanysecondstowaitbeforegivingup.
Eventuallythephoneringsandthemodemanswers.
expectndswhatit'slookingforandexits.
Atthispointgettywakesup,andndingthatithasadialuplinewithDTRonit,startsloginwhichpromptstheusertologin.
Sincethescriptwasoriginallywritten,wehaveaddedafewmorelinestoautomateandverifyphonenumbersbasedontheuidrunningitpartlyforsecurity,butthefragmentshownherewasusedsuccessfullyandformstheheartofourcurrentscript.
Ironically,werecentlynoticeda60KbequivalenttocallbackonUsenetthathadnomorefunctionalitythanadozenorsolinesofexpect.
Ofcourse,notallscriptsarethisshort.
I'mlimitedtowhatcanbepresentedhere,andtheseexamplesreallyservejusttogiveyouafeelofwhatexpectdoesandhowitcanbeapplied.
WhatListing4Callbackscript.
Firstargumentisphonenumber.
spawntipmodemexpect{*connected*}sendATDT[index$argv1]\rsettimeout60expect{*CONNECT*}-5-January21,1992"Assumeayesresponsetoallquestionsaskedbyfsck;thisshouldbeusedwithextremecaution,asitisafreelicensetocontinue,evenaftersevereproblemsareencountered.
"The-noptionhasasimilarlyworthlessmeaning.
Thiskindofinterfaceisinexcusablybad,andyetmanyprogramshavethesamestyle.
Forexample,ftphasanoptionthatdisablesinteractivepromptingsothatitcanberunfromascript,butitprovidesnowaytotakealternativeactionshouldanerroroccur.
Usingexpect,youcanwriteascriptthatallowsfscktoberun,havingquestionsansweredauto-matically.
Listing2isascriptthatcanrunfsckunattendedwhileprovidingthesameexibilityasbeingruninteractively.
Thescriptbeginsbyspawningfsck.
for–Controlsiteration(looping).
Thelanguageusedbyexpectsupportscommonhigh-levelcontrolstructuressuchasif/then/else.
Inthesecondline,aforloopisusedwhichisstructuredsimilarlytotheC-languageversion.
Thebodyoftheforcontainsoneexpectcommand.
Thefollowingexpectcommanddemonstratestheabilitytolookformultiplepatternssimulta-neously.
(Thebackslashes(\)areusedtoquotecharacters–inthiscasewhitespace.
)Inaddition,eachpatterncanhaveanaccompanyingactiontoexecuteifthepatternisfound.
Thisallowsustoprespecifyanswersforspecicquestions.
WhenthequestionsUNREFFILE…CLEARorBADINODENUMBER…FIXappear,thescriptwillautomaticallyanswery.
Ifanythingelseappears,thescriptwillanswern.
Ingeneral,ifallquestionsareknownandanswerableinadvance,ascriptcanberunintheback-ground.
Withmorecomplexprogramsitmaybedesirabletotrapunexpectedquestionsandforceausertointeractivelyevaluatethem.
Listing3isascriptdoesexactlythis.
Listing2Non-interactivefsckscript.
spawnfsckfor{}1{}{expecteofbreak\{*UNREF\FILE*CLEAR\}{sendy\r}\{*BAD\INODE*FIX\}{sendy\r}\{*\}{sendn\r}}Listing3User-friendlyfsckscript.
spawnfsckfor{}1{}{expecteofbreak\{*UNREF\FILE*CLEAR\}{sendn\r}\{*BLK(S)\MISSING*SALVAGE\}{sendy\r}\{*\}{interact+}}-4-January21,1992spawn–Runsaninteractiveprogram.
Thespawnedprogramisreferredtoasthecurrentprocess.
Inthisexample,passwdisspawnedandbecomesthecurrentprocess.
Ausernameispassedasanargumenttopasswd.
expect–Looksforapatternintheoutputofthecurrentprocess.
Theargumentdenesthepattern.
Additionaloptionalargumentsprovidealternativepatternsandactionstoexecutewhenapatternisseen.
(Anexamplewillbeshownlater.
)Inthisexample,expectlooksforthepatternpassword.
Theasteriskallowsittomatchotherdataintheinput,andisausefulshortcuttoavoidspecifyingeverythingindetail.
Thereisnoactionspecied,sothecommandjustwaitsuntilthepatternisfoundbeforecontinuing.
send–Sendsitsargumentstothecurrentprocess.
Thepasswordissenttothecurrentprocess.
The\rindicatesacarriage-return.
(Allthe"usual"Cconventionsaresupported.
)Therearetwosend/expectsequencesbecausepasswdasksthepasswordtobetypedtwiceasaspellingverication.
Thereisnopointtothisinanon-interactivepasswd,butthescripthastodothisbecausepasswddoesn'tknowbetter.
Thenalexpecteofsearchesforanend-of-leintheoutputofpasswdanddemonstratestheuseofkeywordpatterns.
Anotheroneistimeout,usedtodenotethefailureofanypatterntomatch.
Here,eofisnecessaryonlybecausepasswdiscarefullywrittentocheckthatallofitsI/Osucceeds,includingthenalnewlineproducedafterthepasswordhasbeenenteredasecondtime.
Itiseasytoaddacallandtestofgrep$password/usr/dict/wordstothescripttocheckthatapassworddoesn'tappearintheon-linedictionary,however,wewillleavetheillustra-tionofcontrolstructurestothenextexample.
Example–fsckManyprogramsareostensiblynon-interactive.
Thisis,theycanruninthebackgroundbutwithaveryreducedfunctionality.
Forexample,fsckcanberunfromashellscriptonlywiththe-yor-noptions.
Themanual[5]denesthe-yoptionasfollows:Listing1Non-interactivepasswdscript.
Firstargumentisusername.
Secondargumentisnewpassword.
setpassword[index$argv2]spawnpasswd[index$argv1]expect{*password:}send$password\rexpect{*password:}send$password\rexpecteof-3-January21,1992expectisageneral-purposesystemforsolvingtheinteractiveprogramproblem,howeveritsolvesanunusuallylargenumberofproblemsinthesystemadministrationarena.
WhiletheUNIXstyleistobuildsmallprogramsthatcanbeusedasbuildingblocksintheconstructionofotherpro-gramsusingshellsandpipelines,fewsystemadministrationprogramsbehavethisway.
Traditionally,littletimewasspentdesigninggooduserinterfacesforsystemadministratortools.
Thereasonsmaybeanyorallofthefollowing:Systemadministratorswereexperiencedprogrammers,andthereforedidn'tneedallthehand-holdingthatgeneraluserprogramsrequire.
Programssuchasfsckandcrashwereruninfrequently,sotherewaslittlepointspendingmuchtimeonsuchrarelyusedtools.
Systemadministrationtoolswereusedinextremeconditions,considerednotworthprogrammingforbecauseoftheirdifcultyorrarity.
Itwasmorecost-effectivetosolvetheproblembyhandinreal-time.
Systemadministratorssolvedproblemsinsite-dependentways,neverexpect-ingtheirunderdesignedprogramstobepropagatedwidely.
Whateverthereason,theresultisthattheUNIXsystemadministrator'stoolboxislledwithrep-resentativesofsomeoftheworstuserinterfaceseverseen.
Whileonlyacompleteredesignwillhelpalloftheseproblems,expectcanbeusedtoaddressagreatmanyofthem.
Example–passwdTheexpectscriptinListing1takesapasswordasanargument,andcanberunnon-interactivelysuchasbyashellscript.
Ashellscriptcouldpromptandrejecteasilyguessedpasswords.
Alter-natively,theshellscriptcouldcallapasswordgenerator.
Suchacombinationcouldcreatelargenumbersofaccountsatatimewithoutthesystemadministratorhavingtohand-enterpasswordsasiscurrentlydone.
Admittedly,thescriptreopenstheoriginalsecurityproblemthatpasswdwasdesignedtosolve.
Thiscanbeclosedinanumberofways.
Forexample,expectcouldgeneratethepasswordsitselfbydirectlycallingthepasswordgeneratorfromwithinthescript.
ThescriptinglanguageofexpectisdenedcompletelybyLibes[1][2]andOusterhout[3][4].
Inthispaper,commandswillbedescribedastheyareencountered.
Ratherthangivingcomprehen-siveexplanationsofeachcommand,onlyenoughtounderstandtheexampleswillbesupplied.
set–Setstherstargumenttothesecond(i.
e.
,assignment).
Inline1ofthescript,therstargumenttosetispassword.
Thesecondisanexpressionthatisevaluatedtoreturnthesecondargumentofthescriptbyusingtheindexcommand.
Therstargu-mentofindexisalist,fromwhichitretrievestheelementcorrespondingtothepositionofthesecondargument.
argvreferstotheargumentsofthescript,inthesamestyleastheClanguageargv.
-2-January21,1992IntroductionUNIXsystemadministrationofteninvolvesusingprogramsdesignedforinteractiveuse.
Manysuchprograms(passwd,su,etc.
)cannotbeplacedintoshellscripts.
Someprograms(fsck,dump,etc.
)arenotspecicallyinteractive,buthavelittlesupportforautomateduse.
Forexamplethepasswdcommandpromptstheuserforapassword.
Thereisnowaytosupplythepasswordonthecommandline.
Ifyouusepasswdfromashellscript,itwillblockthescriptfromrunningwhileitpromptstheuserwhoinvokedtheshellscript.
Becauseofthis,youcannot,forexample,rejectpasswordsthatarefoundinthesystemdictionary,acommonsecuritymeasure.
Itisironicthatsecuritywasthereasonthatpasswdwasdesignedtoreaddirectlyfromthekeyboardtobeginwith.
passwdisnotaloneinthisrecalcitrantbehavior.
Manyotherprogramsdonotworkwellinsideofshellscriptsandquiteafewofthesearecrucialtoolstothesystemadministrator.
Examplesarerlogin,telnet,crypt,su,dump,adb,andfsck.
Moreproblemswillbementionedlater.
Theproblemwithalloftheseprogramsisnottheprogramsthemselves,buttheshell.
Forexam-ple,theshellcannotseepromptsfrominteractiveprogramsnorcanitseeerrormessages.
Theshellcannotdealwithinteractiveprogramsthiswaybecauseitisincapableofcreatingatwo-wayconnectiontoaprocess.
ThisisaninherentlimitationofclassicUNIXshellssuchassh,cshandksh(fromhereongenericallyreferredtoassimplytheshell).
expect–AnOverviewexpectisaprogramthatsolvesthegeneralproblemofautomatinginteractiveprograms.
expectcommunicateswithprocessesbyinterposingitselfbetweenprocesses(seeFigure1).
Pseudo-ttysareusedsothatprocessesbelievetheyaretalkingtoarealuser.
Ahigh-levelscriptenableshan-dlingofvariedbehavior.
Thescriptoffersjobcontrolsothatmultipleprogramscanbecontrolledsimultaneouslyandaffectoneanother.
Also,arealusermaytakeandreturncontrolfromandtothescriptwhenevernecessary.
Figure1.
expectiscommunicatingwith5processessimultaneously.
Thescriptisincontrolandhasdisabledloggingtotheuser.
Theuseronlyseeswhatthescriptsaystosendandisessentiallytreatedasjustanotherprocess.
expectinteractiveprocessesscriptJanuary21,1992UsingexpecttoAutomateSystemAdministrationTasksDonLibesNationalInstituteofStandardsandTechnologyMetrologyBldg,RoomA-127Gaithersburg,MD20899libes@cme.
nist.
govABSTRACTUNIXsystemadministrationofteninvolvesprogramsdesignedonlyforinteractiveuse.
Manysuchprograms(passwd,su,etc.
)cannotbeplacedintoshellscripts.
Someprograms(fsck,dump,etc.
)arenotspecificallyinteractive,buthavepoorsupportforautomateduse.
expectisaprogramwhichcan"talk"tointeractiveprograms.
Ascriptisusedtoguidethedialogue.
Scriptsarewritteninahigh-levellanguageandprovideflexi-bilityforarbitrarilycomplexdialogues.
Bywritinganexpectscript,onecanruninteractiveprogramsnon-interactively.
Shellscriptsareincapableofmanagingthesesystemadministrationtasks,butexpectscriptscancontrolthemandmanyothers.
Tasksrequiringapersondedi-catedtointeractivelyrespondingtobadlywrittenprograms,canbeautomated.
Inalargeenvironment,thetimeandaggravationsavedisimmense.
expectissimilarinstyletotheshell,andcaneasilybemasteredbyanysystemadministratorwhocanprogramintheshellalready.
Thispaperpresentsrealexam-plesofusingexpecttoautomatesystemadministrationtaskssuchaspasswdandfsck.
Alsodiscussedareanumberofothersystemadministrationtasksthatcanbeautomated.
Keywords:expect,fsck,interaction,passwd,password,programmeddialogue,security,shell,Tcl,UNIX,uucpReprintedfromProceedingsoftheFourthUSENIXLISALargeInstallationSys-temsAdministration(LISA)Conference,ColoradoSprings,CO,October17-19,1990.

CloudCone:KVM月付1.99美元起,洛杉矶机房,支持PayPal/支付宝

CloudCone的[2021 Flash Sale]活动仍在继续,针对独立服务器、VPS或者Hosted email,其中VPS主机基于KVM架构,最低每月1.99美元,支持7天退款到账户,可使用PayPal或者支付宝付款,先充值后下单的方式。这是一家成立于2017年的国外VPS主机商,提供独立服务器租用和VPS主机,其中VPS基于KVM架构,多个不同系列,也经常提供一些促销套餐,数据中心在洛杉...

无忧云:服务器100G高防云服务器,bgpBGP云,洛阳BGP云服务器2核2G仅38.4元/月起

无忧云怎么样?无忧云值不值得购买?无忧云,无忧云是一家成立于2017年的老牌商家旗下的服务器销售品牌,现由深圳市云上无忧网络科技有限公司运营,是正规持证IDC/ISP/IRCS商家,主要销售国内、中国香港、国外服务器产品,线路有腾讯云国外线路、自营香港CN2线路等,都是中国大陆直连线路,非常适合免备案建站业务需求和各种负载较高的项目,同时国内服务器也有多个BGP以及高防节点。目前,四川雅安机房,4...

HostKvm - 夏季云服务器七折优惠 香港和韩国机房月付5.95美元起

HostKvm,我们很多人都算是比较熟悉的国人服务商,旗下也有多个品牌,差异化多占位策略营销的,商家是一个创建于2013年的品牌,有提供中国香港、美国、日本、新加坡区域虚拟化服务器业务,所有业务均对中国大陆地区线路优化,已经如果做海外线路的话,竞争力不够。今天有看到HostKvm夏季优惠发布,主要针对香港国际和韩国VPS提供7折优惠,折后最低月付5.95美元,其他机房VPS依然是全场8折。第一、夏...

sendmail为你推荐
赵雨润《星辰变》电影什么时候能开机拍呢?vista系统重装怎样重装vista系统伪静态什么是伪静态直播加速怎么让已拍摄好的视频加速lockdowndios8.1能用gpp3to2吗?型号A1429iphone6上市时间苹果6是什么时候出的 ?安全漏洞计算机一般存在哪些安全漏洞?云挂机快手极速版后台云挂机辅?助各位用了吗?在哪找的?电子商务网站模板电子商务网站策划书srv记录exchange 2010 自动发现需不需要srv记录
高防服务器租用选锐一 日本动态vps 西部数码vps 如何申请免费域名 a2hosting highfrequency softlayer directadmin dux 双拼域名 789电视 google台湾 电信网络测速器 浙江服务器 lamp是什么意思 网站加速 阿里云邮箱申请 wordpress空间 贵州电信 聚惠网 更多