from pysnmp.hlapi import*# 定义参数
community ='public'# SNMPv2c 社区名
target_ip ='192.168.1.1'# 目标设备 IP
oid ='1.3.6.1.2.1.1.1.0'# 要查询的 OID# 发起 GET 请求
error_indication, error_status, error_index, var_binds =next(getCmd(SnmpEngine(),# SNMP 引擎(管理内部状态)CommunityData(community),# 认证参数(社区名)UdpTransportTarget(# 传输协议(UDP/IP)(target_ip,161), timeout=2, retries=2),ContextData(),# SNMP 上下文(通常默认)ObjectType(ObjectIdentity(oid))# 要查询的 OID))# 处理结果if error_indication:print(f"通信错误: {error_indication}")elif error_status:print(f"协议错误: {error_status} at index {error_index}")else:for oid, value in var_binds:print(f"{oid} = {value}")
1.2 核心参数解析
参数/方法
作用
SnmpEngine()
管理 SNMP 引擎的全局状态(如消息 ID、缓存等),每个会话通常一个实例。
CommunityData(community)
定义 SNMPv1/v2c 的社区名(类似密码)。
UdpTransportTarget()
指定目标设备的 IP 和端口,支持超时(timeout)和重试(retries)。
ObjectIdentity(oid)
将 OID 字符串(如 1.3.6.1.2.1.1.1.0)转换为内部对象。
getCmd()
生成 SNMP GET 请求的生成器,需用 next() 触发执行。
1.3 流程说明
构建请求:通过 getCmd() 方法生成一个 SNMP GET 请求生成器。
发送请求:调用 next() 发送请求并等待响应。
解析响应:返回的 var_binds 是一个包含 (OID, 值) 的列表。
错误处理:
error_indication: 网络或协议错误(如超时)。
error_status: SNMP 协议错误(如 noSuchName)。
2. SNMP WALK 操作详解
2.1 核心代码结构
from pysnmp.hlapi import*# 发起 WALK 请求(遍历子树)
error_indication, error_status, error_index, var_bind_table =next(nextCmd(SnmpEngine(),CommunityData('public'),UdpTransportTarget(('192.168.1.1',161)),ContextData(),ObjectType(ObjectIdentity('1.3.6.1.2.1.1')),# 遍历的起始 OIDlexicographicMode=False# 是否按字典序遍历(默认 True)))if error_indication:print(f"错误: {error_indication}")else:for var_bind_row in var_bind_table:for oid, value in var_bind_row:print(f"{oid} = {value}")
2.2 核心参数解析
参数/方法
作用
nextCmd()
生成 SNMP GETNEXT 请求的生成器,用于遍历 MIB 树。
lexicographicMode=False
控制遍历方式:False 表示仅在指定 OID 子树内遍历,避免无限循环。
2.3 流程说明
遍历机制:nextCmd() 通过连续发送 GETNEXT 请求,逐个获取下一个 OID 的值。
终止条件:当返回的 OID 超出起始 OID 的子树范围时,自动停止。
结果处理:var_bind_table 是一个二维列表,每行对应一次响应的多个变量绑定。
3. SNMP SET 操作详解
3.1 核心代码结构
from pysnmp.hlapi import*# 定义要设置的 OID 和值
oid ='1.3.6.1.2.1.1.5.0'# 系统名称的 OID
new_value = OctetString('NewRouterName')# 新值类型需与 OID 定义匹配# 发起 SET 请求
error_indication, error_status, error_index, var_binds =next(setCmd(SnmpEngine(),CommunityData('private', mpModel=1),# 使用写权限的社区名UdpTransportTarget(('192.168.1.1',161)),ContextData(),ObjectType(ObjectIdentity(oid), new_value)))if error_indication:print(f"通信错误: {error_indication}")elif error_status:print(f"SET 失败: {error_status} at index {error_index}")else:print("SET 操作成功")