This library can help you to get a list of running processes, get a list of tcp ports opened by the process, and find a process by the tcp port it opened.
Just pass the pid of process to the function getLocalTcpPorts(of: pid_t)
.
let result = getLocalTcpPorts(of: 56605)
print(result)
It's quite easy to get all the TCP ports opened by one process with a command.
lsof -nP -p <pid> -a -i4TCP
However, if you need to do the job programmatically with Swift in macOS. There's no available documented API to do this.
So I use below functions from libproc
just like the command lsof
do.
- proc_pidfdinfo
- proc_pidinfo
Just use getAllProcess()
.
for process in getAllProcess() {
print(process.pid, process.name)
}
let process = getProcess(from: 13659)