跳转到主要内容

未提供项目描述

项目描述

Python SNMP 库

PyPI Python Versions CI GitHub license

这是一个纯 Python、开源且免费的 SNMP 引擎实现,支持 v1/v2c/v3 SNMP,遵循 2-clause BSD 许可协议

PySNMP 项目最初由 PSF 提供资助。谢谢!

这个版本是基于 Ilya Etingof 的项目 etingof/pysnmp 的分支。Ilya 不幸于 2022 年 8 月 10 日去世。公告 在这里。他的工作对 Python 社区仍然非常有用,我们将怀念他。

特性

  • 完全支持 SNMPv1/v2c 和 SNMPv3
  • SMI 框架用于解析 MIB 信息和实现 SMI 管理对象
  • 完全的 SNMP 实体实现
  • 支持 USM 扩展安全选项(3DES,192/256 位 AES 加密)
  • 可扩展的网络传输框架(UDP/IPv4,UDP/IPv6)
  • 支持基于异步套接字的 IO API
  • Asyncio 集成
  • PySMI 集成以进行动态 MIB 编译
  • 内置仪表,公开协议引擎操作
  • 兼容 Python eggs 和 py2exe
  • 100% Python,兼容 Python 2.4 至 3.7
  • MT-safe(如果 SnmpEngine 是线程局部的话)

针对 SNMPv3 模型的特定特性包括

  • USM 认证(MD5/SHA-1/SHA-2)和隐私(DES/AES)协议(RFC3414,RFC7860)
  • 基于视图的访问控制,可用于任何 SNMP 模型(RFC3415)
  • 内置 SNMP 代理 PDU 转换器,用于构建多语言 SNMP 实体(RFC2576)
  • 远程 SNMP 引擎配置
  • 可选的 SNMP 引擎发现
  • 包含标准 SNMP 应用程序(RFC3413)

下载与安装

PySNMP 软件可从 PyPIGitHub 免费下载。

只需运行

$ pip install pysnmplib

以下命令以下载和安装 PySNMP 及其依赖项

除了库之外,纯 Python 编写的 SNMP 工具 可以通过以下方式安装

$ pip install snmpclitools

并以与传统的 Net-SNMP 工具非常相似的方式使用

$ snmpget.py -v3 -l authPriv -u usr-md5-des -A authkey1 -X privkey1 demo.snmplabs.com sysDescr.0
SNMPv2-MIB::sysDescr.0 = STRING: Linux zeus 4.8.6.5-smp #2 SMP Sun Nov 13 14:58:11 CDT 2016 i686

示例

PySNMP 采用分层设计。最高级且最容易使用的 API 被称为 hlapi。以下是一个关于如何使用 SNMP GET 的快速示例

from pysnmp.hlapi import *

iterator = getCmd(SnmpEngine(),
                  CommunityData('public'),
                  UdpTransportTarget(('demo.snmplabs.com', 161)),
                  ContextData(),
                  ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)))

errorIndication, errorStatus, errorIndex, varBinds = next(iterator)

if errorIndication:  # SNMP engine errors
    print(errorIndication)
else:
    if errorStatus:  # SNMP agent errors
        print('%s at %s' % (errorStatus.prettyPrint(), varBinds[int(errorIndex)-1] if errorIndex else '?'))
    else:
        for varBind in varBinds:  # SNMP response contents
            print(' = '.join([x.prettyPrint() for x in varBind]))

以下是发送 SNMP TRAP 的方法

from pysnmp.hlapi import *

errorIndication, errorStatus, errorIndex, varBinds = next(
    sendNotification(
        SnmpEngine(OctetString(hexValue='8000000001020304')),
        UsmUserData('usr-sha-aes128', 'authkey1', 'privkey1',
                    authProtocol=usmHMACSHAAuthProtocol,
                    privProtocol=usmAesCfb128Protocol),
        UdpTransportTarget(('demo.snmplabs.com', 162)),
        ContextData(),
        'trap',
        NotificationType(ObjectIdentity('SNMPv2-MIB', 'authenticationFailure'))
    )
)

if errorIndication:
    print(errorIndication)

我们维护一个公开可用的 SNMP 代理和 TRAP 池,位于 demo.snmplabs.com。您可以在实验任何 SNMP 软件时使用它。

:warning: 由于 snmplabs.com 网站现已失效,这种情况不再适用

$ python3 examples/hlapi/asyncore/sync/manager/cmdgen/usm-sha-aes128.py
SNMPv2-MIB::sysDescr.0 = SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m
$
$ python3 examples//hlapi/asyncore/sync/agent/ntforg/v3-inform.py
SNMPv2-MIB::sysUpTime.0 = 0
SNMPv2-MIB::snmpTrapOID.0 = SNMPv2-MIB::warmStart
SNMPv2-MIB::sysName.0 = system name

除此之外,PySNMP 能够自动从 HTTP、FTP 网站或本地目录中检索并使用所需的 MIB。您可以为此目的配置任何可用的 MIB 源(包括 此源)。

有关更多示例脚本,请参阅 pysnmp 网站上的 示例部分

文档

库文档和示例可以在 pysnmp 项目网站 上找到。

如果某事没有按预期工作,请 在 GitHub 上打开问题,或在 Stack Overflow 上发布您的问题,或尝试浏览 pysnmp 邮件列表存档

欢迎提交错误报告和 PR!;-)

版权所有 (c) 2005-2019,Ilya Etingof。保留所有权利。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分发

pysnmplib-5.0.24.tar.gz (399.1 kB 查看哈希)

上传时间

构建分发

pysnmplib-5.0.24-py3-none-any.whl (286.7 kB 查看哈希)

上传时间 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面