Search code examples
python-3.xpysnmp

getting output using pysnmp


Forgive me if this is such a simple thing but I am new to Python, pysnmp and SNMP.

I am trying to run some very simple queries using SNMP to get configuration information from a device and for some reason following the documentation.

I am not getting any output even though I can walk the SNMP via snmpwalk and googling seems to just show the example I have below.

my code is

#!/usr/bin/python3.5

from pysnmp.hlapi import *

varCommunity = "public"
varServer = "demo.pysnmp.com"
varPort = 161

g = getCmd(SnmpEngine(),
       CommunityData(varCommunity),
       UdpTransportTarget((varServer, varPort)),
       ContextData(),
       ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0)))

next(g)

If I add

print(g)

I would get the following output

<generator object getCmd at 0x7f25964847d8>

Solution

  • next(g)
    

    Will return the next value from the generator. If you were typing this code in the Python console, you'd see the actual result. However, since you're running this from a file, the result will just be discarded.

    You need to put the print around it. E.g.

    print(next(g))
    

    For easier debugging, you could get the list of all results like this:

    print(list(g))