Automate Linux Patch Audits With Python: Find Outdated Packages Fast
Every major breach report contains the same sad sentence: the flaw had a patch, and nobody applied it. Patch auditing is boring, repetitive work — which makes it perfect for automation. Today we will build a Python script that checks your own Linux machines for pending updates and tells you what needs attention first.
The Idea
The script runs your package manager’s check for upgradable packages, parses the output, and prints a short ranked report: security updates first, everything else second. Run it on your own servers and laptops on a schedule, and patching stops being a guessing game.
The Script
import subprocessimport sysdef get_upgradable(): try: subprocess.run(["sudo", "apt", "update"], capture_output=True, timeout=300) result = subprocess.run(["apt", "list", "--upgradable"], capture_output=True, text=True, timeout=120) except FileNotFoundError: print("This script targets Debian/Ubuntu (apt).") sys.exit(1) packages = [] for line in result.stdout.splitlines(): if "/" in line and "upgradable" in line: packages.append(line.split("/")[0]) return packagesSECURITY_HINTS = ("security", "openssl", "openssh", "kernel", "linux-image", "curl", "systemd")def rank(pkg: str) -> int: return 0 if any(h in pkg for h in SECURITY_HINTS) else 1if __name__ == "__main__": pkgs = get_upgradable() pkgs.sort(key=rank) print(f"\n{len(pkgs)} upgradable package(s) found.\n") for pkg in pkgs: tag = "[SECURITY?]" if rank(pkg) == 0 else "[routine]" print(f" {tag} {pkg}") if pkgs: print("\nNext step: review, then run 'sudo apt upgrade' in a maintenance window.") else: print("All packages up to date. Nice work.")
How It Works
- Refresh the package lists with apt update, so the results are current rather than last week’s news.
- List upgradable packages with apt list –upgradable and parse out just the names.
- Rank by urgency. Packages whose names hint at security relevance — kernel, OpenSSL, OpenSSH, systemd — sort first. This is a rough heuristic, not a CVE database: it prioritizes your attention, it does not replace checking advisories.
- Print an action list, not a data dump. The output ends with the one command that fixes it.
Running It Safely
- Only run it on machines you own or administer. Never point automation at someone else’s servers.
- Schedule it, don’t memorize it. A weekly cron job plus an alert turns patching into a habit.
- Test updates in a maintenance window. The script reports; it never upgrades on its own. Automatic upgrades can break services — review the list, then upgrade deliberately.
- Level up: for real CVE mapping, feed the package list into your distro’s security tracker or a vulnerability scanner. The script’s job is to make sure nothing sits unnoticed; the scanner’s job is to score it precisely.
Why This Matters Right Now
Look at this week’s news: a perfect-10.0 flaw under active exploitation, VPN zero-days with federal deadlines, a certificate bug in container infrastructure. In every case, the fix already existed — the victims were the ones who had not applied it yet. A ten-line audit script, run weekly, is the difference between “patched” and “breached.” Build it, schedule it, and never be the unpatched machine in the next advisory.



Comments
Post a Comment