Book Image

Mastering Python Scripting for System Administrators

By : Ganesh Sanjiv Naik
Book Image

Mastering Python Scripting for System Administrators

By: Ganesh Sanjiv Naik

Overview of this book

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
Table of Contents (21 chapters)

What this book covers

Chapter 1, Python Scripting Overview, covers the installation procedures for Python as well as the use of the Python interpreter tool. You will learn how to assign values to variables and be introduced to variables and strings. You will study the sequence data types, including lists, tuples, sets, and dictionaries. Also, you will learn how to parse command-line options in scripts.

Chapter 2, Debugging and Profiling Python Scripts, teaches you how to debug Python programs using debugger tools. You will also learn how to handle errors, and explore the concepts of profiling and timing.

Chapter 3, Unit Testing – Introduction to the Unit Testing Framework, is about unit testing in Python. We will create unit tests to test programs.

Chapter 4, Automating Regular Administrative Activities, will teach you how to automate the regular administrative activities of the system administrator. You will learn about accepting inputs, handling passwords, the execution of external commands, reading config files, adding warning codes to scripts, implementing CPU limits, web browser launching, use of the os module, and taking backups.

Chapter 5, Handling Files, Directories, and Data, is where you will learn about using the os module for various activities. You will learn about the data and some methods applied to that data, such as copying, moving, merging, and comparing. You will also learn about the tarfile module and how to use it.

Chapter 6, File Archiving, Encrypting, and Decrypting, delves into file archiving, creating archives, and TAR and ZIP creation. You will also learn about unpacking the .tar and .zip files using applications.

Chapter 7, Text Processing and Regular Expressions, looks at text processing and regular expressions in Python. Python has a very powerful library of things called called regular expressions, which do tasks such as searching and extracting data. You will learn how to use regular expressions with files. You will also learn how to read and write to files.

Chapter 8, Documentation and Reporting, will teach you how to document and report information using Python. You will also learn how to take input using Python scripts and how to print the output. Using Python, you can write scripts for automated information gathering. Writing scripts for receiving emails is easier in Python. You will learn how to format information.

Chapter 9, Working with Various Files, will go into the issue of handling various files, such as PDF files, Excel files, and CSV files. You will learn how to open, edit, and get data from these files using Python.

Chapter 10, Basic Networking – Socket Programming, will first introduce the basics of networking; then you will learn about sockets such as TCP, UDP, and more. You will also learn about how to program sockets to communicate and get information of protocols such as HTTP and FTP.

Chapter 11, Handling Emails Using Python Scripting, explores how to compose and send emails using Python scripts. Sending emails is a very common task in any software program. We can use Python's smtplib module for sending emails in Python programs. In this chapter, you will also learn about the different protocols used for sending emails on different servers.

Chapter 12, Remote Monitoring of Hosts Over Telnet and SSH, shows you how to carry out basic configurations on a server with the SSH protocol. We will begin by using the Telnet module, after which we will implement the same configurations using the preferred method, SSH.

Chapter 13, Building Graphical User Interface, looks at graphical user interface creation with the PyQt module.

Chapter 14, Working with Apache and Other Log Files, explains how to work with Apache log files. You will also learn about log-parsing applications; that is, identifying the particular types of log messages. You will also learn how to parse these files and how to handle multiple files; detecting any exceptions, storing data, and producing reports.

Chapter 15, SOAP and REST API Communication, concerns the basics of SOAP and REST, and the differences between them. You will also get to know the SOAP API and how to use it using different libraries. We will also study the REST API and standard libraries.

Chapter 16, Web Scraping – Extracting Useful Data from Websites, will teach you about extracting data from a website using Python’s libraries. You will also learn how to search for articles and source code using Python.

Chapter 17, Statistics Gathering and Reporting, is about advanced Python libraries that are used in scientific calculations. These libraries are NumPy, SciPy, and Matplotlib. You will learn about the concepts of data visualization and learn how to plot data.

Chapter 18, MySQL and SQLite Database Administrations, looks at database administration using MySQL and SQLite databases. You will learn about the requirements and design for this kind of administration, how to modify the plugin framework, and how to write producer and consumer code.