etingof / pysnmp

Python SNMP library
http://snmplabs.com/pysnmp/
BSD 2-Clause "Simplified" License
583 stars 201 forks source link

What's wrong with pySNMP.... #264

Open eLvErDe opened 5 years ago

eLvErDe commented 5 years ago

Hello,

I'm sorry, I'm probably dump but I *never" managed to get pySNMP working.

I started making a class to implement a protocol SNMP-based but getSnmp *always" says there's no error but returns and empty payload.

So I ended up trying something really minimal, based on an example, and it still does not work:

#!/usr/bin/python3

from pysnmp.hlapi import getCmd
from pysnmp.hlapi import ObjectType
from pysnmp.hlapi import ObjectIdentity
from pysnmp.hlapi import UdpTransportTarget
from pysnmp.hlapi import CommunityData
from pysnmp.hlapi import SnmpEngine

single_oid = ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0'))
community = "public"
hostname = "10.13.92.11"
port = 6001
errorIndication, errorStatus, errorIndex, varBinds = next(getCmd(
    SnmpEngine(),
    CommunityData(community, mpModel=0),
    UdpTransportTarget((hostname, port)),
    single_oid,
    lookupMib=False
))
print(errorIndication)
print(errorStatus)
print(errorIndex)
print(varBinds)

Returns

None
None
None
[]

While it is working perfeclty fine using command line:

snmpget -v 1 -c public 10.13.92.11:6001 1.3.6.1.2.1.1.1.0

iso.3.6.1.2.1.1.1.0 = STRING: "Linux SCU3V3 2.6.30_scu6V3 #37 PREEMPT Thu Sep 22 08:40:25 CEST 2016 armv5tejl"

Thanks in advance for your help,

Adam.

etingof commented 5 years ago

This is curious... Let's add this at the beginning of the script and give it another run:

from pysnmp import debug

debug.setLogger(debug.Debug('msgproc', 'secmod'))
eLvErDe commented 5 years ago

Hi,

Seems to be somehow related to ContextData(), I added this to the call and now I get a value. So after that I ported this code snippet back to my class and modified it step by step and finally I'm able to extract the required value but this is very strange because I had a ContextData instance passed to getCmd ?!?!

etingof commented 5 years ago

Ah, indeed, that's the missing ContextData parameter!

Somehow your only OID got consumed as ContextData (which is ignored if SNMPv1/v2c is in use). Therefore no OIDs remain in the getCmd call. Thus pysnmp returns empty response.

eLvErDe commented 5 years ago

Okay well, that's a bit weird. Are you sure it's a normal behavior ? I mean, it's really hard to understand what's going on...

etingof commented 5 years ago

Okay well, that's a bit weird. Are you sure it's a normal behavior ? I mean, it's really hard to understand what's going on...

Absolutely, I will push a PR to assert ContextData parameter type.

lextm commented 1 year ago

The result of

None
None
None
[]

was caused by a varBinds check,

https://github.com/etingof/pysnmp/blob/v4.4.12/pysnmp/hlapi/asyncore/sync/cmdgen.py#L119

While it is easy to say "to assert ContextData parameter type", the assertion has to be added to multiple locations, which is less ideal. Besides, async operations are verified differently.

Thus, probably leave it as it is, because most people copy the official examples to get started and they shouldn't experience such a missing argument.