Book Image

Hands-On Enterprise Automation with Python

By : Bassem Aly
Book Image

Hands-On Enterprise Automation with Python

By: Bassem Aly

Overview of this book

Hands-On Enterprise Automation with Python starts by covering the set up of a Python environment to perform automation tasks, as well as the modules, libraries, and tools you will be using. We’ll explore examples of network automation tasks using simple Python programs and Ansible. Next, we will walk you through automating administration tasks with Python Fabric, where you will learn to perform server configuration and administration, along with system administration tasks such as user management, database management, and process management. As you progress through this book, you’ll automate several testing services with Python scripts and perform automation tasks on virtual machines and cloud infrastructure with Python. In the concluding chapters, you will cover Python-based offensive security tools and learn how to automate your security tasks. By the end of this book, you will have mastered the skills of automating several system administration tasks with Python.
Table of Contents (20 chapters)

What this book covers

Chapter 1, Setting Up Python Environment, explores how to download and install the Python interpreter along with the Python Integrated Development Environment, called JetBrains PyCharm. The IDE provides you with smart autocompletion, intelligent code analysis, powerful refactoring and integrates with Git, virtualenv, Vagrant, and Docker. This will help you to write professional and robust Python code.

Chapter 2, Common Libraries Used in Automation, covers the Python libraries that are available today and that are used for automation. We will categorize them based on their usage (system, network, and cloud) and provide a simple introduction. As you progress through the book, you will find yourself deep diving into each of them and understanding their usage.

Chapter 3, Setting up Your Network Lab Environment, discusses the merits of network automation and how network operators use it today to automate the current devices. We will explore popular libraries that are used today to automate network nodes from Cisco, Juniper, and Arista. This chapter covers how to build a networking lab to apply the Python script on. We will use an open source network emulation tool called EVE-NG.

Chapter 4, Using Python to Manage Network Devices, dives into managing networking devices through telnet and SSH connections using netmiko, paramiko, and telnetlib. We will learn how to write the Python code to access switches and routers and execute commands on the terminal and then return the output. We will also learn how to utilize different Python techniques to back up and push configuration. The chapter ends with some use cases used today in modern network environment.

Chapter 5, Extracting Useful Data from Network Devices, explains how to use different tools and techniques inside Python to extract useful data from returned output and act upon it. Also, we will use a special library called CiscoConfParse to audit the configuration. Then we will learn how to visualize data to generate appealing graphs and reports with matplotlib.

Chapter 6, Configuration Generator with Python and Jinja2, explains how to generate a common configuration for a site with hundreds of network nodes. We will learn how to write a template and use it to generate a golden configuration with a templating language called Jinja2.

Chapter 7, Parallel Execution of the Python Script, covers how to instantiate and execute your Python code in parallel. This will allow us to finish the automation workflow faster as long as it is not interdependent.

Chapter 8, Preparing a Lab Environment, covers the installation process and preparation for our lab environment. We will install our automation server either in CentOS or Ubuntu over different hypervisors. Then we will learn how to automate the operating system installation with Cobbler.

Chapter 9, Using the Subprocess Module, explains how to send a command from a Python script directly to the operating system shell and investigate the returning output.

Chapter 10, Running System Administration Tasks with Fabric, introduces Fabric, which is a Python library for executing system administration tasks through SSH. Also, it's used in large deployment of applications. We will learn how to utilize and leverage this library to execute tasks on remote servers.

Chapter 11, Generating System Reports, Managing Users, and System Monitoring, explains that collecting data and generating recurring reports from the system is an essential task for any system administrator, and automating this task will help you to discover issues early and provide a solutions for them. In this chapter, we will see some proven ways to automate data collection from servers and generate formal reports. We will learn how to manage new and existing users using Python and Ansible. Also, we will dive into monitoring the system KPI and logs analysis. You can also schedule the monitoring scripts to run on a regular basis and send the result to your mail inbox.

Chapter 12, Interacting with the Database, states that if you're a database administrator or database developer, then Python provides a wide range of libraries and modules that cover managing and working on popular DBMSes such as MySQL, Postgress, and Oracle. In this chapter, we will learn how to interact with DBMSes using Python connectors.

Chapter 13, Ansible for System Administration, explores one of the most powerful tools in configuration management software. Ansible is very powerful when it comes to system administration and can be used to make sure the configuration is replicated exactly across hundreds or even thousands of servers at the same time.

Chapter 14, Creating and Managing VMWare Virtual Machines, explains how to automate VM creation on a VMWare hypervisor. We will discover different ways to create and manage virtual machines over ESXi using VMWare's official binding library.

Chapter 15, Interacting with Openstack API, explains that OpenStack was very popular in creating private IaaS when it came to private cloud. We will use Python modules such as requests to create REST calls and interact with OpenStack services such as nova, cinder, and neutron, and create the required resources over OpenStack. Later in the chapter, we will use Ansible playbooks for the same workflow.

Chapter 16, Automating AWS with Python and Boto3, covers how to automate common AWS services such as EC2 and S3 using official Amazon binindgs (BOTO3), which provides an easy-to-use API for services access.

Chapter 17, Using the SCAPY Framework, introduces SCAPY, which is a powerful Python tool used to build and craft packets and then send them on the wire. You can build any type of network stream and send it on the wire. It can also help you to capture network packets and replay them to the wire.

Chapter 18, Building Network Scanner Using Python, provides a complete example of building a network scanner using Python. You can scan a complete subnet for different protocols and ports and generate a report for each scanned host. Then, we will learn how to share the code with the open source community (GitHub) by leveraging Git.