System automation with Python
You hand server chores to Python and write repeatable, safe scripts that manage processes, files, SSH connections and scheduled jobs.
Level 301 · 18 lessons · 18 practice labs
Lessons
The local system
- subprocess: timeouts, environment and return codes
- Watching processes, memory and disk with psutil
- Watching files for changes
- Managing permissions and ownership from Python
- Idempotent scripts: the same result when run twice
- Short task: a script that warns before the disk fills up
Remote systems
- Running SSH commands with paramiko
- File transfer over SFTP
- Verifying host keys
- Parallel commands on many servers
- Short task: list local accounts on every server
Scheduling and reporting
- Python with cron and systemd timers
- Running a single instance and lock files
- Sending reports by e-mail: smtplib
- Generating configuration files from templates: Jinja2
- Dry-run mode and confirmed changes
- Short task: report certificates that are about to expire
Capstone
- Capstone: an update and health report for a fleet of servers
Practice labs
- Cutting off a hung command with a timeout: Add a timeout to a command run with subprocess.run so the process is killed on expiry and a meaningful result is returned.
- Listing the top memory-using processes: List the five processes using the most memory with psutil by name and percentage, without failing on vanished processes.
- Watching an inbox folder and processing files: Write a polling watcher that processes new files in a folder once they are fully written, never handling a file twice.
- Fixing key files with loose permissions: Find key files anyone can read in a directory tree, set them to 0600 and report the changes.
- Adding a config line only if it is missing: Fix a script that appends a line to a config file so running it twice does not produce a duplicate.
- Adding a dry run to a clean-up script: Add --dry-run and --yes options to a script that deletes old files so nothing is removed without confirmation.
- Telling a stale lock file apart: Fix a scheduled job that never runs again after a leftover lock file by using a PID check and an fcntl lock.
- Collecting server uptimes over SSH: Connect to the lab SSH targets with paramiko, collect uptime output and report unreachable servers separately.
- Sending a file over SFTP and verifying its hash: Upload a config file to the lab server over SFTP and compare the remote sha256sum output with the local hash.
- Rejecting an unknown host key: Fix a script that uses AutoAddPolicy to verify against a known hosts file so a target with a changed key is refused.
- Bounded parallel commands on many servers: Run one command on the lab servers with at most four connections at a time, sorting results by host and separating failures.
- A wrapper script for a scheduled job: Write a wrapper that runs a job, logs start, end and exit code, and returns non-zero on failure.
- Generating virtual host config from a template: Generate config files from a Jinja2 template and a site list, raising an error on undefined variables instead of leaving blanks.
- Preparing a report e-mail with an attachment: Build a report message with a plain-text body and a CSV attachment using EmailMessage and send it to a local fake SMTP server.
- Wiring a script to a systemd timer: Generate service and timer unit files with Python for an hourly script so missed runs are caught up at boot.
- Writing a daily summary report to a file: Produce a dated HTML summary report from disk and service check results, writing values escaped and updating the file when run again the same day.
- Rendering a greeting file from a template: Render a single greeting file with a Jinja2 template from a dictionary holding the server name, environment and owning team, and compare the output with the expected text.
- Writing template output only when it changed: Generate per-server config files from a shared base template, validate the output, write it atomically only when the content changed and report which servers changed.
About this course
You hand server chores to Python and write repeatable, safe scripts that manage processes, files, SSH connections and scheduled jobs.
What you will learn
- The local system
- Remote systems
- Scheduling and reporting
- Capstone
Prerequisites
You should be comfortable with the level 101 and 201 topics of the same path.
Lab environment
Lessons run in your own Kali Linux terminal in the browser, connected to a private lab network. Nothing to install.
Frequently asked questions
- Is this course right for my level?
- The course is at level 301; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.