Netstat in python. Does it's possible?

"Martin v. Löwis" martin at v.loewis.de
Sat Feb 11 10:28:06 EST 2006


billie wrote:
> Hi all. I don't know if Python is good for this kind of jobs but I'm 
> wondering if it's possible emulate the "netstat" command in Python.

As a general recommendation, use strace(1) to answer this kind of
question. Run "strace -o tmp netstat", then inspect tmp to find out
how netstat obtained the information it reported.

As Sybren suggests, this can all be answered from /proc. For a
process you are interested in, list /proc/<pid>/fd (using os.listdir),
then read the contents of all links (using os.readlink). If the link
value starts with "[socket:", it's a socket. Then search
/proc/net/tcp for the ID. The line containing the ID will have
the information you want.

Regards,
Martin



More information about the Python-list mailing list