簡體   English   中英

如何使用PySnmp在Python中獲取OID的值

[英]How to get the value of OID in Python using PySnmp

使用snmpwalk我可以從設備中獲取此信息:

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

我在Python中嘗試了該程序,以從OID上方獲取value字段:

#!/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')

輸出我得到:

當我運行程序時,它會顯示一長串帶有值的OID(甚至不知道為什么我還給葉級OID作為程序中的輸入)。 奇怪。

嘗試了什么

lexicographicMode=TruenextCmd但沒有顯示任何內容。

我希望

我想在程序中提供OID列表,並希望它們的值(值是您可以在第一行中看到的鍵),僅此而已。

請求

請使用pysnmp在python程序中幫助我。

如果需要OID,請使用mibLookup = False參數。 如果只需要MIB的分支,請使用lexicographicMode=False ,但是請確保指定非葉子OID,因為在這種情況下,您將一無所獲。

這是您的腳本,其中包含建議的更改:

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

您應該能夠剪切並粘貼它,它可以在demo.snmplabs.com上針對公共SNMP仿真器運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM