etingof / pysnmp

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

How to encode MibTables using pyasn1 codec encoders #138

Closed luizfschrickte closed 6 years ago

luizfschrickte commented 6 years ago

Hi!

I am developing code for a NTCIP agent, which is basically SNMP with additional layers. I already have created the object instances (most part grouped on tables) successfully - but as a requirement I need to encode some of these tables using BER or OER and store in files. Well, the question is:

Considering that pysnmp apparently is built on top of pyasn1 structure, is there a way to (BER) encode a MibTable directly as a sequence? I am stuck trying to do this in an easy way. but it looks like MibTable's are defined as NoneType objects and have no direct links to its rows, so no way for the encoder to know how to navigate through them.

So, I assume the only way to do this is to create manually a SEQUENCE OF object using pyasn1 and add entries by manually navigating through all rows? I mean, for each table I will need to define a pyasn1 structure in order to accomplish this?

Any guidance here will help - I am searching for examples but it looks like no one is interested in encoding tables! I could already encode MibScalarInstance's and it is OK, but when talking about grouping/sequence objects I can't see a way out.

Thank you very much Luiz Fernando

etingof commented 6 years ago

Well, everything in SNMP is ultimately encoded into BER... Including SNMP tables, they are encoded as a sequence of variable-bindings ordered on the column basis.

If you want to serialize anything with BER, you need to express it as ASN.1 data structure first. SNMP tables are "imaginary", they are just ordered variable-bindings. In the standard they call these tables as "conceptual" probably referring to the fact that there is no solid 2-D data structure behind them.

Once you have your ASN.1 data structure in place, you can of course BER-encode it with pyasn1.

I think you need to explain (or figure out) how exactly do you want SNMP tables to be BER-serialized.

luizfschrickte commented 6 years ago

Thank you!

The 'imaginary' is the important concept I wasn't to sure. While waiting for the answer I have already create a StaticTable class to group the table OIDs and also map data on ASN.1 structures. Works like a charm. Everything is working fine now! Thank you very much!