Read "/proc" to know if a process has opened a port
I can read the /proc/$PID/net/tcp file for example and get information about TCP ports opened by the process.
That file is not a list of tcp ports opened by the process. It is a list of all open tcp ports in the current network namespace, and for processes running in the same network namespace is identical to the contents of /proc/net/tcp
.
To find ports opened by your process, you would need to get a list of socket descriptors from /proc/<pid>/fd
, and then match those descriptors to the inode
field of /proc/net/tcp
.
Please
cat /proc/$PID/net/tcp
and you will get output like this
sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode
0: 00000000:01BB 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 2891985097 1 0000000000000000 100 0 0 10 0
The second column (local_address) of the output shows the port in Hexadecimal. Use your Programming Calculator to convert the hex code to decimal.
For example over here, the port :01BB (in hex) is equal to 433 (in decimal) which is the HTTPS default port.