Operations 8 min read

Using Python Scripts for Common Operations Automation Tasks

The article explains how Python can be leveraged for various operations automation tasks—such as remote server management, log parsing, system monitoring, batch deployment, and backup—by showcasing practical examples and code snippets for each use case.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Using Python Scripts for Common Operations Automation Tasks

Python is a popular language for automating operations tasks due to its rich third‑party libraries.

Typical automation scenarios include connecting to remote servers, parsing log files, monitoring system status, batch deploying software, and performing backup and recovery.

Remote execution can be done with the paramiko library for SSH connections, as shown in the example code:

<code>import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='remote.server.com', username='user', password='password')
stdin, stdout, stderr = ssh.exec_command('ls -l /tmp')</code>

Log parsing can be achieved with the regex library to extract error messages, illustrated by a sample script:

<code>import regex
with open('log.txt', 'r') as f:
    log = f.read()
errors = regex.findall(r'ERROR:\s+(.*)', log)
for error in errors:
    print(error)</code>

System monitoring and alerting can be implemented with psutil to obtain CPU usage and smtplib to send email alerts, as demonstrated below:

<code>import psutil
import smtplib
cpu_percent = psutil.cpu_percent()
if cpu_percent > 80:
    server = smtplib.SMTP('smtp.example.com')
    server.login('user', 'password')
    message = f'CPU usage exceeds 80%: {cpu_percent}%'
    subject = 'Alert: High CPU Usage'
    server.sendmail('[email protected]', '[email protected]', subject, message)
    server.quit()</code>

Batch deployment can be simplified using the fabric library, with a task that runs apt-get update on multiple hosts:

<code>from fabric import task
@task
def update_system(c):
    c.run('apt-get update')</code>

Backup and recovery can be performed with the standard shutil module, using copy for files and copytree for directories:

<code>import shutil
shutil.copy('/path/to/file', '/path/to/backup/file')
shutil.copytree('/path/to/dir', '/path/to/backup/dir')</code>

Additional Python tools such as pytest , selenium , numpy , pandas , matplotlib , and machine‑learning libraries further extend its usefulness for sysadmins.

MonitoringAutomationOperationsDeploymentsysadminscripting
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.