Learn how Python tools for IT can make your IT admin life easier. Before you start get our IT Admin Tools runtime environment for Mac and Linux, or IT Admin Tools-Win for Windows, which come with a recent version of Python and all of the tools listed in this post.

Why would one need Python Tools for IT administration? In the age of DevOps, a greater emphasis has been placed on utilizing tools that provide the means to automate both simple and complex administrative tasks. In doing so, IT personnel are freed from the responsibility of manually performing time-consuming and repetitive tasks, such as server configuration or application deployment. This allows IT teams to focus more on complex challenges, process improvement and providing greater value to end users. With a reasonably small learning curve and high-level of readability, Python is a great language for teams looking to automate their workflows and processes.

Today, there exist a plethora of Python apps for automating administrative tasks. Below, I will discuss (in no particular order) 10 of the most useful and valuable Python tools that support IT administration.

Fabric

Built on top of Paramiko and Invoke, Fabric is a Python tool for interacting with remote servers via Secure Shell (SSH), and performing tasks on these servers. Popularly used for automating application deployments, Fabric is also a valid option for the execution of other system administration-related tasks (file transfer, configuring servers, etc.) that need to be performed on one or more remote nodes.

After installing Fabric, the System Administrator creates a file called fabfile.py. Within this file, functions are defined to execute tasks on remote servers. These functions can later be executed via the fab Command Line Interface (CLI).

Consider the (very) simple example below, where I create a data directory (/home/scott/data_dir) on my remote node, and copy a CSV file from my host machine to the remote machine using Fabric:

from fabric import Connection, task
@task
def remote_copy(c):
  c.run('mkdir -p /home/scott/data_dir')
  c.put('data.csv', '/home/scott/data_dir')

I can list the tasks defined in my fabfile via the command fab --list:

C:\Users\Scott\code>fab --list
Available commands:
remote_copy

Next, I can execute my remote_copy task, connecting to my remote machine (remotenode), and using the option to prompt for my password for use with SSH authentication:

C:\Users\Scott\code>fab -H scott@remotenode remote-copy --prompt-for-login-password
Enter login password for use with SSH auth:

After entering the password, the directory data_dir is created on remotenode, and the CSV file is copied over.

The latest versions of Fabric require Python 3.4+. It is free to use, and its functionality is well-documented, allowing any developer or system administrator to wrap their heads around Fabric’s capabilities in a timely manner.

psutil

Another Python package quite useful for IT Administration is psutil. Short for “process and system utilities,” psutil is a library that enables admins and developers to quickly gather information on running processes and system utilization. It is cross-platform, and is typically used for monitoring the state of systems or managing processes.

For instance, an admin may need to pull some CPU statistics and disk usage for the C drive on a machine they are tasked with monitoring, or else during issue troubleshooting. With psutil, this can be accomplished with short code snippet:

import psutil
cpu_stats = psutil.cpu_stats();
print(cpu_stats)
disk_usage = psutil.disk_usage('C:/')
print(disk_usage)
C:\Users\Scott\code>python stats.py
scpustats(ctx_switches=269517545, interrupts=220192269, soft_interrupts=0, syscalls=1196023326)
sdiskusage(total=484993335296, used=84763840512, free=400229494784, percent=17.5)

Click

Click is a Python package for use in creating CLIs quickly and efficiently. In today’s world, System Administrators are writing scripts with greater frequency. Click enables them to do so in a manner that allows them easy access to parameterized functions via the command line, streamlining the process for gathering information or executing commonly performed tasks.

Taking the psutil use case above, where a sysadmin pulled cpu stats and disk usage information, we can now create a script using Click that gives us more control over what’s being pulled by leveraging an argument named task.
If we provide all as the value for the task argument, we will pull both cpu stats and disk usage information. On the other hand, cpu will simply pull cpu stats and disk will only pull disk usage information.

import click
import psutil
@click.command()
@click.option('--task', default='all', help='all, cpu or disk')
def get_stats(task):
  click.echo('getting stats %s' % task)
  if (task == 'cpu' or task == 'all'):
    cpu_stats = psutil.cpu_stats();
    click.echo(cpu_stats)
  if (task == 'disk' or task == 'all'):
    disk_usage = psutil.disk_usage('C:/')
    click.echo(disk_usage)
if __name__ == '__main__':
  get_stats()

The output when executing get_stats to pull cpu stats and disk usage information should like something like:

C:\Users\Scott\code>python get_stats.py --task all
getting stats all
scpustats(ctx_switches=303841026, interrupts=247996435, soft_interrupts=0, syscalls=1305170163)
sdiskusage(total=484993335296, used=85040525312, free=399952809984, percent=17.5)

The output when utilizing the help option, to provide further detail on how to use the CLI we’ve created should look similar to:

C:\Users\Scott\code>python get_stats.py --help
Usage: get_stats.py [OPTIONS]
Options:
  --task TEXT  all, cpu or disk
  --help       Show this message and exit.

Salt

Salt is a Python tool for infrastructure management. Similar to Fabric, Salt allows for a host machine (master) to manage remote nodes (minions).

One particularly advantageous feature offered by Salt is the ability to utilize its event infrastructure to drive reactive processes that enable the implementation of things like intuitive scalability, or even a self-healing infrastructure. SaltStack’s very thorough documentation is kept up-to-date, providing all that’s needed for an IT administration team to begin the adoption of infrastructure-as-code techniques in a manner that can help automate traditionally manual sysadmin responsibilities.

To get started, just follow along with their brief but instructive tutorials on:

Selenium

Automation is the name of the game in DevOps, and Selenium helps organizations do just that with their browser-based web applications. Automated testing via Selenium helps organizations build confidence in their software releases.

While IT administrators may not be directly involved in writing Selenium test scripts, they’re invaluable in terms of time savings for developers and admin personnel alike, since they go a long way toward ensuring the quality of the software being deployed.

Learn more about Selenium.

Ansible

Ansible is an open-source Python tool used to automate a variety of critical IT tasks, including application deployment, configuration management, server provisioning, and more.

At a very high-level, Ansible works by promoting the development of playbooks (written in YAML), while IT administrators write “plays” that are defined by tasks. These tasks dictate the instructions to be executed on the remote hosts.

For example, the following is an Ansible playbook designed to streamline the process for setting up an Apache web server on one or more remote hosts:

---
- hosts: all
  become: true
  vars:
    vars/variables.yml
  tasks:
    - name: Update apt-cache
      apt: update_cache=yes
    - name: Install apache web server
      apt: name=apache2 state=latest
    - name: Create document root
      file: path={{ document_root_path }} state=directory owner=www-data group=www-data
    - name: Copy index.html
      copy: src=index.html dest={{ document_root_path }}/index.html owner=”{{ owner }}” mode=0644
    - name: Set up virtual hosts file
      template: src=virtualhosts.conf dest=/etc/apache2/sites-available/000-default.conf
      notify: Restart Apache
  
  handlers:
    - name: Restart Apache
      service: name=apache2 state=restarted

Requests

Perfectly defined in the Requests documentation, the Requests library allows the user to simply and elegantly send HTTP requests. From an IT administration perspective, this library can help construct and retrieve results for highly-readable HTTP requests in a timely manner, and provide additional insight into issues with a web server or application that an administrator may be tasked with troubleshooting.

For example, if a resource located at URL is misbehaving, the following snippet is a good place to start gathering information on the issue:

req = requests.get(‘<url>’)
print(req.status_code)

Boto3

For administrators working with Windows, pywin32 is an invaluable package which you need to be familiar with. This library provides admins with pythonic access to many functions within the Win32 API, as well as the ability to create and use COM objects. This can prove useful to sys admins looking to build useful Python utilities that need to interact with Windows-specific components.

The pywin32 package allows admins to perform a number of use cases that can make their lives easier. One such use case would be to write a Python program that reads Windows event logs and takes some action based on what they contain. Consider the script below, which simply opens the System event log, prints out the number or records contained, and then closes the log:

import win32evtlog
system_event_log=win32evtlog.OpenEventLog(None, "System")
count = win32evtlog.GetNumberOfEventLogRecords(system_event_log)
print(count)
win32evtlog.CloseEventLog(system_event_log)

Our output:

C:\Users\Scott\code>py check_system_event_log.py
15874

2to3

With Python 2.7 no longer being maintained, it’s very likely your organization has already moved your Python 2 applications to Python 3. But as a system administrator who writes the occasional Python script to help streamline their daily responsibilities, you may still have some scripts that need to be migrated, in which case 2to3 can be a big help.

2to3 is a library that takes your Python 2-compatible code and migrates it for use with Python 3. In many scenarios, this can save you the headache of having to research any incompatibilities between the two versions of Python, and/or trial-and-erroring your way to a working script. The syntax is simple:

2to3 -w my_script.py

Keep in mind that the -w flag tells 2to3 to make the changes directly in my_script.py. If that’s a concern, ensure you make a copy of the original script first.

Read: Python 2 To 3 Migration: A Developer’s Experience

Next Steps

The simplest way to get started making your IT admin life easier with this set of tools is to get our IT Admin Tools runtime environment for Mac and Linux, or IT Admin Tools-Win for Windows, which come with a recent version of Python and all of the tools listed in this post.

The easiest way to get the IT Admin Tools runtime is to first install our State Tool CLI, which will automatically download and install it into a virtual environment for you.

To install the State Tool on Windows, run the following in Powershell:

IEX(New-Object Net.WebClient).downloadString('https://platform.www.activestate.com/dl/cli/install.ps1')

Then type the following at a cmd prompt to automatically install the runtime into a virtual environment:

state activate Pizza-Team/IT-Admin-Tools-Win

To install the State Tool on Linux or Mac, just run the following command in your terminal:

sh <(curl -q https://platform.www.activestate.com/dl/cli/install.sh)

Then run the following to automatically download and install the runtime into a virtual environment:

state activate Pizza-Team/IT-Admin-Tools

To learn more about working with the State Tool, refer to the documentation.

Related Blogs:

Top 10 Tasks to Automate with Python

Top 10 Python Tools To Make a Developer’s Life Easier