def fetch_mib(mib): target_directory = os.path.dirname(pysnmp_mibs.__file__) reader = HttpReader('mibs.snmplabs.com', 80, '/asn1/@mib@') mibCompiler = MibCompiler(SmiStarParser(), PySnmpCodeGen(), PyFileWriter(target_directory)) mibCompiler.addSources(reader) mibCompiler.compile(mib)
def fetch_mib(mib): import pysnmp_mibs from pysmi.codegen import PySnmpCodeGen from pysmi.compiler import MibCompiler from pysmi.parser import SmiStarParser from pysmi.reader import HttpReader from pysmi.writer import PyFileWriter target_directory = os.path.dirname(pysnmp_mibs.__file__) reader = HttpReader('mibs.snmplabs.com', 80, '/asn1/@mib@') mibCompiler = MibCompiler(SmiStarParser(), PySnmpCodeGen(), PyFileWriter(target_directory)) mibCompiler.addSources(reader) mibCompiler.compile(mib)
def fetch_mib(mib, source_url): try: from urllib.parse import urlparse except ImportError: from urlparse import urlparse import pysnmp_mibs from pysmi.codegen import PySnmpCodeGen from pysmi.compiler import MibCompiler from pysmi.parser import SmiStarParser from pysmi.reader import HttpReader from pysmi.writer import PyFileWriter target_directory = os.path.dirname(pysnmp_mibs.__file__) parsed_url = urlparse(source_url) reader = HttpReader(parsed_url.netloc, 80, parsed_url.path) mibCompiler = MibCompiler(SmiStarParser(), PySnmpCodeGen(), PyFileWriter(target_directory)) mibCompiler.addSources(reader) mibCompiler.compile(mib)
else: dstDirectory = os.path.join(dstDirectory, '.pysnmp', 'mibs') # Compiler infrastructure borrowers = [PyFileBorrower(x[1], genTexts=mibBorrowers[x[0]][1]) for x in enumerate(getReadersFromUrls(*[m[0] for m in mibBorrowers], **dict(lowcaseMatching=False)))] searchers = [PyFileSearcher(dstDirectory)] for mibSearcher in mibSearchers: searchers.append(PyPackageSearcher(mibSearcher)) searchers.append(StubSearcher(*mibStubs)) codeGenerator = PySnmpCodeGen() fileWriter = PyFileWriter(dstDirectory).setOptions(pyCompile=pyCompileFlag, pyOptimizationLevel=pyOptimizationLevel) elif dstFormat == 'json': if not mibStubs: mibStubs = JsonCodeGen.baseMibs if not mibBorrowers: mibBorrowers = [('http://mibs.snmplabs.com/json/notexts/@mib@', False), ('http://mibs.snmplabs.com/json/fulltexts/@mib@', True)] if not dstDirectory: dstDirectory = os.path.join('.')
import sys from pysmi.reader import CallbackReader from pysmi.searcher import StubSearcher from pysmi.writer import CallbackWriter from pysmi.parser import SmiV2Parser from pysmi.codegen import PySnmpCodeGen from pysmi.compiler import MibCompiler inputMibs = ['IF-MIB', 'IP-MIB'] srcDir = '/usr/share/snmp/mibs/' # we will read MIBs from here # Initialize compiler infrastructure mibCompiler = MibCompiler( SmiV2Parser(), PySnmpCodeGen(), # out own callback function stores results in its own way CallbackWriter(lambda m, d, c: sys.stdout.write(d))) # our own callback function serves as a MIB source here mibCompiler.addSources( CallbackReader(lambda m, c: open(srcDir + m + '.txt').read())) # never recompile MIBs with MACROs mibCompiler.addSearchers(StubSearcher(*PySnmpCodeGen.baseMibs)) # run non-recursive MIB compilation results = mibCompiler.compile(*inputMibs, **dict(noDeps=True)) print('Results: %s' % ', '.join(['%s:%s' % (x, results[x]) for x in results]))
from pysmi.reader import FtpReader from pysmi.reader import FileReader from pysmi.searcher import StubSearcher from pysmi.writer import PyFileWriter from pysmi.parser import SmiStarParser from pysmi.codegen import PySnmpCodeGen from pysmi.compiler import MibCompiler inputMibs = ['IF-MIB', 'BRIDGE-MIB', 'LUXL-POE-MIB', 'LUXL-SMI', 'LUXL-TC'] httpSources = [('mibs.snmplabs.com', 80, '/asn1/@mib@')] ftpSources = [('ftp.cisco.com', '/pub/mibs/v2/@mib@')] srcDirectories = [('./LUXL_MIBs_ALL')] dstDirectory = './lib/python3.7/site-packages/pysnmp/smi/mibs/' # Initialize compiler infrastructure mibCompiler = MibCompiler(SmiStarParser(), PySnmpCodeGen(), PyFileWriter(dstDirectory)) # search for source MIBs at Web and FTP sites mibCompiler.addSources(*[HttpReader(*x) for x in httpSources]) mibCompiler.addSources(*[FtpReader(*x) for x in ftpSources]) mibCompiler.addSources(*[FileReader(x) for x in srcDirectories]) # never recompile MIBs with MACROs mibCompiler.addSearchers(StubSearcher(*PySnmpCodeGen.baseMibs)) # run non-recursive MIB compilation results = mibCompiler.compile(*inputMibs, **dict(noDeps=True)) print('Results: %s' % ', '.join(['%s:%s' % (x, results[x]) for x in results]))