简体   繁体   中英

Get specific OID from SNMP

I would like to get the sysUpTime from SNMP

It's my first time working on this so i am not sure how to make it work. I downloaded the MIB Browser app and i get this:

在此处输入图像描述

How can i do it in python way? I am trying this code

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

But i get an error

No SNMP response received before timeout

You should follow the standard tutorial and issue a GET request,

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 are completely different operations that usually are used to query a table or discover objects. Don't misuse them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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