Perl for System Administration Jacinta Richardson Paul Fenwick PerlforSystemAdministration byJacintaRichardsonandPaulFenwick Copyright©2006-2008JacintaRichardson([email protected]) Copyright©2006-2008PaulFenwick([email protected]) Copyright©2006-2008PerlTrainingAustralia(http://perltraining.com.au) ConventionsusedthroughoutthistextarebasedupontheconventionsusedintheNetizentrainingmanualsbyKirrilyRobert,andfoundat http://sourceforge.net/projects/spork Distributionofthisworkisprohibitedunlesspriorpermissionisobtainedfromthecopyrightholder. ThistrainingmanualismaintainedbyPerlTrainingAustralia,andcanbefoundathttp://www.perltraining.com.au/notes.html. Thisisrevision1.2ofPerlTrainingAustralia’s"PerlforSystemAdministrators"trainingmanual. Table of Contents 1.AboutPerlTrainingAustralia.......................................................................................................1 Training.......................................................................................................................................1 Consulting...................................................................................................................................1 Contactus....................................................................................................................................1 2.Introduction.....................................................................................................................................3 Courseoutline.............................................................................................................................3 Assumedknowledge...................................................................................................................3 Moduleobjectives.......................................................................................................................3 Platformandversiondetails........................................................................................................3 Thecoursenotes..........................................................................................................................4 Othermaterials............................................................................................................................4 3.WhyusePerlforSystemAdministration?...................................................................................5 4.PerlBasics........................................................................................................................................7 Inthischapter..............................................................................................................................7 Importantbasics..........................................................................................................................7 Help...................................................................................................................................7 Shebangline......................................................................................................................7 Stricturesandwarnings.....................................................................................................7 Strict.........................................................................................................................8 Warnings..................................................................................................................8 Comments..........................................................................................................................8 Startingyourprogram........................................................................................................9 Variables......................................................................................................................................9 Scalars................................................................................................................................9 Quotesandinterpolation........................................................................................10 Arrays..............................................................................................................................10 Arraylookups.........................................................................................................11 Changingarrayelements........................................................................................11 Addingarrayelements...........................................................................................11 Countingbackwards...............................................................................................11 Lastindex...............................................................................................................11 Arraylength...........................................................................................................11 Interpolation...........................................................................................................12 Hashes..............................................................................................................................12 Hashlookups..........................................................................................................12 Changinghashvalues............................................................................................12 Addinghashpairs..................................................................................................13 Hashsize................................................................................................................13 Interpolation...........................................................................................................13 SpecialVariables.............................................................................................................13 $_............................................................................................................................13 @ARGV................................................................................................................13 %ENV....................................................................................................................13 Conditionalsandtruth...............................................................................................................14 Comparisonoperators......................................................................................................14 Booleanoperators............................................................................................................15 if-elsif-else.......................................................................................................................15 unless......................................................................................................................15 PerlTrainingAustralia(http://perltraining.com.au/) iii Trailingconditionals..............................................................................................15 Loopingconstructs....................................................................................................................16 while................................................................................................................................16 foreach.............................................................................................................................16 Subroutines................................................................................................................................17 FileI/O......................................................................................................................................18 Reading............................................................................................................................19 Changingtheinputrecordseparator......................................................................19 Writing.............................................................................................................................20 CPAN........................................................................................................................................20 autodie.......................................................................................................................................21 Chaptersummary......................................................................................................................22 5.Regularexpressions......................................................................................................................23 Inthischapter............................................................................................................................23 Whatareregularexpressions?...................................................................................................23 Regularexpressionoperatorsandfunctions..............................................................................23 m/PATTERN/-thematchoperator.................................................................................23 s/PATTERN/REPLACEMENT/-thesubstitutionoperator............................................24 Exercises................................................................................................................24 Bindingoperators............................................................................................................25 Easymodifiers.................................................................................................................25 Metacharacters.........................................................................................................................25 Someeasymetacharacters..............................................................................................25 Quantifiers.......................................................................................................................27 Exercises..........................................................................................................................27 Groupingtechniques.................................................................................................................28 Characterclasses.............................................................................................................28 Exercises................................................................................................................29 Alternation.......................................................................................................................29 Theconceptofatoms.......................................................................................................30 Exercises...................................................................................................................................30 Chaptersummary......................................................................................................................31 6.Advancedregularexpressions.....................................................................................................33 Inthischapter............................................................................................................................33 Assumedknowledge.................................................................................................................33 Capturingmatchedstringstoscalars........................................................................................33 Extendedregularexpressions....................................................................................................34 Exercise...........................................................................................................................35 Advancedexercise...........................................................................................................35 Greediness.................................................................................................................................35 Exercise...........................................................................................................................36 Moremetacharacters................................................................................................................36 Workingwithmulti-linestrings................................................................................................37 Exercise...........................................................................................................................39 Regexpmodifiersformulti-linedata...............................................................................39 Backreferences.........................................................................................................................40 Specialvariables..............................................................................................................40 Exercises..........................................................................................................................41 Advancedexercises.........................................................................................................42 Chaptersummary......................................................................................................................42 iv PerlTrainingAustralia(http://perltraining.com.au/) 7.Systeminteraction,wrappers,andprocessmanipulation........................................................43 Inthischapter............................................................................................................................43 Platformindependence..............................................................................................................43 Exitvalues.................................................................................................................................43 Invokingshellcommandsusingsystem....................................................................................43 Multipleargumentsystem...............................................................................................44 Problemswithsystem......................................................................................................44 IPC::System::Simpleandautodie....................................................................................45 Capturingaprogram’soutput...................................................................................................47 backticks/qx.....................................................................................................................47 Pipedopen.......................................................................................................................47 Multi-argopen.................................................................................................................48 exec...........................................................................................................................................48 Example-Tapebackups...........................................................................................................49 Sendingsignals.........................................................................................................................50 Chaptersummary......................................................................................................................51 8.Thecommandline.........................................................................................................................53 Inthischapter............................................................................................................................53 Onceoffscripts.........................................................................................................................53 Usingtheexecuteswitch(-e)toconvertfromepoch-time.......................................................53 Script-lessprogramming...........................................................................................................54 Printingswitch(-p)..........................................................................................................54 Non-printingswitch(-n)..................................................................................................55 Moduleswitch(-M).........................................................................................................55 In-placeswitch(-i)..........................................................................................................55 Autosplitswitch(-a)........................................................................................................56 Otherswitches...........................................................................................................................57 Checkswitch(-c).............................................................................................................57 Warningsswitch(-w).......................................................................................................57 Debuggingswitch(-d).....................................................................................................57 Includeswitch(-I)...........................................................................................................57 Taintswitch(-T)..............................................................................................................58 Chaptersummary......................................................................................................................58 9.Filesystemanalysisandtraversal................................................................................................59 Inthischapter............................................................................................................................59 Directoryseparators..................................................................................................................59 Workingwithfiles.....................................................................................................................59 Copying,movingandrenamingfiles...............................................................................59 Deletingfiles....................................................................................................................60 Findinginformationaboutfiles.......................................................................................60 Openthefileonlyif...............................................................................................61 Temporaryfiles................................................................................................................61 Filelocking......................................................................................................................62 Lockingyourprocess.............................................................................................63 FilePermissions........................................................................................................................63 Changingpermissions.....................................................................................................64 Defaultpermissions(umask)...........................................................................................64 Changingownership........................................................................................................64 Links................................................................................................................................65 Workingwithdirectories...........................................................................................................65 Readingdirectories..........................................................................................................65 PerlTrainingAustralia(http://perltraining.com.au/) v Returningnormalfiles............................................................................................66 Creatingandremovingdirectories..................................................................................66 Directorypaths................................................................................................................66 Directoryrepresentations.......................................................................................67 Preventingpathtraversalattacks............................................................................67 Changingdirectories........................................................................................................68 Currentworkingdirectory,absolutepathforfiles...........................................................68 File::Find...................................................................................................................................68 File::Find::Rule................................................................................................................69 Chaptersummary......................................................................................................................69 10.Mailprocessingandfiltering.....................................................................................................71 Inthischapter............................................................................................................................71 Sendingmail.............................................................................................................................71 Withattachments.............................................................................................................71 Filteringmail.............................................................................................................................72 Mail::Audit......................................................................................................................72 Acceptingandfilteringmail...................................................................................73 Chaptersummary......................................................................................................................75 11.Securityconsiderations..............................................................................................................77 Inthischapter............................................................................................................................77 Potentialsecuritypitfalls...........................................................................................................77 Codingforsecurity....................................................................................................................78 Taintchecking...........................................................................................................................78 Turningontaint...............................................................................................................79 Untaintingyourdata........................................................................................................79 Dangerousenvironmentvariables.............................................................................................80 PERL5LIB,PERLLIB,PERL5OPT...............................................................................80 Set-user-idPerlprograms..........................................................................................................80 Chaptersummary......................................................................................................................81 12.Logfileprocessingandmonitoring............................................................................................83 Inthischapter............................................................................................................................83 Tailingfiles................................................................................................................................83 Optionalarguments..........................................................................................................83 File::Tail::App.................................................................................................................84 Exercises..........................................................................................................................85 Interestingdata..........................................................................................................................85 ParsingApacheLogfiles...........................................................................................................86 GeneratingreportsfromlogfileswithLogfile...........................................................................87 LoggingwithPerl......................................................................................................................88 Reallysimplelogging......................................................................................................88 Log4perl..........................................................................................................................89 LoggingtoSyslog............................................................................................................89 Chaptersummary......................................................................................................................90 13.Interactingwithnetworkservices.............................................................................................91 Inthischapter............................................................................................................................91 SendingdatatoIRC..................................................................................................................91 Eventdrivenservices.......................................................................................................91 SendinganAOLinstantmessage..............................................................................................92 Call-backs........................................................................................................................93 Sendingdatatoaspeechengine...............................................................................................93 vi PerlTrainingAustralia(http://perltraining.com.au/) Webbrowsingandscraping......................................................................................................93 WorkingwithLDAP.................................................................................................................95 Connecting.......................................................................................................................95 Searching.........................................................................................................................95 Adding.............................................................................................................................95 Modifying........................................................................................................................96 Chaptersummary......................................................................................................................96 14.FurtherResources......................................................................................................................99 OnlineResources......................................................................................................................99 Books........................................................................................................................................99 Index.................................................................................................................................................101 PerlTrainingAustralia(http://perltraining.com.au/) vii viii PerlTrainingAustralia(http://perltraining.com.au/) List of Tables 1-1.PerlTrainingAustralia’scontactdetails........................................................................................1 5-1.Bindingoperators........................................................................................................................25 5-2.Regexpmodifiers.........................................................................................................................25 5-3.Regularexpressionmetacharacters............................................................................................26 5-4.Regularexpressionquantifiers....................................................................................................27 6-1.Moremetacharacters..................................................................................................................36 6-2.Effectsofsingleandmulti-lineoptions.......................................................................................40 PerlTrainingAustralia(http://perltraining.com.au/) ix x PerlTrainingAustralia(http://perltraining.com.au/)
Description: