Entry
What Python code can I use to find all the network ports that are in use on a server?
Aug 15th, 2002 23:22
Simon Ryan, unknown unknown, Aaron Berg
to figure out what ports are used on a server you could use netstat -a
-n
import os
popen("netstat -a -n")
----
Alternatively if you don't want to fork another process and you have
access to a /proc filesystem you can try opening the following:
/proc/net/tcp
/proc/net/udp
There will be one line per port and the details will be in hex of course.
eg: Sendmail on my system looks like:
9: 00000000:0019 000...(remaining tokens removed for clarity)
It is the ninth port in the list, it is bound to all interfaces
(00000000) and it is port 25 which is 0x0019 hexadecimal