diff options
author | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
---|---|---|
committer | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
commit | 5f8de423f190bbb79a62f804151bc24824fa32d8 (patch) | |
tree | 10027f336435511475e392454359edea8e25895d /python/psutil/examples/netstat.py | |
parent | 49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff) | |
download | UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip |
Add m-esr52 at 52.6.0
Diffstat (limited to 'python/psutil/examples/netstat.py')
-rwxr-xr-x | python/psutil/examples/netstat.py | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/python/psutil/examples/netstat.py b/python/psutil/examples/netstat.py new file mode 100755 index 000000000..884622e9e --- /dev/null +++ b/python/psutil/examples/netstat.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python + +# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +""" +A clone of 'netstat -antp' on Linux. + +$ python examples/netstat.py +Proto Local address Remote address Status PID Program name +tcp 127.0.0.1:48256 127.0.0.1:45884 ESTABLISHED 13646 chrome +tcp 127.0.0.1:47073 127.0.0.1:45884 ESTABLISHED 13646 chrome +tcp 127.0.0.1:47072 127.0.0.1:45884 ESTABLISHED 13646 chrome +tcp 127.0.0.1:45884 - LISTEN 13651 GoogleTalkPlugi +tcp 127.0.0.1:60948 - LISTEN 13651 GoogleTalkPlugi +tcp 172.17.42.1:49102 127.0.0.1:19305 CLOSE_WAIT 13651 GoogleTalkPlugi +tcp 172.17.42.1:55797 127.0.0.1:443 CLOSE_WAIT 13651 GoogleTalkPlugi +... +""" + +import socket +from socket import AF_INET, SOCK_STREAM, SOCK_DGRAM + +import psutil + + +AD = "-" +AF_INET6 = getattr(socket, 'AF_INET6', object()) +proto_map = { + (AF_INET, SOCK_STREAM): 'tcp', + (AF_INET6, SOCK_STREAM): 'tcp6', + (AF_INET, SOCK_DGRAM): 'udp', + (AF_INET6, SOCK_DGRAM): 'udp6', +} + + +def main(): + templ = "%-5s %-30s %-30s %-13s %-6s %s" + print(templ % ( + "Proto", "Local address", "Remote address", "Status", "PID", + "Program name")) + proc_names = {} + for p in psutil.process_iter(): + try: + proc_names[p.pid] = p.name() + except psutil.Error: + pass + for c in psutil.net_connections(kind='inet'): + laddr = "%s:%s" % (c.laddr) + raddr = "" + if c.raddr: + raddr = "%s:%s" % (c.raddr) + print(templ % ( + proto_map[(c.family, c.type)], + laddr, + raddr or AD, + c.status, + c.pid or AD, + proc_names.get(c.pid, '?')[:15], + )) + +if __name__ == '__main__': + main() |