open-ports-and-programs.py is a cross-platform Python script that enumerates local network connections (both listening and non-listening), listing the corresponding ports, process names, PIDs, and optional DNS lookups. It acts as a flexible alternative to classic tools like netstat
or lsof
, incorporating regex-based filtering, continuous monitoring, and various sorting options.
-
Port & Process Mapping
- Automatically discovers open ports and correlates them to processes/PIDs using
psutil
.
- Automatically discovers open ports and correlates them to processes/PIDs using
-
Sorting Options
- Sort by PID, Port, or Program name for easy navigation of large connection lists.
-
Regex Filtering
- Include or exclude connections by process name or ports using regex (e.g.,
-r "(chrome|firefox|opera)"
).
- Include or exclude connections by process name or ports using regex (e.g.,
-
Continuous Monitoring
- With
-c/--continuous [INTERVAL]
, repeatedly update the list of open ports for real-time tracking.
- With
-
DNS Resolution
- Use
-d/--dns
to resolve remote IP addresses to hostnames, providing more recognizable output.
- Use
-
Bare Output for Scripting
-b/--bare
yields minimal formatting, perfect for piping or further script consumption.
- Clone the Repository
git clone https://github.com/djdarcy/dazzle.git cd dazzle
- Install Dependencies
pip install psutil pytz
- psutil: to inspect processes and network connections.
- pytz: (and the standard library
datetime
) handle timezones for the script’s output.
- Run the Script
python open-ports-and-programs.py [options]
List all listening and non-listening connections, sorted by Program (default):
python open-ports-and-programs.py
python open-ports-and-programs.py --port
Equivalent to -p
, sorts results by numeric port values.
python open-ports-and-programs.py --listening
Limits results to just connections in the LISTEN
state.
# Refresh every 5 seconds, sorting by Port
python open-ports-and-programs.py -p --continuous 5
# Only show connections for Chrome, Firefox, or Opera; sort by PID
python open-ports-and-programs.py --pid --regex "(chrome|firefox|opera)"
python open-ports-and-programs.py --dns
Converts remote IPs to hostnames.
Argument | Description |
---|---|
-s {PID,Port,Program}, --sort {PID,Port,Program} |
Full: Sort by PID, Port, or Program (default: Program). |
-i, --pid |
Quick: Sort output by PID (Process ID). |
-p, --port |
Quick: Sort output by Port. |
-b, --bare |
Bare output format, suitable for use in scripts. |
-l, --listening |
Show only connections in the LISTEN state. |
-d, --dns |
Resolve remote IP addresses to domain names for better readability. |
-r REGEX, --regex REGEX |
Filter connections using a regex pattern applied to process names or ports. |
-c [CONTINUOUS], --continuous [CONTINUOUS] |
Continuously monitor network connections at the specified interval in seconds (default: 10s). |
-h, --help |
Show the help message and exit. |
- DNS Resolution Overhead
- Repeated DNS lookups in
--continuous
mode can slow performance. Consider caching or skipping DNS for large lists.
- Repeated DNS lookups in
- PID Caching
- PIDs are retrieved repeatedly for each connection, which adds overhead. A simple memoization or dictionary cache could make frequent scans more efficient.
- Platform Permissions
- Some OSes require elevated privileges or additional permissions for listing certain connections or processes.
Contributions are welcome. Feel free to:
- Fork the repository.
- Make changes on a new branch (e.g.,
feature/add-protocol-filter
). - Submit a pull request with a clear description of your modifications.
Like the project?
open-ports-and-programs.py, Copyright (C) 2025 Dustin Darcy
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.