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)

Installing the PyCharm IDE

PyCharm is a fully fledged IDE, used by many developers around the world to write and develop Python code. The IDE is developed by the Jetbrains company and provides rich code analysis and completion, syntax highlighting, unit testing, code coverage, error discovery, and other Python linting operations.

Also, PyCharm Professional Edition supports Python web frameworks, such as Django, web2py, and Flask, beside integrations with Docker and vagrant for running a code over them. It provides amazing integration with multiple version control systems, such as Git (and GitHub), CVS, and subversion.

In the next few steps, we will install PyCharm Community Edition:

  1. Go to the PyCharm download page (https://www.jetbrains.com/pycharm/download/) and choose your platform. Also, choose to download either the Community Edition (free forever) or the Professional Edition (the Community version is completely fine for running the codes in this book):
  1. Install the software as usual, but make sure that you select the following options:
    • 32- or 64-bit launcher (depending on your operating system).
    • Create Associations (this will make PyCharm the default application for Python files).
    • Download and install JRE x86 by JetBrains:
  1. Wait until PyCharm downloads the additional packages from the internet, and installs it, then choose Run PyCharm Community Edition:
  1. Since this is a new and fresh installation, we won't import any settings from
  1. Select the desired UI theme (either the default or darcula, for dark mode). You can install some additional plugins, such as Markdown and BashSupport, which will make PyCharm recognize and support those languages. When you finish, click on Start Using PyCharm:

Setting up a Python project inside PyCharm

Inside PyCharm, a Python project is a collection of Python files that you have developed and Python modules that are either built in or were installed from a third party. You will need to create a new project and save it to a specific location inside your machine before starting to develop your code. Also, you will need to choose the default interpreter for this project. By default, PyCharm will scan the default location on the system and search for the Python interpreter. The other option is to create a completely isolated environment, using Python virtualenv. The basic problem with the virtualenv address is its package dependencies. Let's assume that you're working on multiple different Python projects, and one of them needs a specific version of x package. On the other hand, one of the other projects needs a completely different version from the same package. Notice that all installed Python packages go to /usr/lib/python2.7/site-packages, and you can't store different versions of the same package. The virtualenv will solve this problem by creating an environment that has its own installation directories and its own package; each time you work on either of the two projects, PyCharm (with the help of virtualenv) will activate the corresponding environment to avoid any conflict between packages.

Follow these steps to set up the project:

  1. Choose Create New Project:
  1. Choose the project settings:
    1. Select the type of project; in our case, it will be Pure Python.
    2. Choose the project's location on the local hard drive.
    3. Choose the Project Interpreter. Either use the existing Python installation in the default directory, or create a new virtual environment tied specifically to that project.
    4. Click on Create.
  1. Create a new Python File inside the project:
    1. Right-click on the project name and select New.
    2. Choose Python File from the menu, then choose a filename.

A new, blank file is opened, and you can write a Python code directly into it. Try to import the __future__ module, for example, and PyCharm will automatically open a pop-up window with all possible completions available as shown in the following screenshot:

  1. Run your code:
    1. Enter the code that you wish to run.
    2. Choose Edit Configuration to configure the runtime settings for the Python file.
  1. Configure new Python settings for running your file:
    1. Click on the + sign to add a new configuration, and choose Python.
    2. Choose the configuration name.
    3. Choose the script path inside your project.
    4. Click on OK.
  1. Run the code:
    1. Click on the play button beside the configuration name.
    2. PyCharm will execute the code inside the file specified in the configuration, and will return the output to the terminal.