简体   繁体   English

使用pysnmp获取单个SNMP值

[英]Get a single SNMP value with pysnmp

I'm trying to get the value of a single OID into a variable. 我正在尝试将单个OID的值转换为变量。 However, I only am able to walk it using pysnmp . 但是,我只能使用pysnmp I want to get the value of OID 1.3.6.1.4.1.2.3.51.2.2.7.1.0 which returns value 255 if I test it using an SNMP tool. 我想获取OID 1.3.6.1.4.1.2.3.51.2.2.7.1.0的值,如果我使用SNMP工具对其进行测试,它将返回值255。

Using this code I don't get any output: 使用此代码,我没有任何输出:

def getsnmp(host, oid):

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

        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]))

getsnmp('10.100.11.30', '1.3.6.1.4.1.2.3.51.2.2.7.1.0')

However, If I remove the last .0, I get as a result: 但是,如果删除最后一个.0,则会得到以下结果:

1.3.6.1.4.1.2.3.51.2.2.7.1.0 = 255 1.3.6.1.4.1.2.3.51.2.2.7.1.0 = 255

How do I access the specific OID directly without a walk? 我如何无需步行即可直接访问特定的OID?

Thanks! 谢谢!

The nextCmd returns the next OID relative to the given one. nextCmd返回相对于给定的下一个 OID。 It never returns the value for the given OID. 它永远不会返回给定OID的值。

If you need to query specific OID you should use getCmd function instead of nextCmd . 如果需要查询特定的OID,则应使用getCmd函数而不是nextCmd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM