Boost Your Ops Efficiency: 5 Python Scripts Every Engineer Should Know
This article explains how Python can automate common operations tasks—remote command execution, log parsing, system monitoring with alerts, batch software deployment, and backup/recovery—providing code examples and practical tips to improve efficiency and reduce manual errors.
Many operations engineers use Python scripts to automate routine tasks because the language offers rich third‑party libraries and strong automation capabilities.
Connect to remote servers and execute commands
Parse log files and extract useful information
Monitor system status and send alerts
Batch‑deploy software or update systems
Perform backup and recovery tasks
1. Connect to remote servers and execute commands
Python can use the
paramikolibrary to establish SSH connections and run commands on remote hosts.
import paramiko
# Create SSH client
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect to remote server
ssh.connect(hostname='remote.server.com', username='user', password='password')
# Execute command
stdin, stdout, stderr = ssh.exec_command('ls -l /tmp')2. Parse log files and extract useful information
The
regexlibrary provides powerful regular‑expression tools for extracting data from logs.
import regex
# Read log file
with open('log.txt', 'r') as f:
log = f.read()
# Find error messages
errors = regex.findall(r'ERROR:\s+(.*)', log)
# Print each error
for error in errors:
print(error)More advanced parsing can be done with libraries such as
loguruor
python‑logstash.
3. Monitor system status and send alerts
The
psutillibrary can retrieve CPU, memory, disk, and network metrics. Combined with
smtplib, it can send email alerts when thresholds are exceeded.
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 is {cpu_percent}%'
subject = 'Alert: High CPU Usage'
server.sendmail('[email protected]', '[email protected]', subject, message)
server.quit()4. Batch‑deploy software or update systems
The
fabriclibrary enables remote command execution across multiple hosts.
from fabric import task
@task
def update_system(c):
c.run('apt-get update')Other tools such as Ansible or Puppet can also be used for large‑scale deployments.
5. Perform backup and recovery tasks
The standard
shutilmodule provides simple file and directory copying functions.
import shutil
# Backup a single file
shutil.copy('/path/to/file', '/path/to/backup/file')
# Backup an entire directory
shutil.copytree('/path/to/dir', '/path/to/backup/dir')Beyond these examples, Python can also support automated testing (e.g.,
pytest,
selenium), data analysis (
numpy,
pandas), visualization (
matplotlib,
seaborn), and machine‑learning workflows (
scikit‑learn,
tensorflow), making it a versatile tool for operations engineers.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.