netstat/ss_在C / Python中重新实现netstat

本文介绍了如何使用psutil库在Python中实现类似netstat的功能,允许开发者在纯Python环境中列出系统连接并获取关联进程的PID。这个功能适用于系统监控,例如检查HTTP服务器是否在特定端口运行。文章详细阐述了不同平台的实现方式,包括Linux、Windows、FreeBSD、Solaris和OSX,并指出在OSX上由于系统限制,可能需要root权限。
摘要由CSDN通过智能技术生成

netstat/ss

psutil 2.1.0 is out and with it I finally managed to implement something I’ve been wanting to have for a long time: netstat-like functionalities (see ticket). Similarly to “netstat -antp” on UNIX you can now list system-wide connections in pure python and also determine what process (PID) is using a particular connection:

psutil 2.1.0已经发布,有了它,我终于设法实现了很长时间以来一直想要的东西:类似于netstat的功能(请参阅票证 )。 与UNIX上的“ netstat -antp”类似,您现在可以在纯python中列出系统范围的连接,还可以确定哪个进程(PID)正在使用特定的连接

>>> import psutil
>>> from pprint import pprint as pp
>>> pp(psutil.net_connections())
[sconn(fd=-1, family=2, type=1, laddr=('127.0.0.1', 587), raddr=(), status='LISTEN', pid=None),
 sconn(fd=-1, family=2, type=1, laddr=('127.0.0.1', 6379), raddr=(), status='LISTEN', pid=None),
 sconn(fd=-1, family=2, type=1, laddr=('127.0.1.1', 53), raddr=(), status='LISTEN', pid=None),
 sconn(fd=-1, family=2, type=1, laddr=('10.0.3.1', 53), raddr=(), status='LISTEN', pid=None),
 sconn(fd=-1, family=2, type=1, laddr=('127.0.0.1', 631), raddr=(), status='LISTEN', pid=None),
 sconn(fd=-1, family=2, type=1, laddr=('127.0.0.1', 25), raddr=(), status='LISTEN', pid=None),
 sconn(fd=-1, family=2, type=1, laddr=('0.0.0.0', 3389), raddr=(), status='LISTEN', pid=None),
 sconn(fd=17, family=2, type=1, laddr=('127.0.0.1', 34785), raddr=(), status='LISTEN', pid=3591),
 sconn(fd=15, family=2, type=1, laddr=('127.0.0.1', 56359), raddr=(), status='LISTEN', pid=3591),
 sconn(fd=-1, family=10, type=2, laddr=('::', 56720), raddr=(), status='NONE', pid=None)]
>>>
>>> import psutil
>>> from pprint import pprint as pp
>>> pp(psutil.net_connections())
[sconn(fd=-1, family=2, type=1, laddr=('127.0.0.1', 587), raddr=(), status='LISTEN', pid=None),
 sconn(fd=-1, family=2, type=1, laddr=('127.0.0.1', 6379), raddr=(), status='LISTEN', pid=None),
 sconn(fd=-1, family=2, type=1, laddr=('127.0.1.1', 53), raddr=(), status='LISTEN', pid=None),
 sconn(fd=-1, family=2, type=1, laddr=('10.0.3.1', 53), raddr=(), status='LISTEN', pid=None),
 sconn(fd=-1, family=2, type=1, laddr=('127.0.0.1', 631), raddr=(), status='LISTEN', pid=None),
 sconn(fd=-1, family=2, type=1, laddr=('127.0.0.1', 25), raddr=(), status='LISTEN', pid=None),
 sconn(fd=-1, family=2, type=1, laddr=('0.0.0.0', 3389), raddr=(), status='LISTEN', pid=None),
 sconn(fd=17, family=2, type=1, laddr=('127.0.0.1', 34785), raddr=(), status='LISTEN', pid=3591),
 sconn(fd=15, family=2, type=1, laddr=('127.0.0.1', 56359), raddr=(), status='LISTEN', pid=3591),
 sconn(fd=-1, family=10, type=2, laddr=('::', 56720), raddr=(), status='NONE', pid=None)]
>>>
 

This is yet another functionality which can be used for monitoring purposes. For example, say you want to make sure your HTTP server is running on port 80, you can do something like this:

这是可用于监视目的的另一功能。 例如,假设您要确保HTTP服务器在端口80上运行,则可以执行以下操作:

import psutil

def check_listening_port(port):
    """Return True if the given TCP port is busy and in LISTEN mode."""
    for conn in psutil.net_connections(kind='tcp'):
        if conn.laddr[1] == port and conn.status == psutil.CONN_LISTEN:
            return True
    return False

print(check_listening_port(80))

纯Python中的Netstat ( Netstat in pure python)

Here it is, in 65 lines of code: netstat.py. Pretty neat right? 😉

它在65行代码中: netstat.py 。 很整洁吧? 😉

实施方式 ( Implementation(s))

As always, each platform required its own, different, implementation. Luckily for some platforms (OSX, Windows) I was able to reuse and customize some code from the existing Process.connections() implementation which was already in place. For those of you who are interested in knowing how this was done here’s the source code references:

与往常一样,每个平台都需要自己的,不同的实现。 幸运的是,对于某些平台(OSX,Windows),我能够重用和自定义已经存在的现有Process.connections() 实现中的一些代码。 对于那些想知道如何完成的人,这里是源代码参考:

Hopefully this will help whoever needs to do this into another language. The only platform where this is sort of clunky is OSX, which does not expose anything to list all system-wide sockets in a single shot, so you’re forced to query each process. That means you’ll need root privileges otherwise you’ll get an access denied error. For what it’s worth, I took a look at lsof and it has the same limitation and netstat runs with SUID.

希望这将帮助需要将其翻译成另一种语言的任何人。 OSX唯一笨拙的平台是OSX,它不会公开任何内容以一次性列出所有系统范围的套接字,因此您不得不查询每个进程。 这意味着您将需要root特权,否则将收到拒绝访问错误。 对于它的价值,我看了一下lsof,它具有相同的限制,并且netstat与SUID一起运行。

翻译自: https://www.pybloggers.com/2014/04/reimplementing-netstat-in-c-python/

netstat/ss

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值