Turn repetitive security workflows into reliable Python — file wrangling, log analysis, recon, OSINT, tool orchestration, scheduling, and remote automation.
Part of the [[Readme|Python for Security Professionals]] course.
This is the practical heart of the course: using Python as glue to automate real defensive and offensive tasks. You will build small, runnable scripts that walk file systems, mine logs for attacks, sweep a subnet, gather public OSINT, wrap tools like nmap and whois, run jobs unattended, and drive remote hosts over SSH — all with the standard library plus a few well-known packages.
- Recognise when to automate and how to structure resilient, idempotent glue code.
- Enumerate and transform files at scale with
pathlib,os, andglob. - Extract attack signals from logs using
reandcollections.Counter. - Discover live hosts and open ports with
socketandipaddress. - Gather public information ethically with
requestsandBeautifulSoup. - Safely wrap external tools with
subprocess(noshell=Trueinjection). - Schedule scripts with cron, the
schedulelibrary, and drift-free sleep loops. - Automate remote command execution and file transfer with
paramiko. - Speed up I/O-bound work with threads and processes.
- [[Scripts,-Modules,-Packages,-and-Libraries/Readme|Modules & Packages]] —
subprocess,os, and the concurrency modules. - [[Error-and-Exception-Handling/Readme|Error & Exception Handling]] — automation must survive failures.
- [[Input-Output-File-Handling/Readme|Input/Output File Handling]] — reading target lists and writing reports.
- [[Advanced-Python-Modules/Readme|Advanced Python Modules]] —
reandCounterfor log analysis.
- [[Automating-Repetitive-Tasks|Automating Repetitive Tasks]] — the scripting philosophy and glue-code patterns
- [[File-System-Automation|File System Automation]] — walk trees, batch-rename, find files by pattern
- [[Log-Parsing-and-Analysis|Log Parsing and Analysis]] — mine auth/access logs for failed logins with
re+Counter - [[Subprocess-for-System-Commands|Subprocess for System Commands]] — wrap nmap/whois and parse their output safely
- [[Bash-Script-in-Python|Bash Script in Python]] — drive shell scripts from Python
- [[Network-Reconnaissance-Scripts|Network Reconnaissance Scripts]] — host discovery and subnet sweeps with
socket/ipaddress - [[Web-Scraping-for-OSINT|Web Scraping for OSINT]] — gather public info with
requests+BeautifulSoup(ethically) - [[SSH-Automation|SSH Automation]] — remote commands and file pulls with
paramiko - [[Scheduling-Automated-Tasks|Scheduling Automated Tasks]] — cron, the
schedulelibrary, andtime.sleeploops - [[Threading-Module|Threading Module]] — concurrency for I/O-bound automation
- [[Multiprocessing-Module|Multiprocessing Module]] — parallelism for CPU-bound work
- [[concurrent.futures-(ThreadPoolExecutor)-Module|ThreadPoolExecutor]] — high-level thread pools for I/O-bound sweeps
- [[concurrent.futures-(ProcessPoolExecutor)-Module|ProcessPoolExecutor]] — high-level process pools for CPU-bound work
- Build a resilient batch runner that processes a target list and logs per-item success/failure.
- Write a failed-login detector for your own
auth.logand flag brute-force source IPs. - Sweep
127.0.0.0/30for open ports, then start a listener and confirm it is detected. - Scrape
https://example.compolitely: checkrobots.txt, extract links, rate-limit. - Wrap
nmapagainst127.0.0.1and parse open ports into a table; then schedule it via cron. - Automate SSH to localhost: run commands and pull a file back over SFTP.
The distinction between these four techniques matters, and choosing wrongly is the most common mistake in this module:
| Technique | Use when | Module |
|---|---|---|
| Automation | A task is repetitive and deterministic | os, pathlib, shutil |
| Concurrency (threads) | The bottleneck is waiting — network, disk | threading, ThreadPoolExecutor |
| Multiprocessing | The bottleneck is CPU — hashing, parsing, crypto | multiprocessing, ProcessPoolExecutor |
| External command execution | An existing tool already does the job well | subprocess |
- File system automation — walking trees to find world-writable files, SUID binaries, or stray credential files.
- Log parsing and analysis — extracting failed logins with
re, counting sources withCounter, and correlating by timestamp. - Network reconnaissance — host discovery and subnet sweeps built on
socketandipaddress, parallelised with a thread pool. - Web scraping for OSINT — gathering public information with
requestsandBeautifulSoup, respectingrobots.txtand rate limits. - Wrapping existing tools — driving
nmaporwhoisthroughsubprocessand parsing their structured output. - Scheduled monitoring — running an integrity check or certificate-expiry sweep on a cron schedule.
[!warning] Authorized reconnaissance only Every reconnaissance and scanning example in this module must only be pointed at systems you own or are explicitly authorized to test. Use
127.0.0.1, a lab VM, or a private test network. Concurrency multiplies the traffic you generate — rate-limit deliberately, and remember that automated scanning is both loud and easily logged.
shell=Truewith untrusted input — command injection. Always pass an argument list tosubprocess.run().- Using threads for CPU-bound work — the GIL means no speedup; use processes.
- Using processes for I/O-bound work — startup cost outweighs any gain.
- No timeout on a subprocess or socket, so one hung target blocks the entire run.
- Ignoring a subprocess exit code, treating a failed tool as a clean result.
- Unbounded concurrency — thousands of workers exhaust file descriptors and hammer the target.
- Scraping without rate limiting or ignoring
robots.txt. - Missing the
__main__guard withmultiprocessing, causing recursive process spawning. - Hardcoding credentials in an automation script instead of reading them from the environment.
- Choose threads for waiting and processes for computing; measure with [[Timeit-Module|timeit]] rather than guessing.
- Always pass a list to
subprocess.run()and always set atimeout. - Bound worker counts explicitly and add a deliberate delay between requests.
- Enforce an authorization check before any code that generates traffic.
- Log every action with a timestamp so the run is auditable and reproducible.
- Prefer a tool's machine-readable output (
-oX,--json) over scraping human-readable text. - Make scheduled jobs idempotent so a re-run does not duplicate results.
- What is the difference between concurrency and parallelism, and which does
threadingprovide in CPython? - When should you use
ProcessPoolExecutorrather thanThreadPoolExecutor? - What security issue can occur if
subprocessis called withshell=Trueand a user-supplied target? - How would you troubleshoot an automation run that finishes far too quickly and reports nothing?
- Why must reconnaissance scripts state their authorization scope, and how would you enforce it in code?
- Write a Python example that scans a list of hosts concurrently while limiting the run to 20 workers.
# Install the third-party libraries used in this module
pip install requests beautifulsoup4 paramiko schedule
# System tools the subprocess/recon notes wrap
sudo apt install nmap whois
# Run a scheduled check every 5 minutes (user crontab)
# */5 * * * * /usr/bin/python3 /full/path/check.py >> /tmp/check.log 2>&1- Python docs — the standard library
- requests documentation
- Beautiful Soup documentation
- Paramiko documentation
- [[concurrent.futures-(ThreadPoolExecutor)-Module|concurrent.futures — ThreadPoolExecutor]]
- [[concurrent.futures-(ProcessPoolExecutor)-Module|concurrent.futures — ProcessPoolExecutor]]
- [[Paramiko]] — SSH/SFTP library reference
- [[Readme|Python for Security Professionals]]
- Previous: [[Advanced-Python-Data-Structures/Readme|12. Advanced Python Data Structures]]
- Home: [[Readme|Course Home]]
- Next: [[Security-Tool-Development/Readme|14. Security Tool Development]]