Cover image for Automating System Administration with Perl, 2nd Edition

Book description

If you do systems administration work of any kind, you have to deal with the growing complexity of your environment and increasing demands on your time. Automating System Administration with Perl, Second Edition, not only offers you the right tools for your job, but also suggests the best way to approach specific problems and to securely automate recurring tasks.

Updated and expanded to cover the latest operating systems, technologies, and Perl modules, this edition of the "Otter Book" will help you:

  • Manage user accounts

  • Monitor filesystems and processes

  • Work with configuration files in important formats such as XML and YAML

  • Administer databases, including MySQL, MS-SQL, and Oracle with DBI

  • Work with directory services like LDAP and Active Directory

  • Script email protocols and spam control

  • Effectively create, handle, and analyze log files

  • Administer network name and configuration services, including NIS, DNS and DHCP

  • Maintain, monitor, and map network services, using technologies and tools such as SNMP, nmap, libpcap, GraphViz and RRDtool

  • Improve filesystem, process, and network security

This edition includes additional appendixes to get you up to speed on technologies such as XML/XPath, LDAP, SNMP, and SQL. With this book in hand and Perl in your toolbox, you can do more with less -- fewer resources, less effort, and far less hassle.

Table of Contents

  1. Automating System Administration with Perl
  2. Dedication
  3. Preface
    1. What’s New in This Edition?
    2. How This Book Is Structured
    3. Typographical Conventions
    4. Operating System Naming Conventions
    5. Coding Conventions
    6. Using Code Examples
    7. How to Contact Us
    8. Safari® Books Online
    9. Acknowledgments from the First Edition
    10. Acknowledgments for the Second Edition
  4. 1. Introduction
    1. Automation Is a Must
    2. How Perl Can Help You
    3. This Book Will Show You How
    4. What You Need
    5. Some Notes About the Perl Versions Used for This Book
      1. What About Perl 5.10?
      2. What About Strawberry Perl?
      3. What About Perl 6?
    6. Some Notes About Using Vista with the Code in This Book
    7. Locating and Installing Modules
      1. Installing Modules on Unix
      2. Installing Modules on Win32
    8. It’s Not Easy Being Omnipotent
      1. Don’t Do It
      2. Drop Your Privileges As Soon As Possible
      3. Be Careful When Reading Data
      4. Be Careful When Writing Data
      5. Avoid Race Conditions
      6. Enjoy
    9. References for More Information
  5. 2. Filesystems
    1. Perl to the Rescue
    2. Filesystem Differences
      1. Unix
      2. Windows-Based Operating Systems
      3. Mac OS X
      4. Filesystem Differences Summary
      5. Dealing with Filesystem Differences from Perl
    3. Walking or Traversing the Filesystem by Hand
    4. Walking the Filesystem Using the File::Find Module
    5. Walking the Filesystem Using the File::Find::Rule Module
    6. Manipulating Disk Quotas
      1. Editing Quotas with edquota Trickery
      2. Editing Quotas Using the Quota Module
    7. Editing NTFS Quotas Under Windows
    8. Querying Filesystem Usage
    9. Module Information for This Chapter
    10. References for More Information
  6. 3. User Accounts
    1. Unix User Identities
      1. The Classic Unix Password File
      2. Changes to the Password File in BSD 4.4 Systems
        1. Extra fields in passwd files
        2. The binary database format
      3. Shadow Passwords
    2. Windows-Based Operating System User Identities
      1. Windows User Identity Storage and Access
      2. Windows User ID Numbers
      3. Windows Passwords Don’t Play Nice with Unix Passwords
      4. Windows Groups
      5. Windows User Rights
    3. Building an Account System to Manage Users
      1. The Backend Database
        1. Adding to the account queue
      2. The Low-Level Component Library
        1. Unix account creation and deletion routines
        2. Unix account creation and deletion routines—a variation
        3. Windows account creation and deletion routines
      3. The Process Scripts
      4. Account System Wrap-Up
    4. Module Information for This Chapter
    5. References for More Information
      1. Unix Password Files
      2. Windows User Administration
  7. 4. User Activity
    1. Process Management
      1. Windows-Based Operating System Process Control
        1. Using external binaries
        2. Using the Win32::Process::Info module
        3. Using the GUI control modules (Win32::Setupsup and Win32::GuiTest)
        4. Using Windows Management Instrumentation (WMI)
      2. Unix Process Control
        1. Calling an external program
        2. Examining the kernel process structures
        3. Using the /proc filesystem
        4. Using the Proc::ProcessTable module
    2. File and Network Operations
      1. Tracking File Operations on Windows
      2. Tracking Network Operations on Windows
      3. Tracking File and Network Operations in Unix
    3. Module Information for This Chapter
      1. Installing Win32::Setupsup
    4. References for More Information
  8. 5. TCP/IP Name and Configuration Services
    1. Host Files
      1. Generating Host Files
      2. Error-Checking the Host File Generation Process
      3. Improving the Host File Output
      4. Incorporating a Source Code Control System
    2. NIS, NIS+, and WINS
      1. NIS+
      2. Windows Internet Name Server (WINS)
    3. Domain Name Service (DNS)
      1. Generating DNS (BIND) Configuration Files
        1. Creating the administrative header
        2. Generating multiple configuration files
      2. DNS Checking: An Iterative Approach
        1. Using nslookup
        2. Working with raw network sockets
        3. Using Net::DNS
    4. DHCP
      1. Active Probing for Rogue DHCP Servers
      2. Monitoring Legitimate DHCP Servers
    5. Module Information for This Chapter
    6. References for More Information
  9. 6. Working with Configuration Files
    1. Configuration File Formats
      1. Binary
      2. Naked Delimited Data
      3. Key/Value Pairs
      4. Markup Languages
        1. XML
        2. Writing XML from Perl
        3. Survey of best-practice tools to parse and manipulate XML from Perl
        4. Working with XML using XML::Simple
        5. Working with XML using XML::LibXML
        6. Working with XML using SAX2 via XML::SAX
        7. Working with XML using a hybrid approach (XML::Twig)
        8. YAML
    2. All-in-One Modules
    3. Advanced Configuration Storage Mechanisms
    4. Module Information for This Chapter
    5. References for More Information
      1. XML and YAML
  10. 7. SQL Database Administration
    1. Interacting with a SQL Server from Perl
    2. Using the DBI Framework
    3. Using ODBC from Within DBI
    4. Server Documentation
      1. MySQL Server via DBI
      2. Oracle Server via DBI
      3. Microsoft SQL Server via ODBC
    5. Database Logins
    6. Monitoring Space Usage on a Database Server
    7. Module Information for This Chapter
    8. References for More Information
      1. DBI
      2. Microsoft SQL Server
      3. ODBC
      4. Oracle
  11. 8. Email
    1. Sending Mail
      1. Getting sendmail (or a Similar Mail Transport Agent)
      2. Using the OS-Specific IPC Framework to Drive a Mail Client
      3. Speaking the Mail Protocols Directly
        1. Sending vanilla mail messages with Email::Send
        2. Sending mail messages with attachments using Email::Send
        3. Sending HTML mail messages using Email::Send
    2. Common Mistakes in Sending Email
      1. Overzealous Message Sending
        1. Controlling the frequency of mail
        2. Controlling the amount of mail
      2. Subject Line Waste
      3. Insufficient Information in the Message Body
    3. Fetching Mail
      1. Talking POP3 to Fetch Mail
      2. Talking IMAP4rev1 to Fetch Mail
    4. Processing Mail
      1. Dissecting a Single Message
      2. Dissecting a Whole Mailbox
      3. Dealing with Spam
        1. SpamAssassin
        2. Feedback loops
      4. Support Mail Augmentation
    5. Module Information for This Chapter
    6. References for More Information
  12. 9. Directory Services
    1. What’s a Directory?
    2. Finger: A Simple Directory Service
    3. The WHOIS Directory Service
    4. LDAP: A Sophisticated Directory Service
      1. LDAP Programming with Perl
      2. The Initial LDAP Connection
      3. Performing LDAP Searches
      4. Entry Representation in Perl
      5. Adding Entries with LDIF
      6. Adding Entries with Standard LDAP Operations
      7. Deleting Entries
      8. Modifying Entry Names
      9. Modifying Entry Attributes
      10. Deeper LDAP Topics
        1. Referrals and references
        2. Controls and extensions
        3. The root DSE
        4. DSML
      11. Putting It All Together
    5. Active Directory Service Interfaces
      1. ADSI Basics
      2. Using ADSI from Perl
      3. Dealing with Container/Collection Objects
      4. Identifying a Container Object
      5. So How Do You Know Anything About an Object?
      6. Searching
      7. Performing Common Tasks Using the WinNT and LDAP Namespaces
      8. Working with Users via ADSI
      9. Working with Groups via ADSI
      10. Working with File Shares via ADSI
      11. Working with Print Queues and Print Jobs via ADSI
      12. Working with Windows-Based Operating System Services via ADSI
    6. Module Information for This Chapter
    7. References for More Information
      1. LDAP
      2. ADSI
  13. 10. Log Files
    1. Reading Text Logs
    2. Reading Binary Log Files
      1. Using unpack()
      2. Calling an OS (or Someone Else’s) Binary
      3. Using the OS’s Logging API
    3. Structure of Log File Data
    4. Dealing with Log File Information
      1. Space Management of Logging Information
        1. Log rotation
        2. Circular buffering
          1. Input blocking in log-processing programs
          2. Security in log-processing programs
      2. Log Parsing and Analysis
        1. Stream read-count
        2. A simple stream read-count variation
        3. Read-remember-process
        4. Black boxes
        5. Using databases
          1. Using Perl-only databases
          2. Using Perl-cliented SQL databases
    5. Writing Your Own Log Files
      1. Logging Shortcuts and Formatting Help
      2. Basic/Intermediate Logging Frameworks
      3. Advanced Logging Framework
    6. Module Information for This Chapter
    7. References for More Information
  14. 11. Security
    1. Noticing Unexpected or Unauthorized Changes
      1. Local Filesystem Changes
      2. Changes in Data Served Over the Network
    2. Noticing Suspicious Activities
      1. Local Signs of Peril
      2. Finding Problematic Patterns
    3. Danger on the Wire, or “Perl Saves the Day”
    4. Preventing Suspicious Activities
      1. Suggest Better Passwords
      2. Reject Bad Passwords
    5. Module Information for This Chapter
    6. References for More Information
  15. 12. SNMP
    1. Using SNMP from Perl
      1. Sending and Receiving SNMP Traps, Notifications, and Informs
      2. Alternative SNMP Programming Interfaces
    2. Module Information for This Chapter
    3. References for More Information
  16. 13. Network Mapping and Monitoring
    1. Network Mapping
      1. Discovering Hosts
      2. Discovering Network Services
      3. Physical Location
        1. Observation 1: Proximity can help
        2. Observation 2: Conventions can help
    2. Presenting the Information
      1. Textual Presentation Tools
      2. Graphical Presentation Tools
        1. Using the GD::Graph module family
        2. Using GraphViz
        3. Using RRDtool
    3. Monitoring Frameworks
      1. Extending Existing Monitoring Packages
        1. Xymon
        2. Mon
        3. Nagios
    4. What’s Left?
    5. Module Information for This Chapter
    6. References for More Information
  17. 14. Experiential Learning
    1. Playing with Timelines
      1. Task One: Parsing crontab Files
      2. Task Two: Displaying the Timeline
      3. Task Three: Writing Out the Correct XML File
      4. Putting It All Together
      5. Summary: What Can We Learn from This?
    2. Playing with Geocoding
      1. Geocoding from Postal Addresses
      2. Geocoding from IP Addresses
      3. Summary: What Can We Learn from This?
    3. Playing with an MP3 Collection
      1. Summary: What Can We Learn from This?
    4. One Final Exploration
      1. Part One: Retrieving the Wiki Page with WWW::Mechanize
      2. Part Two: Extracting the Data
      3. Part Three: Geocoding and Mapping the Data
      4. Summary: What Can We Learn from This?
    5. Remember to Play
    6. Module Information for This Chapter
    7. Source Material for This Chapter
  18. A. The Eight-Minute XML Tutorial
    1. XML Is a Markup Language
    2. XML Is Picky
    3. Two Key XML Terms
    4. Leftovers
    5. References for More Information
  19. B. The 10-Minute XPath Tutorial
    1. XPath Basic Concepts
      1. Basic Location Paths
      2. Predicates
      3. Abbreviations and Axes
      4. Further Exploration
    2. References for More Information
  20. C. The 10-Minute LDAP Tutorial
    1. LDAP Data Organization
  21. D. The 15-Minute SQL Tutorial
    1. Creating/Deleting Databases and Tables
    2. Inserting Data into a Table
    3. Querying Information
      1. Retrieving All of the Rows in a Table
      2. Retrieving a Subset of the Rows in a Table
      3. Simple Manipulation of Data Returned by Queries
      4. Adding the Query Results to Another Table
    4. Changing Table Information
    5. Relating Tables to Each Other
    6. SQL Stragglers
      1. Views
      2. Cursors
      3. Stored Procedures
  22. E. The Five-Minute RCS Tutorial
    1. References for More Information
  23. F. The Two-Minute VBScript-to-Perl Tutorial
    1. Translation Tactics
      1. Tactic 1: Loading Your Modules
      2. Tactic 2: Referencing an Object
      3. Tactic 3: Accessing Object Properties Using the Hash Dereference Syntax
      4. Tactic 4: Dealing with Container Objects
      5. Tactic 5: Converting Method Invocations
      6. Tactic 6: Dealing with Constants
    2. References for More Information
  24. G. The 20-Minute SNMP Tutorial
    1. SNMP in Practice
  25. Index
  26. About the Author
  27. Colophon
  28. Copyright