The Background
GITHUB: Scanner Project
During university, I took several courses relating to networking and security. However, much of the coursework was theory-based, with only one course giving us the opportunity to use Cisco Packet Tracer, which I thoroughly enjoyed.
As my interest in APIs grew, I wanted to develop a better understanding of how network protocols worked. This knowledge proved useful while working at FIS, where I was troubleshooting errors involving communication between services.
While investigating the logs, I identified HTTP response codes returned by a client's system. These codes allowed me to narrow down the issue without having direct access to their environment. This experience highlighted how valuable an understanding of network communication can be when troubleshooting technical problems.
With this project, I wanted to explore networking further by developing my own network scanner using Python and Nmap. The aim was to monitor my home network, identify connected devices, and detect unexpected changes that might warrant further investigation.
Requirements and Goals
I wanted to create a small network monitoring tool capable of the following:
Discovering hosts on the network and identifying when devices appear or disappear.
Scanning the 10 most common TCP ports and detecting changes.
Comparing port states between scans.
Running automatically every hour and generating a report.
This project was also an opportunity to practise Python programming, particularly working with JSON files and nested dictionaries. I wanted to explore the Nmap Python package, understand the data it returned, and build a lightweight scanner that I could eventually deploy within my homelab.
Development
I broke the project into four manageable components:
Scanner: Discover connected hosts and scan their ports.
Storage: Save the scan results in JSON format.
Comparison: Compare the latest scan against the previous results.
Reporting: Generate a readable summary of the changes detected.
Scanning the Network
I started by running Nmap directly through the command-line interface to understand the available commands and the results they returned.
Once I had established the two commands I wanted to use, one for host discovery and another for port scanning, I integrated them into Python using the Nmap package.
At this stage, I decided to retain the complete scan results rather than filter the data. This would give me greater flexibility when developing the comparison functions and allow me to revisit the results later if needed.
Storing the Results
The Python Nmap package returns scan results as nested dictionaries. I used Python's built-in json module to serialise these dictionaries into JSON files.
I created a dedicated directory to store the scans, with each file named using the date and time of execution. This allowed the program to identify the previous scan and compare it against the latest results.
Function for saving the nested python dictionaries to json
def to_json(log, file_name,file_path):
json_log = json.dumps(log)
with open(file_path / file_name, "w") as f:
f.write(json_log)
Comparing the Scans
The core functionality of the project was the comparison module.
This module loads the previous scan and compares it against the current results to identify changes in connected hosts and their ports.
I developed functions to identify newly discovered hosts, hosts that had disappeared, and hosts that remained connected. For existing hosts, I then compared their TCP port information to detect any changes.
One issue I encountered was how JSON handles dictionary keys. When saving the scan results, integer dictionary keys were converted into strings. However, the current scan returned by Nmap still contained integer keys, which caused inconsistencies during comparisons.
The solution was relatively straightforward: normalising the data types before performing the comparison.
Once this was working, I created a reporting module to print the results in a readable format in the terminal and save a separate report for each execution.
Deployment and Automation
With the core functionality complete, I containerised the application using Docker.
I then configured a systemd service and timer to execute the scanner automatically at the start of every hour.
This was my first experience setting up this type of scheduled deployment, and the process went smoothly. It was particularly satisfying to have the application running independently without requiring manual execution.
Outcome and Lessons Learned
This was a relatively small project, but it gave me valuable experience working with nested Python dictionaries, JSON serialisation, comparing structured data, and automating a containerised application.
The comparison functionality was particularly useful as a programming exercise, requiring me to think carefully about data structures, missing values, and how information changes between executions.
Although I enjoyed building the scanner, I don't currently plan to expand it significantly.
One of the lessons from this project was recognising when developing a custom solution is worthwhile and when an established tool would be more appropriate.
For my homelab, I believe my time would be better spent learning and implementing existing network monitoring and security tools rather than continuing to develop a scanner that duplicates functionality already available elsewhere.
My next step towards understanding networking is to build a small TCP client-server application in C. By working at a lower level, I hope to develop a better understanding of how applications communicate over a network.
Alongside this, I plan to deploy pfSense within my homelab to explore firewall configuration, network segmentation, and monitoring.
Ultimately, this project achieved what I set out to do: strengthen my Python skills, gain practical experience with Nmap, and build a foundation for more advanced networking projects.