繁体   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