pysnmp表操作

本文档详细介绍了如何使用SNMP协议来获取网络设备的数据。包括使用SNMPv3的不同安全级别进行GET请求,如无认证无隐私、SHA认证AES128隐私,以及获取特定表行和整个表的方法。示例代码展示了如何使用Python的pysnmp库进行SNMPGET、SNMPGETBULK和SNMPGETNEXT操作。
摘要由CSDN通过智能技术生成

表操作

获取表行

使用以下选项发送 SNMP GET 请求:

  • 使用 SNMPv3,用户 'usr-none-none',无身份验证,无隐私
  • 通过 IPv4/UDP
  • 到 demo.snmplabs.com:161 的代理
  • 对于 IF-MIB::ifInOctets.1 和 IF-MIB::ifOutOctets.1 MIB 对象
  • 在 MIB 执行响应 OID 和值解析

功能类似于:

$ snmpget -v3 -l authPriv -u usr-md5-des -A authkey1 -X privkey1 demo.snmplabs.com IF-MIB::ifInOctets.1 IF-MIB::ifOutOctets.1
from pysnmp.hlapi import *

iterator = getCmd(
    SnmpEngine(),
    UsmUserData('usr-none-none'),
    UdpTransportTarget(('demo.snmplabs.com', 161)),
    ContextData(),
    ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1)),
    ObjectType(ObjectIdentity('IF-MIB', 'ifOutOctets', 1))
)

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

if errorIndication:
    print(errorIndication)

elif errorStatus:
    print('%s at %s' % (errorStatus.prettyPrint(),
                        errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))

else:
    for varBind in varBinds:
        print(' = '.join([x.prettyPrint() for x in varBind]))

Download 脚本。

按复合索引获取表行

使用以下选项发送 SNMP GET 请求:

  • 使用 SNMPv3、用户 'usr-sha-aes128'、SHA 身份验证、AES128 隐私
  • 通过 IPv4/UDP
  • 到 demo.snmplabs.com:161 的代理
  • 对于 TCP-MIB::tcpConnLocalAddress.”0.0.0.0”.22.”0.0.0.0”.0 MIB 对象

功能类似于:

$ snmpget -v3 -l authPriv -u usr-md5-des -A authkey1 -X privkey1 -a SHA -x AES demo.snmplabs.com TCP-MIB::tcpConnLocalAddress.”0.0.0.0”.22.”0.0.0.0 ”.0
from pysnmp.hlapi import *

iterator = getCmd(
    SnmpEngine(),
    UsmUserData(
        'usr-sha-aes128', 'authkey1', 'privkey1',
        authProtocol=USM_AUTH_HMAC96_SHA,
        privProtocol=USM_PRIV_CFB128_AES
    ),
    UdpTransportTarget(('demo.snmplabs.com', 161)),
    ContextData(),
    ObjectType(
        ObjectIdentity(
            'TCP-MIB',
            'tcpConnLocalAddress',
            '0.0.0.0', 22,
            '0.0.0.0', 0
        )
    ).addAsn1MibSource('http://mibs.snmplabs.com/asn1/@mib@')
)

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

if errorIndication:
    print(errorIndication)

elif errorStatus:
    print('%s at %s' % (errorStatus.prettyPrint(),
                        errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))

else:
    for varBind in varBinds:
        print(' = '.join([x.prettyPrint() for x in varBind]))

Download 脚本。

获取标量和表变量

使用以下选项发送一系列 SNMP GETBULK 请求:

  • 使用带有用户“usr-md5-des”、MD5 身份验证和 DES 隐私协议的 SNMPv3
  • 通过 IPv6/UDP
  • 到 [::1]:161 的代理
  • 值非中继器 = 1,最大重复数 = 25
  • 对于 IP-MIB::ipAdEntAddr 和 IF-MIB::ifEntry 表的所有列
  • 当响应 OID 离开表的范围时停止

功能类似于:

$ snmpbulkwalk -v3 -lauthPriv -u usr-md5-des -A authkey1 -X privkey1 -Cn1, -Cr25 demo.snmplabs.com IP-MIB::ipAdEntAddr IP-MIB::ipAddrEntry
from pysnmp.hlapi import *

iterator = bulkCmd(
    SnmpEngine(),
    UsmUserData('usr-md5-des', 'authkey1', 'privkey1'),
    Udp6TransportTarget(('::1', 161)),
    ContextData(),
    1, 25,
    ObjectType(ObjectIdentity('IP-MIB', 'ipAdEntAddr')),
    ObjectType(ObjectIdentity('IP-MIB', 'ipAddrEntry')),
    lexicographicMode=False
)

for errorIndication, errorStatus, errorIndex, varBinds in iterator:

    if errorIndication:
        print(errorIndication)
        break

    elif errorStatus:
        print('%s at %s' % (errorStatus.prettyPrint(),
                            errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
        break

    else:
        for varBind in varBinds:
            print(' = '.join([x.prettyPrint() for x in varBind]))

Download 脚本。

获取整个 SNMP 表

使用以下选项发送一系列 SNMP GETNEXT 请求:

  • 使用 SNMPv1,社区“公共”
  • 通过 IPv4/UDP
  • 到 demo.snmplabs.com:161 的代理
  • 对于 IF-MIB::ifEntry 表的某些列
  • 当响应 OID 离开初始 OID 的范围时停止

功能类似于:

$ snmpwalk -v1 -c public demo.snmplabs.com IF-MIB::ifDescr IF-MIB::ifType IF-MIB::ifMtu IF-MIB::ifSpeed IF-MIB::ifPhysAddress IF-MIB::ifType
from pysnmp.hlapi import *

iterator = nextCmd(
    SnmpEngine(),
    CommunityData('public', mpModel=0),
    UdpTransportTarget(('demo.snmplabs.com', 161)),
    ContextData(),
    ObjectType(ObjectIdentity('IF-MIB', 'ifDescr')),
    ObjectType(ObjectIdentity('IF-MIB', 'ifType')),
    ObjectType(ObjectIdentity('IF-MIB', 'ifMtu')),
    ObjectType(ObjectIdentity('IF-MIB', 'ifSpeed')),
    ObjectType(ObjectIdentity('IF-MIB', 'ifPhysAddress')),
    ObjectType(ObjectIdentity('IF-MIB', 'ifType')),
    lexicographicMode=False
)

for errorIndication, errorStatus, errorIndex, varBinds in iterator:

    if errorIndication:
        print(errorIndication)
        break

    elif errorStatus:
        print('%s at %s' % (errorStatus.prettyPrint(),
                            errorIndex and varBinds[int(errorIndex)-1][0] or '?'))
        break

    else:
        for varBind in varBinds:
            print(' = '.join([x.prettyPrint() for x in varBind]))

Download 脚本。

另请参阅:库参考

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值