How to get the value of OID in Python using PySnmp

夙愿已清 提交于 2019-12-11 14:56:41

问题


Using snmpwalk I can get this from my device:

OID=.1.3.6.1.4.1.5296.1.9.1.1.1.7.115.101.99.99.97.57.27.1.41
Type=OctetString
Value=secca99

I tried this program in Python to get the value field from above OID:

#!/usr/bin/env python3

from pysnmp.hlapi import *
import sys

def walk(host, oid):

    for (errorIndication,
         errorStatus,
         errorIndex,
         varBinds) in nextCmd(SnmpEngine(),
                              CommunityData('public'),
                              UdpTransportTarget((host, 161)),
                              ContextData(),
                              ObjectType(ObjectIdentity(oid))):

        if errorIndication:
            print(errorIndication, file=sys.stderr)
            break

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

        else:
            for varBind in varBinds:
                print(varBind)


walk('10.78.163.39',
     '.1.3.6.1.4.1.5296.1.9.1.1.1.7.115.101.99.99.97.57.27.1.41')

Output i get:

When i run the program, it shows a long list of OID's (don't know why even I am giving the leaf level OID as input in program) with values. STRANGE.

What is tried

lexicographicMode=True in the nextCmd but it doesn't show anything than.

What i wish

I want to give a list of OID in my program and wants their values(value is a key you can see in first line), that's it.

Request

Please help me in python program to do so using pysnmp.


回答1:


If you want OIDs, use mibLookup=False parameter. If you want just the branch of the MIB, use lexicographicMode=False, but make sure to specify non-leaf OID because in that case you will get nothing in return.

Here's your script with the suggested changes:

from pysnmp.hlapi import *
import sys

def walk(host, oid):

    for (errorIndication,
         errorStatus,
         errorIndex,
         varBinds) in nextCmd(SnmpEngine(),
                              CommunityData('public'),
                              UdpTransportTarget((host, 161)),
                              ContextData(),
                              ObjectType(ObjectIdentity(oid)),
                              lookupMib=False,
                              lexicographicMode=False):

        if errorIndication:
            print(errorIndication, file=sys.stderr)
            break

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

        else:
            for varBind in varBinds:
                 print('%s = %s' % varBind)

walk('demo.snmplabs.com', '1.3.6.1.2.1.1.9.1.2')

You should be able to cut&paste it, it's running against public SNMP simulator at demo.snmplabs.com.



来源:https://stackoverflow.com/questions/49137695/how-to-get-the-value-of-oid-in-python-using-pysnmp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!