anfedorov / psutil

Automatically exported from code.google.com/p/psutil
Other
0 stars 0 forks source link

Provide system-wide open connections similarly to netstat #387

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Proposal
Right now we have Process.get_connections() which provides connections opened 
by a specific process.
What the user usually wants to do instead is to retrieve *all* the connections 
opened by the OS and possibly also figure out what PID opened a particular 
connection, a-la "netstat -antp".

Right now if the user wants to emulate netstat he/she has to iterate over *all* 
processes:
https://code.google.com/p/psutil/source/browse/examples/netstat.py
This is slow and undesirable.

Proposal is to add a new psutil.network_connections() returning all connections 
opened by the OS as a list of (family, type, laddr, raddr, status, pid) tuples 
and possibly also deprecate Process.get_connections().
It's likely that the PID associated with a particular connection might not 
always be retrievable (e.g. not enough privileges) in which case pid should be 
set to None instead of raising AccessDenied.

On what platforms would this be available?
All

Proposed API
>>> psutil.network_connections()
[connection(family=2, type=1, laddr=('0.0.0.0', 61171), raddr=(), 
status='LISTEN'), pid=None)
 connection(family=2, type=1, laddr=('127.0.0.1', 53), raddr=(), status='LISTEN'), pid=23845)
 ...]

Are there existent implementations we can use as an example?
Current Linux and Windows implementations can be easily refactored.
OSX and BSD implementations, on the other hand, use system calls referring to a 
specific PID (kinfo_getfile(pid, ...) on BSD, proc_pidinfo(pid, ...) on OSX)) 
so the change required for those platforms is bigger.

Original issue reported on code.google.com by g.rodola on 31 May 2013 at 2:14

GoogleCodeExporter commented 8 years ago
Not the same as direct system calls as we'd be doing in psutil, but there is a 
Python netstat parser module out there that supports various platforms: 

http://code.activestate.com/pypm/pynetstat/

Original comment by jlo...@gmail.com on 31 May 2013 at 2:59

GoogleCodeExporter commented 8 years ago
I think this is basically what I was looking for too. Right now I'm running 
lsof in a subprocess to find processes listening on specific ports ("lsof -i") 
or using specific sockets ("lsof -U"), and then using psutil to query some info 
on those processes, but it would be great if I could just do 
psutil.get_connections(kind=KIND) with KIND being "inet" or "socket" and being 
able to filter those myself. The advantage being that this would be more cross 
platform then my lsof subprocess commands.

Original comment by mroo...@gmail.com on 24 Jul 2013 at 3:25

GoogleCodeExporter commented 8 years ago
Note(s) to self as I stopped working on this for a while and need to figure out 
where I left.

Linux: revision eea7b8cf93cc
Windows: revision 563f08ae6892
FreeBSD: revision 6e91124e047c
SunOS: revision 77972d06c769

What appears to be missing is the OSX implementation.
Most updated code currently lives in "netstat2" HG branch.
All implementations are previous to 2.0 version so they need some namings 
readaptation.

Original comment by g.rodola on 11 Mar 2014 at 2:54

GoogleCodeExporter commented 8 years ago
netstat source code for OSX:
http://opensource.apple.com/source/network_cmds/network_cmds-329.2.1/netstat.tpr
oj/inet.c

Original comment by g.rodola on 12 Mar 2014 at 11:57

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
OK, this is done and all implementations were merged into master.

Original comment by g.rodola on 8 Apr 2014 at 12:07

GoogleCodeExporter commented 8 years ago

Original comment by g.rodola on 8 Apr 2014 at 10:13