Last active
June 10, 2024 20:10
-
-
Save fmerlin/d4a60dc4fcdf1687b9742088eb2e4c1c to your computer and use it in GitHub Desktop.
List all the ports opened by processes on the local machine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import psutil | |
""" | |
List all the ports opened by processes on the local machine | |
to run it with sudo: sudo python port_processes.py | |
""" | |
connections = {} | |
for c in psutil.net_connections(): | |
if c.status == 'LISTEN': | |
connections[c.pid] = connections.get(c.pid, set()) | |
connections[c.pid].add(c.laddr[1]) | |
for k, v in connections.iteritems(): | |
p = psutil.Process(k) | |
print("Process:", p.name(), "Dir:", p.cwd(), "Ports:", list(v)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how do I list the ports that are being used for each PID?