charlestolley / python-snmp

A user-friendly SNMP library
MIT License
15 stars 3 forks source link

Bad Community string handling in v2c? and 'get' Timeout #5

Closed perl5punk closed 1 year ago

perl5punk commented 1 year ago

On Windows with Python 3.10

Having a couple of issues, first when I use the v2c "Getting Started" it raises an exception in ber.py, line 273

    return encode_identifier(identifier) + encode_length(len(data)) + data
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
TypeError: can't concat str to bytes

if I change it to a byte string then it just times out

engine = Engine(SNMPv2c, defaultCommunity=config.SNMP_DEFAULT_COMMUNITY.encode('ascii'))
host = engine.Manager(ip_address)
snmp_get = host.get(oid, community=b"default") # added community here to test an invalid one

Exception

.... v2c.py, line 98, in wait
    raise Timeout()
snmp.manager.Timeout

I added a debug line to see how it's encoding the string and this is what I get

b'0(\x02\x01\x01\x04\x07default\xa0\x1a\x02\x04\x95\xfa\xec%\x02\x01\x00\x02\x01\x000\x0c0\n\x06\x06+\x06\x01\x02\x01\x01\x05\x00'

I've tried a valid community string and an invalid one and they both timeout, which tells me it's possibly an issue with the community string but I'm not 100%, might be something really dumb I missed.

Please advise. Thank you.

charlestolley commented 1 year ago

which version of the library are you using? If you installed with pip, you can use pip show snmp to check the version

perl5punk commented 1 year ago

Whoops, forgot that.. 0.4.2

charlestolley commented 1 year ago

It looks like the encoding of your request is valid, though I will point out that you used a GET request with the OID "1.3.6.1.2.1.1", which is not a valid "instance identifier" (see this SO answer if you don't know what that means (starting with the 3rd paragraph "Finally..."): https://stackoverflow.com/a/75100658/6284025). I would recommend trying a GET-NEXT. Depending on the implementation of the agent you are sending the request to, this may be the issue, or there may be something else wrong. What type of machine is on the other end of your request?

perl5punk commented 1 year ago

It was a completely valid timeout, it turns out the network was blocking 161 in certain conditions. Thanks for the quick response!