文章目录
- SNMP(Simple Network Management Protocol) 是用于网络设备管理的标准协议,支持对路由器、交换机、服务器等设备的监控和配置。核心功能包括: GET:查询设备信息(如 CPU 使用率、接口状态)。 SET:修改设备配置(如设置设备名称)。 TRAP/INFORM:设备主动上报事件(如接口故障)。
- 角色 功能 对应 pysnmp 模块 Manager 管理端(客户端),发起 SNMP 请求(GET/SET) hlapi(高级 API) Agent 被管理设备端(服务器),响应请求并执行操作 pysnmp.entity(Agent 实现) MIB 管理信息库,定义可管理的对象(OID)及其属性(类型、权限等) pysnmp.smi(MIB 解析) OID 对象标识符,唯一标识被管理的对象(如 1.3.6.1.2.1.1.1.0 表示系统描述) ObjectIdentity 类
- 结构:分层树形结构,如 1.3.6.1.2.1.1.1.0 对应 iso.org.dod.internet.mgmt.mib-2.system.sysDescr.0。 作用:唯一标识被管理的对象(如系统名称、接口流量)。 在 pysnmp 中的操作: from pysnmp.hlapi import ObjectIdentity # 通过 OID 字符串创建 oid = ObjectIdentity(‘1.3.6.1.2.1.1.1.0’) # 通过 MIB 符号创建(需加载 MIB) oid = ObjectIdentity(‘SNMPv2-MIB’, ‘sysDescr’, 0)
- 功能:定义 OID 的元数据(名称、数据类型、访问权限等)。 在 pysnmp 中的加载: from pysnmp.smi import builder, view # 初始化 MIB 编译器 mib_builder = builder.MibBuilder() mib_view = view.MibViewController(mib_builder) # 加载 MIB 文件 mib_builder.loadModule(‘SNMPv2-MIB’)
- 版本 特点 pysnmp 实现类 v1 基于社区名(明文),无加密 CommunityData(community) v2c 改进错误处理,仍使用社区名 CommunityData(community) v3 支持用户认证(USM)、加密(AES/DES) UsmUserData(user, authKey, privKey)
- SNMP 消息由 Header、Security Parameters(仅 v3)和 PDU 组成: SNMP Message (BER Encoded) ├── Version ├── Community/UsmSecurityParameters (v3) └── PDU (Protocol Data Unit) ├── PDU Type (GET/SET/GETNEXT…) ├── Request ID ├── Error Status ├── Error Index └── Variable Bindings (OID-Value Pairs)
- 编码(发送请求): from pysnmp.proto.api import v2c # 构建 GET 请求 PDU pdu = v2c.GetRequestPDU().addVarBinds((‘1.3.6.1.2.1.1.1.0′, v2c.OctetString(”))) message = v2c.Message(apiVersion=1, community=’public’, pdu=pdu) # BER 编码为二进制 encoded_message = message.encode() 解码(接收响应): decoded_message, _ = v2c.Message.decode(encoded_message) response_pdu = decoded_message[‘pdu’]
- 层级 功能 pysnmp 模块 应用层 用户接口(GET/SET/WALK) hlapi 协议层 消息构建、编码/解码、安全处理(v3) proto, entity 传输层 网络通信(UDP/TCP) carrier MIB 层 OID 解析与 MIB 管理 smi
- SNMP 概念 pysnmp 类/方法 示例代码 Community (v2c) CommunityData CommunityData('public', mpModel=1) User (v3) UsmUserData UsmUserData('user1', authKey='auth123') PDU GetRequestPDU, SetRequestPDU v2c.GetRequestPDU() Transport UdpTransportTarget, asyncore 事件循环 UdpTransportTarget(('192.168.1.1', 161)
- 用户发起请求: from pysnmp.hlapi import getCmd, ObjectType, ObjectIdentity error_indication, error_status, error_index, var_binds = next( getCmd(SnmpEngine(), CommunityData(‘public’), UdpTransportTarget((‘192.168.1.1’, 161)), ContextData(), ObjectType(ObjectIdentity(‘1.3.6.1.2.1.1.1.0’))) 协议层构建 PDU: 创建 GetRequestPDU,填充 OID。 传输层发送数据: 使用 UDP 发送 BER 编码的 SNMP 消息。 Agent 处理请求: 查找 OID 对应的值,生成 GetResponsePDU。 Manager 解析响应: 解码响应并返回 var_binds。
- 底层操作:通过连续发送 GETNEXT 请求,直到 OID 超出子树范围。 在 pysnmp 中的实现: from pysnmp.hlapi import nextCmd for (error_indication, error_status, error_index, var_binds) in nextCmd( SnmpEngine(), CommunityData(‘public’), UdpTransportTarget((‘192.168.1.1’, 161)), ContextData(), ObjectType(ObjectIdentity(‘1.3.6.1.2.1.1’))): # 处理每个 OID-Value 对
- 认证(Authentication): 算法:MD5、SHA(usmHMACMD5AuthProtocol、usmHMACSHAAuthProtocol)。 防止数据篡改。 加密(Privacy): 算法:DES、AES(usmDESPrivProtocol、usmAesCfb128Protocol)。 防止数据窃听。
- from pysnmp.hlapi import UsmUserData, getCmd error_indication, error_status, error_index, var_binds = next( getCmd(SnmpEngine(), UsmUserData(‘user1′, authKey=’authkey123′, privKey=’privkey123’, authProtocol=usmHMACSHAAuthProtocol, privProtocol=usmAesCfb128Protocol), UdpTransportTarget((‘192.168.1.1’, 161)), ContextData(), ObjectType(ObjectIdentity(‘1.3.6.1.2.1.1.1.0’))) )
目录
- 1. SNMP 协议基础
- 1.1 SNMP 是什么?
- 1.2 SNMP 架构
- 2. SNMP 协议核心概念
- 2.1 OID(Object Identifier)
- 2.2 MIB(Management Information Base)
- 2.3 SNMP 版本
- 3. SNMP 消息结构与pysnmp实现
- 3.1 SNMP 消息格式
- 3.2pysnmp中的编码与解码
- 4.pysnmp模块设计与 SNMP 协议映射
- 4.1 分层架构
- 4.2 核心类与 SNMP 协议对应
- 5. 深入 SNMP 操作流程
- 5.1 GET 请求全流程
- 5.2 SNMP Walk 实现原理
- 6. SNMPv3 安全机制与pysnmp
- 6.1 USM(User Security Model)
- 6.2pysnmp中的 SNMPv3 配置
- 7. 总结
SNMP(Simple Network Management Protocol) 是用于网络设备管理的标准协议,支持对路由器、交换机、服务器等设备的监控和配置。核心功能包括:
- GET:查询设备信息(如 CPU 使用率、接口状态)。
- SET:修改设备配置(如设置设备名称)。
- TRAP/INFORM:设备主动上报事件(如接口故障)。
| 角色 | 功能 | 对应 pysnmp 模块 |
|---|---|---|
| Manager | 管理端(客户端),发起 SNMP 请求(GET/SET) | hlapi(高级 API) |
| Agent | 被管理设备端(服务器),响应请求并执行操作 | pysnmp.entity(Agent 实现) |
| MIB | 管理信息库,定义可管理的对象(OID)及其属性(类型、权限等) | pysnmp.smi(MIB 解析) |
| OID | 对象标识符,唯一标识被管理的对象(如 1.3.6.1.2.1.1.1.0 表示系统描述) | ObjectIdentity 类 |
- 结构:分层树形结构,如
1.3.6.1.2.1.1.1.0 对应 iso.org.dod.internet.mgmt.mib-2.system.sysDescr.0。
- 作用:唯一标识被管理的对象(如系统名称、接口流量)。
- 在
pysnmp 中的操作:
from pysnmp.hlapi import ObjectIdentity
# 通过 OID 字符串创建
oid = ObjectIdentity('1.3.6.1.2.1.1.1.0')
# 通过 MIB 符号创建(需加载 MIB)
oid = ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)
1.3.6.1.2.1.1.1.0 对应 iso.org.dod.internet.mgmt.mib-2.system.sysDescr.0。pysnmp 中的操作:
from pysnmp.hlapi import ObjectIdentity
# 通过 OID 字符串创建
oid = ObjectIdentity('1.3.6.1.2.1.1.1.0')
# 通过 MIB 符号创建(需加载 MIB)
oid = ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)
- 功能:定义 OID 的元数据(名称、数据类型、访问权限等)。
- 在
pysnmp 中的加载:
from pysnmp.smi import builder, view
# 初始化 MIB 编译器
mib_builder = builder.MibBuilder()
mib_view = view.MibViewController(mib_builder)
# 加载 MIB 文件
mib_builder.loadModule('SNMPv2-MIB')
pysnmp 中的加载:
from pysnmp.smi import builder, view
# 初始化 MIB 编译器
mib_builder = builder.MibBuilder()
mib_view = view.MibViewController(mib_builder)
# 加载 MIB 文件
mib_builder.loadModule('SNMPv2-MIB')
| 版本 | 特点 | pysnmp 实现类 |
|---|---|---|
| v1 | 基于社区名(明文),无加密 | CommunityData(community) |
| v2c | 改进错误处理,仍使用社区名 | CommunityData(community) |
| v3 | 支持用户认证(USM)、加密(AES/DES) | UsmUserData(user, authKey, privKey) |
SNMP 消息由 Header、Security Parameters(仅 v3)和 PDU 组成:
SNMP Message (BER Encoded)
├── Version
├── Community/UsmSecurityParameters (v3)
└── PDU (Protocol Data Unit)
├── PDU Type (GET/SET/GETNEXT...)
├── Request ID
├── Error Status
├── Error Index
└── Variable Bindings (OID-Value Pairs)
- 编码(发送请求):
from pysnmp.proto.api import v2c
# 构建 GET 请求 PDU
pdu = v2c.GetRequestPDU().addVarBinds(('1.3.6.1.2.1.1.1.0', v2c.OctetString('')))
message = v2c.Message(apiVersion=1, community='public', pdu=pdu)
# BER 编码为二进制
encoded_message = message.encode()
- 解码(接收响应):
decoded_message, _ = v2c.Message.decode(encoded_message)
response_pdu = decoded_message['pdu']
from pysnmp.proto.api import v2c
# 构建 GET 请求 PDU
pdu = v2c.GetRequestPDU().addVarBinds(('1.3.6.1.2.1.1.1.0', v2c.OctetString('')))
message = v2c.Message(apiVersion=1, community='public', pdu=pdu)
# BER 编码为二进制
encoded_message = message.encode()
decoded_message, _ = v2c.Message.decode(encoded_message) response_pdu = decoded_message['pdu']
| 层级 | 功能 | pysnmp 模块 |
|---|---|---|
| 应用层 | 用户接口(GET/SET/WALK) | hlapi |
| 协议层 | 消息构建、编码/解码、安全处理(v3) | proto, entity |
| 传输层 | 网络通信(UDP/TCP) | carrier |
| MIB 层 | OID 解析与 MIB 管理 | smi |
| SNMP 概念 | pysnmp 类/方法 | 示例代码 |
|---|---|---|
| Community (v2c) | CommunityData | CommunityData('public', mpModel=1) |
| User (v3) | UsmUserData | UsmUserData('user1', authKey='auth123') |
| PDU | GetRequestPDU, SetRequestPDU | v2c.GetRequestPDU() |
| Transport | UdpTransportTarget, asyncore 事件循环 | UdpTransportTarget(('192.168.1.1', 161) |
- 用户发起请求:
from pysnmp.hlapi import getCmd, ObjectType, ObjectIdentity
error_indication, error_status, error_index, var_binds = next(
getCmd(SnmpEngine(),
CommunityData('public'),
UdpTransportTarget(('192.168.1.1', 161)),
ContextData(),
ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0')))
- 协议层构建 PDU:
- 创建
GetRequestPDU,填充 OID。
- 传输层发送数据:
- 使用 UDP 发送 BER 编码的 SNMP 消息。
- Agent 处理请求:
- 查找 OID 对应的值,生成
GetResponsePDU。
- Manager 解析响应:
- 解码响应并返回
var_binds。
from pysnmp.hlapi import getCmd, ObjectType, ObjectIdentity
error_indication, error_status, error_index, var_binds = next(
getCmd(SnmpEngine(),
CommunityData('public'),
UdpTransportTarget(('192.168.1.1', 161)),
ContextData(),
ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0')))
- 创建
GetRequestPDU,填充 OID。
- 使用 UDP 发送 BER 编码的 SNMP 消息。
- 查找 OID 对应的值,生成
GetResponsePDU。
- 解码响应并返回
var_binds。
- 底层操作:通过连续发送
GETNEXT 请求,直到 OID 超出子树范围。
- 在
pysnmp 中的实现:
from pysnmp.hlapi import nextCmd
for (error_indication, error_status, error_index, var_binds) in nextCmd(
SnmpEngine(),
CommunityData('public'),
UdpTransportTarget(('192.168.1.1', 161)),
ContextData(),
ObjectType(ObjectIdentity('1.3.6.1.2.1.1'))):
# 处理每个 OID-Value 对
GETNEXT 请求,直到 OID 超出子树范围。pysnmp 中的实现:
from pysnmp.hlapi import nextCmd
for (error_indication, error_status, error_index, var_binds) in nextCmd(
SnmpEngine(),
CommunityData('public'),
UdpTransportTarget(('192.168.1.1', 161)),
ContextData(),
ObjectType(ObjectIdentity('1.3.6.1.2.1.1'))):
# 处理每个 OID-Value 对
- 认证(Authentication):
- 算法:MD5、SHA(
usmHMACMD5AuthProtocol、usmHMACSHAAuthProtocol)。
- 防止数据篡改。
- 加密(Privacy):
- 算法:DES、AES(
usmDESPrivProtocol、usmAesCfb128Protocol)。
- 防止数据窃听。
- 算法:MD5、SHA(
usmHMACMD5AuthProtocol、usmHMACSHAAuthProtocol)。 - 防止数据篡改。
- 算法:DES、AES(
usmDESPrivProtocol、usmAesCfb128Protocol)。 - 防止数据窃听。
from pysnmp.hlapi import UsmUserData, getCmd
error_indication, error_status, error_index, var_binds = next(
getCmd(SnmpEngine(),
UsmUserData('user1',
authKey='authkey123',
privKey='privkey123',
authProtocol=usmHMACSHAAuthProtocol,
privProtocol=usmAesCfb128Protocol),
UdpTransportTarget(('192.168.1.1', 161)),
ContextData(),
ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0')))
)
通过理解 SNMP 协议的核心概念(OID、MIB、PDU)和操作流程(GET/SET/WALK),可以更深入地掌握 pysnmp 模块的设计逻辑:
- 分层架构:分离协议处理、传输层和 MIB 管理。
- 灵活扩展:支持多版本 SNMP 和自定义传输协议。
- 协议驱动:
pysnmp的类和方法直接映射到 SNMP 消息结构。
到此这篇关于深入解析pysnmp的SNMP协议的文章就介绍到这了,更多相关pysnmp SNMP协议内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客!
您可能感兴趣的文章:
- Python pysnmp使用方法及代码实例