Mastering Python Scripting for System Administrators

Book description

Leverage the features and libraries of Python to administrate your environment efficiently.

Key Features

  • Learn how to solve problems of system administrators and automate routine activities
  • Learn to handle regular expressions, network administration
  • Building GUI, web-scraping and database administration including data analytics

Book Description

Python has evolved over time and extended its features in relation to every possible IT operation. Python is simple to learn, yet has powerful libraries that can be used to build powerful Python scripts for solving real-world problems and automating administrators' routine activities. The objective of this book is to walk through a series of projects that will teach readers Python scripting with each project.

This book will initially cover Python installation and quickly revise basic to advanced programming fundamentals. The book will then focus on the development process as a whole, from setup to planning to building different tools. It will include IT administrators' routine activities (text processing, regular expressions, file archiving, and encryption), network administration (socket programming, email handling, the remote controlling of devices using telnet/ssh, and protocols such as SNMP/DHCP), building graphical user interface, working with websites (Apache log file processing, SOAP and REST APIs communication, and web scraping), and database administration (MySQL and similar database data administration, data analytics, and reporting).

By the end of this book, you will be able to use the latest features of Python and be able to build powerful tools that will solve challenging, real-world tasks

What you will learn

  • Understand how to install Python and debug Python scripts
  • Understand and write scripts for automating testing and routine administrative activities
  • Understand how to write scripts for text processing, encryption, decryption, and archiving
  • Handle files, such as pdf, excel, csv, and txt files, and generate reports
  • Write scripts for remote network administration, including handling emails
  • Build interactive tools using a graphical user interface
  • Handle Apache log files, SOAP and REST APIs communication
  • Automate database administration and perform statistical analysis

Who this book is for

This book would be ideal for users with some basic understanding of Python programming and who are interested in scaling their programming skills to command line scripting and system administration.

Prior knowledge of Python would be necessary.

Publisher resources

Download Example Code

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Mastering Python Scripting for System Administrators
  3. Dedication
  4. About Packt
    1. Why subscribe?
    2. Packt.com
  5. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Get in touch
      1. Reviews
  7. Python Scripting Overview
    1. Technical requirements
    2. Why Python?
    3. Python syntax compared to other programming languages
    4. Python installation
      1. Installation on the Linux platform
      2. Installation on the Windows platform
        1. Installing and using pip to install packages
      3. Installation on Mac
      4. Installing Jupyter notebook
      5. Installing and using the virtual environment
      6. Installing Geany and PyCharm
    5. Python interpreter
      1. The difference between Python and Bash scripting
      2. Starting the interactive console
      3. Writing scripts with the Python interactive console
      4. Multiple lines
      5. Importing modules through the Python interpreter
      6. Exiting the Python console
        1. The keyboard shortcut
        2. Using the quit() or exit() functions
      7. Indentation and tabs
      8. Variables
        1. Creating and assigning values to variables
      9. Numbers
        1. Number type conversion
    6. Strings
      1. Concatenation (+) and repetition (*)
      2. String slicing
      3. Accessing values in strings
      4. Updating strings
      5. Escape characters
      6. Special string operators
      7. %  string formatting operator
      8. Triple quotes in Python
      9. Strings are immutable
    7. Understanding lists
      1. Accessing values in lists
      2. Updating lists
      3. Deleting list elements
      4. Basic list operations
      5. List operations
      6. Indexing, slicing, and matrices
    8. Tuples
      1. Accessing values in tuples
      2. Updating tuples
      3. Deleting tuple elements
      4. Basic tuple operations
      5. Indexing, slicing, and matrices
      6. max() and min()
    9. Sets
    10. Dictionaries
    11. Parsing command-line arguments
      1. Command-line arguments in Python
      2. Sys.argv
    12. Decision making
      1. Python if statement syntax
      2. Python if...else statement syntax
      3. Python if...elif...else statement
    13. Loops
      1. for loop
      2. The range() function
      3. while loop
    14. Iterators
    15. Generators
      1. How to create a generator in Python?
    16. Functions
      1. The return statement
      2. Lambda functions
    17. Modules
      1. Importing modules
    18. Summary
    19. Questions
    20. Further reading
  8. Debugging and Profiling Python Scripts
    1. What is debugging?
      1. Python debugging techniques
    2. Error handling (exception handling)
    3. Debuggers tools
      1. The pdb debugger
      2. Within an interpreter
      3. From a command line
      4. Within a Python script
    4. Debugging basic program crashes
    5. Profiling and timing programs
      1. The cProfile module
      2. timeit
    6. Making programs run faster
    7. Summary
    8. Questions
    9. Further reading
  9. Unit Testing - Introduction to the Unit Testing Framework
    1. What is unittest?
    2. Creating unit tests
    3. Methods used in unit testing
    4. Summary
    5. Questions
    6. Further reading
  10. Automating Regular Administrative Activities
    1. Accepting input by redirection, pipe, and input files
      1. Input by redirection
      2. Input by pipe
      3. Input by input file
    2. Handling passwords at runtime in scripts
    3. Executing external commands and getting their output
    4. Capturing output using the subprocess module
    5. Prompting for passwords during runtime and validation
    6. Reading configuration files
    7. Adding logging and warning code to scripts
      1. Generating warnings
    8. Putting limits on CPU and memory usage
    9. Launching webbrowser
    10. Using the os module for handling directory and files
      1. Creating and deleting the directory
      2. Examining the content of a filesystem
    11. Making backups (with rsync)
    12. Summary
    13. Questions
    14. Further reading
  11. Handling Files, Directories, and Data
    1. Using the os module to work with directories
      1. Get the working directory
      2. Changing the directory
      3. Listing files and directories
      4. Renaming a directory
    2. Copying, moving, renaming, and deleting data
      1. Copying the data
      2. Moving the data
      3. Renaming data
      4. Deleting data
    3. Working with paths
    4. Comparing data
    5. Merging data
    6. Pattern matching files and directories
    7. Metadata: data about data
    8. Compressing and restoring
    9. Using the tarfile module to create TAR archives
    10. Using a tarfile module to examine the contents of TAR files
    11. Summary
    12. Questions
    13. Further reading
  12. File Archiving, Encrypting, and Decrypting
    1. Creating and unpacking archives
      1. Creating archives
      2. Unpacking archives
    2. Tar archives
    3. ZIP creation
    4. File encryption and decryption
    5. Summary
    6. Questions
    7. Further reading
  13. Text Processing and Regular Expressions
    1. Text wrapping
      1. The wrap() function
      2. The fill() function
      3. The dedent() function
      4. The indent() function
      5. The shorten() function
    2. Regular expressions
      1. The match() function
      2. The search() function
      3. The findall() function
      4. The sub() function
    3. Unicode strings
      1. Unicode code point
      2. Encoding
      3. Decoding
      4. Avoiding UnicodeDecodeError
    4. Summary
    5. Questions
    6. Further reading
  14. Documentation and Reporting
    1. Standard input and output
    2. Information formatting
    3. Sending email
    4. Summary
    5. Questions
    6. Further reading
  15. Working with Various Files
    1. Working with PDF files
      1. Reading a PDF document and getting the number of pages
      2. Extracting text
      3. Rotating PDF pages
    2. Working with Excel files
      1. Using the xlrd module
        1. Reading an Excel file
        2. Extracting the names of columns
      2. Using pandas 
        1. Reading an Excel file
        2. Reading specific columns in an Excel file
      3. Using openpyxl
        1. Creating a new Excel file
        2. Appending values
        3. Reading multiple cells
    3. Working with CSV files
      1. Reading a CSV file
      2. Writing into a CSV file
    4. Working with txt files
      1. The open() function
        1. File opening
      2. The close() function
      3. Writing a text file
      4. Reading a text file
    5. Summary
    6. Questions
    7. Further reading
  16. Basic Networking - Socket Programming
    1. Sockets
    2. The http package
      1. The http.client module
      2. The http.server module
    3. The ftplib module
      1. Downloading files
      2. Getting a welcome message using getwelcome():
      3. Sending commands to the server using the sendcmd() function
    4. The urllib package
      1. Python urllib response headers
    5. Summary
    6. Questions
    7. Further reading
  17. Handling Emails Using Python Scripting
    1. Email message format
    2. Adding HTML and multimedia content
    3. POP3 and IMAP servers
      1. Receiving email using the poplib library
      2. Receiving email using the imaplib library
    4. Summary
    5. Questions
  18. Remote Monitoring of Hosts Over Telnet and SSH
    1. The telnetlib() module
      1. SSH
    2. The subprocess.Popen() module
    3. SSH using fabric module
    4. SSH using the Paramiko library
    5. SSH using the Netmiko library
    6. Summary
    7. Questions
    8. Further reading
  19. Building Graphical User Interfaces
    1. Introduction to GUI
    2. Using a library to create a GUI-based application
    3. Installing and using the Apache Log Viewer app
    4. Summary
    5. Questions
    6. Further reading
  20. Working with Apache and Other Log Files
    1. Parsing complex log files
    2. The need for exceptions
      1. Analyzing exceptions
    3. Tricks for parsing different files
    4. Error log
    5. Access log
      1. Common log format
    6. Parsing other log files
    7. Summary
    8. Questions
    9. Further reading
  21. SOAP and REST API Communication
    1. What is SOAP?
      1. Using libraries for SOAP
    2. What is a RESTful API?
      1. Using standard libraries for RESTful APIs
    3. Working with JSON data
    4. Summary
    5. Questions
    6. Further reading
  22. Web Scraping - Extracting Useful Data from Websites
    1. What is web scraping?
    2. Data extraction
      1. The requests library
      2. The beautifulsoup library
    3. Extracting information from Wikipedia
    4. Summary
    5. Questions
    6. Further reading
  23. Statistics Gathering and Reporting
    1. NumPY module
      1. Using arrays and scalars
      2. Array indexing
        1. Indexing a 2D array
      3. Universal array functions
    2. Pandas module
      1. Series
      2. DataFrames
    3. Data visualization
      1. Matplotlib
        1. Histograms
        2. Scatter plots
        3. Bar charts
      2. Plotly
        1. Scatter plots
        2. Line scatter plots
        3. Box plots
        4. Contour plots
    4. Summary
    5. Questions
    6. Further reading
  24. MySQL and SQLite Database Administrations
    1. MySQL database administration
      1. Getting a database version
      2. Creating a table and inserting data
      3. Retrieving the data
      4. Updating the data
      5. Deleting the data
    2. SQLite database administration
      1. Connecting to the database
      2. Creating a table
      3. Inserting the data
      4. Retrieving the data
      5. Updating the data
      6. Deleting the data
    3. Summary
    4. Questions
    5. Further reading
  25. Assessments
    1. Chapter 1, Python Scripting Overview
    2. Chapter 2, Debugging and Profiling Python Scripts
    3. Chapter 3, Unit Testing – Introduction to the Unit Testing Framework
    4. Chapter 4, Automating Regular Administrative Activities
    5. Chapter 5, Handling Files, Directories, and Data
    6. Chapter 6, File Archiving, Encrypting, and Decrypting
    7. Chapter 7, Text Processing and Regular Expressions
    8. Chapter 8, Documentation and Reporting
    9. Chapter 9, Working with Various Files
    10. Chapter 10, Basic Networking – Socket Programming
    11. Chapter 11, Handling Emails Using Python Scripting
    12. Chapter 12, Remote Monitoring of Hosts Over Telnet and SSH
    13. Chapter 13, Building Graphical User Interfaces
    14. Chapter 14, Working with Apache and Other Log Files
    15. Chapter 15, SOAP and REST API Communication
    16. Chapter 16, Web Scraping – Extracting Useful Data from Websites
    17. Chapter 17, Statistics Gathering and Reporting
    18. Chapter 18, MySQL and SQLite Database Administrations
  26. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Mastering Python Scripting for System Administrators
  • Author(s): Ganesh Sanjiv Naik
  • Release date: January 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781789133226