Python is an excellent language for system administration, easily replacing tools like bash and AWK for many tasks. This guide focuses on making useful Python 3 scripts to automate jobs, perform security checks, and handle system files. A good script breaks down a large task into smaller, manageable pieces, making it easier to debug and maintain.
Table of Contents
💻 Reading from Standard Input and Parsing Logs
One common sysadmin task is parsing log files. A Python script can read data line-by-line from standard input (stdin
), allowing you to pipe data into it just like a shell command (e.g., cat access.log | ./my_script.py
).
import sys
import re
counter = dict()
for line in sys.stdin:
# Regular expression to find the host IP address
host = re.findall(r'(\S+)', line)[0]
counter[host] = counter.get(host, 0) + 1
for host, count in counter.items():
print(f"{host}: {count}")
This script uses the re
module for regular expressions to parse an Apache log file and count requests per IP address.
💻 Getting System Information with `psutil`
The cross-platform psutil
library is a powerful tool for retrieving information about running processes and system utilization.
- Installation:
pip install psutil
- Example Usage:
import psutil # Get CPU usage over 1 second print(f"CPU Usage: {psutil.cpu_percent(interval=1)}%") # Get virtual memory stats print(psutil.virtual_memory()) # Get info for a specific process (PID 1 is usually 'init') p = psutil.Process(1) print(p.name(), p.uids())
💻 Walking File Systems with `os.walk`
Another essential task is working with files and directories. The os.walk()
function is perfect for recursively traversing a directory tree.
import os
directory = '.'
for root, dirs, files in os.walk(directory):
for file in files:
pathname = os.path.join(root, file)
file_size = os.path.getsize(pathname)
print(f"{pathname} - {file_size} bytes")
This script walks through the current directory and all its subdirectories, printing the full path and size of every file it finds.
—
💻 Continue Your Learning Journey
- Python Project Guide: How to Get Started with Python 3
- Python Project Guide: Using Functions in Python 3
- Python Project Guide: Build an FTP Client & Server
- Python Project Guide: Times, Dates & Numbers
- Python Project Guide: NumPy & SciPy: For Science!
More Topics
- Python Project Guide: NumPy & SciPy: For Science!
- Python Project Guide: Times, Dates & Numbers
- Python Project Guide: Build an FTP Client & Server
- Python Project Guide: Using Functions in Python 3
- Python Project Guide: How to Get Started with Python 3
- Coding Concepts Explained: Avoid Common Coding Mistakes
- Coding Concepts Explained: The Magic of Compilers