Python Project Guide: Making Scripts

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.

💻 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.

  1. Installation:
    pip install psutil
  2. 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

More Topics

Hello! I'm a gaming enthusiast, a history buff, a cinema lover, connected to the news, and I enjoy exploring different lifestyles. I'm Yaman Şener/trioner.com, a web content creator who brings all these interests together to offer readers in-depth analyses, informative content, and inspiring perspectives. I'm here to accompany you through the vast spectrum of the digital world.

Leave a Reply

Your email address will not be published. Required fields are marked *