Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide system-wide open connections similarly to netstat #387

Closed
giampaolo opened this issue May 23, 2014 · 6 comments
Closed

Provide system-wide open connections similarly to netstat #387

giampaolo opened this issue May 23, 2014 · 6 comments

Comments

@giampaolo
Copy link
Owner

From g.rodola on May 31, 2013 16:14:16

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: http://code.google.com/p/psutil/issues/detail?id=387

@giampaolo
Copy link
Owner Author

From [email protected] on May 31, 2013 07:59:26

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/

@giampaolo
Copy link
Owner Author

From [email protected] on July 23, 2013 20:25:54

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.

@giampaolo
Copy link
Owner Author

From g.rodola on March 11, 2014 07:54:46

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.

Status: Started

@giampaolo
Copy link
Owner Author

From g.rodola on March 12, 2014 16:57:17

netstat source code for OSX: 
http://opensource.apple.com/source/network_cmds/network_cmds-329.2.1/netstat.tproj/inet.c

@giampaolo
Copy link
Owner Author

From g.rodola on April 07, 2014 17:07:37

OK, this is done and all implementations were merged into master.

@giampaolo
Copy link
Owner Author

From g.rodola on April 08, 2014 15:13:50

Status: Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant