In [57]:
!pip3 install pysmi
In [58]:
from pysmi.reader import FileReader, HttpReader
reader = FileReader('/tmp/SNMP-MIBs', recursive=True, ignoreErrors=True)
In [59]:
from pysmi.parser.dialect import smiV1Relaxed
from pysmi.parser.smi import parserFactory
Parser = parserFactory(**smiV1Relaxed)
parser = Parser()
The SmiStarParser() is a convenient short-cut for the same thing.
In [60]:
from pysmi.parser import SmiStarParser
parser = SmiStarParser()
In [61]:
from pysmi.codegen import JsonCodeGen
codegen = JsonCodeGen()
The Writer gets the output from the Generator. The FileWriter(path) can be used to save the output to a file inside of path. Alternatively the CallbackWriter() can be used for further processing of the output. The code below shows how it works although in this example it operates similar to the FileWriter().
In [62]:
from pysmi.writer import CallbackWriter
def callback(mibname, doc, context):
with open('/tmp/SNMP-MIBs-JSON/{}.json'.format(mibname), 'wt') as fp:
fp.write(doc)
writer = CallbackWriter(callback)
In [63]:
from pysmi.compiler import MibCompiler
compiler = MibCompiler(parser, codegen, writer)
The reader is added as source to the Compiler.
In [64]:
compiler.addSources(reader)
Out[64]:
Finally the Compiler can be "run" against some MIB names(!).
In [65]:
status = compiler.compile('IF-MIB', 'VRRP-MIB')
The status shows that the imported MIBs were also compiled.
In [66]:
status
Out[66]:
The Writer created the JSON files.
In [67]:
!ls /tmp/SNMP-MIBs-JSON
In [68]:
!head /tmp/SNMP-MIBs-JSON/IF-MIB.json
The status will also show if a MIB could not be processed.
In [69]:
status = compiler.compile('MISSING-MIB')
status
Out[69]: