簡體   English   中英

從 SNMP 獲取特定的 OID

[英]Get specific OID from SNMP

我想從 SNMP 獲取 sysUpTime

這是我第一次從事這項工作,所以我不確定如何讓它發揮作用。 我下載了MIB 瀏覽器應用程序,我得到了這個:

在此處輸入圖像描述

我怎樣才能以 python 的方式做到這一點? 我正在嘗試這段代碼

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('192.188.14.126', '1.3.6.1.2.1.1.3.0')

但我得到一個錯誤

超時前未收到 SNMP 響應

您應該遵循標准教程並發出 GET 請求,

https://www.pysnmp.com/pysnmp/quick-start#fetch-snmp-variable

import asyncio
from pysnmp.hlapi.asyncio import *


@asyncio.coroutine
def run():
    snmpEngine = SnmpEngine()
    errorIndication, errorStatus, errorIndex, varBinds = yield from getCmd(
        snmpEngine,
        CommunityData('public', mpModel=0),
        UdpTransportTarget(('192.188.14.126', 161)),
        ContextData(),
        ObjectType(ObjectIdentity('1.3.6.1.2.1.1.3.0'))
    )

    if errorIndication:
        print(errorIndication)
    elif errorStatus:
        print('{} at {}'.format(
            errorStatus.prettyPrint(),
            errorIndex and varBinds[int(errorIndex) - 1][0] or '?'
        )
              )
    else:
        for varBind in varBinds:
            print(' = '.join([x.prettyPrint() for x in varBind]))

    snmpEngine.transportDispatcher.closeDispatcher()


asyncio.get_event_loop().run_until_complete(run())

WALK/GET-NEXT/GET-BULK 是完全不同的操作,通常用於查詢表或發現對象。 不要濫用它們。

暫無
暫無

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

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