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)

Python installation

In this section, we will be learning about the installation of Python on different platforms, such as Linux and Windows.

Installation on the Linux platform

Most Linux distributions have Python 2 in their default installations. Some of them also have Python 3 included.

To install python3 on Debian-based Linux, run the following command in the Terminal:

sudo apt install python3

To install python3 on centos, run the following command in the Terminal:

sudo yum install python3

If you are unable to install Python using the preceding commands, download Python from https://www.python.org/downloads/ and follow the instructions.

Installation on the Windows platform

For installing Python in Microsoft Windows, you'll have to download the executable from python.org and install it. Download python.exe from https://www.python.org/downloads/ and choose the Python version that you want install on your PC. Then, double-click on the downloaded exe and install Python. On the installation wizard, there's checkbox that says Add Python to the path. Check this checkbox and then follow the instructions to install python3.

Installing and using pip to install packages

In Linux, install pip as follows:

sudo apt install python-pip --- This will install pip for python 2.
sudo apt install python3-pip --- This will install pip for python 3.

In Windows, install pip as follows:

python -m pip install pip

Installation on Mac

To install python3, first we must have brew installed on our system. To install brew on your system, run the following command:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

By running the preceding command. brew will get installed. Now we will install python3 using brew:

brew install python3

Installing Jupyter notebook

For installing the Jupyter Notebook, download Anaconda.

Install the downloaded version of Anaconda and follow the instructions on the wizard.

Install Jupyter using pip:

pip install jupyter

In Linux, pip install jupyter will install Jupyter for python 2. If you want to install jupyter for python 3, run the following command:

pip3 install jupyter

Installing and using the virtual environment

Now we will see how to install the virtual environment and how to activate it.

To install the virtual environment on Linux, perform the following steps:

  1. First check whether pip is installed or not. We are going to install pip for python3:
sudo apt install python3-pip
  1. Install the virtual environment using pip3:
sudo pip3 install virtualenv
  1. Now we will create the virtual environment. You can give it any name; I have called it pythonenv:
virtualenv pythonenv
  1. Activate your virtual environment:
source venv/bin/activate
  1. After your work is done, you can deactivate virtualenv by using following command:
deactivate

In Windows, run the pip install virtualenv command to install the virtual environment. The steps for installing virtualenv are same as with Linux.

Installing Geany and PyCharm

Download Geany from https://www.geany.org/download/releases and download the required binaries. Follow the instructions while installing.

Download PyCharm from https://www.jetbrains.com/pycharm/download/#section=windows and follow the instructions.