Python Scripting for Sysadmin Task Automation
Learn to automate repetitive sysadmin tasks with Python scripting—practical examples for file management, log parsing, and system monitoring.
If you’re a sysadmin still SSH-ing into boxes to run the same checks by hand, you’re wasting time. Python is the perfect glue for automating repetitive tasks—from log parsing to user management—without the complexity of full configuration management tools. Here’s how to start scripting your daily grind away, with real examples you can adapt today.
1. Automating Log Parsing with re and collections
Manually grepping logs for errors is fine for one server, but not for ten. Python’s standard library handles this elegantly. Let’s say you want to find the top 5 IPs that triggered 404 errors in an Nginx access log:
import re
from collections import Counter
pattern = re.compile(r'(\d+\.\d+\.\d+\.\d+) .*" 404 ')
ips = []
with open('/var/log/nginx/access.log') as f:
for line in f:
match = pattern.search(line)
if match:
ips.append(match.group(1))
for ip, count in Counter(ips).most_common(5):
print(f"{ip}: {count} times")
Run this via cron daily, and you’ll spot brute-force attempts or broken links without lifting a finger. Extend it to email you the output with smtplib, and you have a poor man’s monitoring alert.
2. Bulk User Management with subprocess
Creating or deleting users across multiple servers? Instead of a fragile bash loop, use subprocess to wrap system commands safely. This script disables a user on a list of remote hosts via SSH:
import subprocess
import sys
user = sys.argv[1]
hosts = ["web01", "web02", "db01"]
for host in hosts:
cmd = f"ssh {host} 'sudo usermod -L {user} && sudo chage -E 0 {user}'"
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
if result.returncode == 0:
print(f"OK: {host}")
else:
print(f"FAIL: {host} - {result.stderr}")
Add error handling and a dry-run flag (--dry-run printing the command instead of executing) and you’ve got a safe, repeatable tool. Remember: always use subprocess.run() with capture_output=True—never os.system().
3. Disk Space Monitoring with shutil and smtplib
Checking disk space on a fleet of servers is a classic. Python’s shutil.disk_usage() gives you the stats directly. Combine it with smtplib to send an alert only when thresholds are crossed:
import shutil
import smtplib
from email.message import EmailMessage
def check_disk(path="/"):
usage = shutil.disk_usage(path)
percent = usage.used / usage.total * 100
return percent
if check_disk() > 85:
msg = EmailMessage()
msg.set_content(f"Disk usage on {__import__('socket').gethostname()} is {check_disk():.1f}%")
msg["Subject"] = "Disk Alert"
msg["To"] = "admin@example.com"
msg["From"] = "monitor@example.com"
with smtplib.SMTP("localhost") as s:
s.send_message(msg)
print("Alert sent.")
else:
print("Disk OK.")
Wrap this in a while True loop with time.sleep(3600) for a lightweight daemon, or run it from cron every hour.
4. Inventory Reporting with os and json
Need a quick hardware inventory? No need for agent-based tools—just walk the filesystem and system files:
import os
import json
import platform
inventory = {
"hostname": platform.node(),
"os": platform.platform(),
"cpu_cores": os.cpu_count(),
"memory_kb": os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES') // 1024,
"disks": []
}
for disk in ["/dev/sda1", "/dev/nvme0n1p1"]:
if os.path.exists(disk):
inventory["disks"].append(disk)
print(json.dumps(inventory, indent=2))
Save the output to a JSON file and feed it into your CMDB or a simple dashboard. You can easily extend this to read psutil if you’re allowed third-party libraries—it gives you CPU, RAM, and network stats in one line.
Conclusion
Start small: pick one repetitive task, write a 20-line script, and schedule it with cron or systemd. Python’s standard library covers most sysadmin needs—parsing, subprocess, email, and filesystem operations—so you don’t need a heavy framework. The payoff is immediate: fewer manual errors, more free time, and scripts you can reuse across your whole infrastructure. Your future self will thank you.