2

Using snmpwalk I can get this from my device:

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

I tried this program in Python to get the value field from above OID:

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

Output i get:

When i run the program, it shows a long list of OID's (don't know why even I am giving the leaf level OID as input in program) with values. STRANGE.

What is tried

lexicographicMode=True in the nextCmd but it doesn't show anything than.

What i wish

I want to give a list of OID in my program and wants their values(value is a key you can see in first line), that's it.

Request

Please help me in python program to do so using pysnmp.

Community
  • 1
  • 1
Nikhil
  • 417
  • 1
  • 6
  • 16

1 Answers1

4

If you want OIDs, use mibLookup=False parameter. If you want just the branch of the MIB, use lexicographicMode=False, but make sure to specify non-leaf OID because in that case you will get nothing in return.

Here's your script with the suggested changes:

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

You should be able to cut&paste it, it's running against public SNMP simulator at demo.snmplabs.com.

Ilya Etingof
  • 5,440
  • 1
  • 17
  • 21
  • Thanks @llya, similarly i have created 2 more programs, grt. usiing wireshark i can see a request-id goes on when i run this program, can i get the request id number using python program similarly, when i run this program i basically give community:public and version v2c, but in get-response i get request-id, this is what is need to fetch. Please help me out how to do it. Here is the image of snmp response in wireshark. https://i.imgur.com/aH5gO4B.png – Nikhil Mar 12 '18 at 18:44