Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/imh-pyth.../bin
File: mibdump.py
#!/opt/imh-python/bin/python3.9
[0] Fix | Delete
#
[1] Fix | Delete
# This file is part of pysmi software.
[2] Fix | Delete
#
[3] Fix | Delete
# Copyright (c) 2015-2019, Ilya Etingof <etingof@gmail.com>
[4] Fix | Delete
# License: http://snmplabs.com/pysmi/license.html
[5] Fix | Delete
#
[6] Fix | Delete
# SNMP SMI/MIB data management tool
[7] Fix | Delete
#
[8] Fix | Delete
import os
[9] Fix | Delete
import sys
[10] Fix | Delete
import getopt
[11] Fix | Delete
from pysmi.reader import getReadersFromUrls
[12] Fix | Delete
from pysmi.searcher import AnyFileSearcher, PyFileSearcher, PyPackageSearcher, StubSearcher
[13] Fix | Delete
from pysmi.borrower import AnyFileBorrower, PyFileBorrower
[14] Fix | Delete
from pysmi.writer import PyFileWriter, FileWriter, CallbackWriter
[15] Fix | Delete
from pysmi.parser import SmiV1CompatParser
[16] Fix | Delete
from pysmi.codegen import PySnmpCodeGen, JsonCodeGen, NullCodeGen
[17] Fix | Delete
from pysmi.compiler import MibCompiler
[18] Fix | Delete
from pysmi import debug
[19] Fix | Delete
from pysmi import error
[20] Fix | Delete
[21] Fix | Delete
# sysexits.h
[22] Fix | Delete
EX_OK = 0
[23] Fix | Delete
EX_USAGE = 64
[24] Fix | Delete
EX_SOFTWARE = 70
[25] Fix | Delete
EX_MIB_MISSING = 79
[26] Fix | Delete
EX_MIB_FAILED = 79
[27] Fix | Delete
[28] Fix | Delete
# Defaults
[29] Fix | Delete
verboseFlag = True
[30] Fix | Delete
mibSources = []
[31] Fix | Delete
doFuzzyMatchingFlag = True
[32] Fix | Delete
mibSearchers = []
[33] Fix | Delete
mibStubs = []
[34] Fix | Delete
mibBorrowers = []
[35] Fix | Delete
dstFormat = None
[36] Fix | Delete
dstDirectory = None
[37] Fix | Delete
cacheDirectory = ''
[38] Fix | Delete
nodepsFlag = False
[39] Fix | Delete
rebuildFlag = False
[40] Fix | Delete
dryrunFlag = False
[41] Fix | Delete
genMibTextsFlag = False
[42] Fix | Delete
keepTextsLayout = False
[43] Fix | Delete
pyCompileFlag = True
[44] Fix | Delete
pyOptimizationLevel = 0
[45] Fix | Delete
ignoreErrorsFlag = False
[46] Fix | Delete
buildIndexFlag = False
[47] Fix | Delete
writeMibsFlag = True
[48] Fix | Delete
[49] Fix | Delete
helpMessage = """\
[50] Fix | Delete
Usage: %s [--help]
[51] Fix | Delete
[--version]
[52] Fix | Delete
[--quiet]
[53] Fix | Delete
[--debug=<%s>]
[54] Fix | Delete
[--mib-source=<URI>]
[55] Fix | Delete
[--mib-searcher=<PATH|PACKAGE>]
[56] Fix | Delete
[--mib-stub=<MIB-NAME>]
[57] Fix | Delete
[--mib-borrower=<PATH>]
[58] Fix | Delete
[--destination-format=<FORMAT>]
[59] Fix | Delete
[--destination-directory=<DIRECTORY>]
[60] Fix | Delete
[--cache-directory=<DIRECTORY>]
[61] Fix | Delete
[--disable-fuzzy-source]
[62] Fix | Delete
[--no-dependencies]
[63] Fix | Delete
[--no-python-compile]
[64] Fix | Delete
[--python-optimization-level]
[65] Fix | Delete
[--ignore-errors]
[66] Fix | Delete
[--build-index]
[67] Fix | Delete
[--rebuild]
[68] Fix | Delete
[--dry-run]
[69] Fix | Delete
[--no-mib-writes]
[70] Fix | Delete
[--generate-mib-texts]
[71] Fix | Delete
[--keep-texts-layout]
[72] Fix | Delete
<MIB-NAME> [MIB-NAME [...]]]
[73] Fix | Delete
Where:
[74] Fix | Delete
URI - file, zip, http, https, ftp, sftp schemes are supported.
[75] Fix | Delete
Use @mib@ placeholder token in URI to refer directly to
[76] Fix | Delete
the required MIB module when source does not support
[77] Fix | Delete
directory listing (e.g. HTTP).
[78] Fix | Delete
FORMAT - pysnmp, json, null""" % (
[79] Fix | Delete
sys.argv[0],
[80] Fix | Delete
'|'.join([x for x in sorted(debug.flagMap)])
[81] Fix | Delete
)
[82] Fix | Delete
[83] Fix | Delete
try:
[84] Fix | Delete
opts, inputMibs = getopt.getopt(
[85] Fix | Delete
sys.argv[1:], 'hv',
[86] Fix | Delete
['help', 'version', 'quiet', 'debug=',
[87] Fix | Delete
'mib-source=', 'mib-searcher=', 'mib-stub=', 'mib-borrower=',
[88] Fix | Delete
'destination-format=', 'destination-directory=', 'cache-directory=',
[89] Fix | Delete
'no-dependencies', 'no-python-compile', 'python-optimization-level=',
[90] Fix | Delete
'ignore-errors', 'build-index', 'rebuild', 'dry-run', 'no-mib-writes',
[91] Fix | Delete
'generate-mib-texts', 'disable-fuzzy-source', 'keep-texts-layout']
[92] Fix | Delete
)
[93] Fix | Delete
[94] Fix | Delete
except getopt.GetoptError:
[95] Fix | Delete
if verboseFlag:
[96] Fix | Delete
sys.stderr.write('ERROR: %s\r\n%s\r\n' % (sys.exc_info()[1], helpMessage))
[97] Fix | Delete
[98] Fix | Delete
sys.exit(EX_USAGE)
[99] Fix | Delete
[100] Fix | Delete
for opt in opts:
[101] Fix | Delete
if opt[0] == '-h' or opt[0] == '--help':
[102] Fix | Delete
sys.stderr.write("""\
[103] Fix | Delete
Synopsis:
[104] Fix | Delete
SNMP SMI/MIB files conversion tool
[105] Fix | Delete
Documentation:
[106] Fix | Delete
http://snmplabs.com/pysmi
[107] Fix | Delete
%s
[108] Fix | Delete
""" % helpMessage)
[109] Fix | Delete
sys.exit(EX_OK)
[110] Fix | Delete
[111] Fix | Delete
if opt[0] == '-v' or opt[0] == '--version':
[112] Fix | Delete
from pysmi import __version__
[113] Fix | Delete
[114] Fix | Delete
sys.stderr.write("""\
[115] Fix | Delete
SNMP SMI/MIB library version %s, written by Ilya Etingof <etingof@gmail.com>
[116] Fix | Delete
Python interpreter: %s
[117] Fix | Delete
Software documentation and support at http://snmplabs.com/pysmi
[118] Fix | Delete
%s
[119] Fix | Delete
""" % (__version__, sys.version, helpMessage))
[120] Fix | Delete
sys.exit(EX_OK)
[121] Fix | Delete
[122] Fix | Delete
if opt[0] == '--quiet':
[123] Fix | Delete
verboseFlag = False
[124] Fix | Delete
[125] Fix | Delete
if opt[0] == '--debug':
[126] Fix | Delete
debug.setLogger(debug.Debug(*opt[1].split(',')))
[127] Fix | Delete
[128] Fix | Delete
if opt[0] == '--mib-source':
[129] Fix | Delete
mibSources.append(opt[1])
[130] Fix | Delete
[131] Fix | Delete
if opt[0] == '--mib-searcher':
[132] Fix | Delete
mibSearchers.append(opt[1])
[133] Fix | Delete
[134] Fix | Delete
if opt[0] == '--mib-stub':
[135] Fix | Delete
mibStubs.append(opt[1])
[136] Fix | Delete
[137] Fix | Delete
if opt[0] == '--mib-borrower':
[138] Fix | Delete
mibBorrowers.append((opt[1], genMibTextsFlag))
[139] Fix | Delete
[140] Fix | Delete
if opt[0] == '--destination-format':
[141] Fix | Delete
dstFormat = opt[1]
[142] Fix | Delete
[143] Fix | Delete
if opt[0] == '--destination-directory':
[144] Fix | Delete
dstDirectory = opt[1]
[145] Fix | Delete
[146] Fix | Delete
if opt[0] == '--cache-directory':
[147] Fix | Delete
cacheDirectory = opt[1]
[148] Fix | Delete
[149] Fix | Delete
if opt[0] == '--no-dependencies':
[150] Fix | Delete
nodepsFlag = True
[151] Fix | Delete
[152] Fix | Delete
if opt[0] == '--no-python-compile':
[153] Fix | Delete
pyCompileFlag = False
[154] Fix | Delete
[155] Fix | Delete
if opt[0] == '--python-optimization-level':
[156] Fix | Delete
try:
[157] Fix | Delete
pyOptimizationLevel = int(opt[1])
[158] Fix | Delete
[159] Fix | Delete
except ValueError:
[160] Fix | Delete
sys.stderr.write('ERROR: known Python optimization levels: -1, 0, 1, 2\r\n%s\r\n' % helpMessage)
[161] Fix | Delete
sys.exit(EX_USAGE)
[162] Fix | Delete
[163] Fix | Delete
if opt[0] == '--ignore-errors':
[164] Fix | Delete
ignoreErrorsFlag = True
[165] Fix | Delete
[166] Fix | Delete
if opt[0] == '--build-index':
[167] Fix | Delete
buildIndexFlag = True
[168] Fix | Delete
[169] Fix | Delete
if opt[0] == '--rebuild':
[170] Fix | Delete
rebuildFlag = True
[171] Fix | Delete
[172] Fix | Delete
if opt[0] == '--dry-run':
[173] Fix | Delete
dryrunFlag = True
[174] Fix | Delete
[175] Fix | Delete
if opt[0] == '--no-mib-writes':
[176] Fix | Delete
writeMibsFlag = False
[177] Fix | Delete
[178] Fix | Delete
if opt[0] == '--generate-mib-texts':
[179] Fix | Delete
genMibTextsFlag = True
[180] Fix | Delete
[181] Fix | Delete
if opt[0] == '--disable-fuzzy-source':
[182] Fix | Delete
doFuzzyMatchingFlag = False
[183] Fix | Delete
[184] Fix | Delete
if opt[0] == '--keep-texts-layout':
[185] Fix | Delete
keepTextsLayout = True
[186] Fix | Delete
[187] Fix | Delete
if not mibSources:
[188] Fix | Delete
mibSources = ['file:///usr/share/snmp/mibs',
[189] Fix | Delete
'http://mibs.snmplabs.com/asn1/@mib@']
[190] Fix | Delete
[191] Fix | Delete
if inputMibs:
[192] Fix | Delete
mibSources = sorted(
[193] Fix | Delete
set([os.path.abspath(os.path.dirname(x))
[194] Fix | Delete
for x in inputMibs
[195] Fix | Delete
if os.path.sep in x])
[196] Fix | Delete
) + mibSources
[197] Fix | Delete
[198] Fix | Delete
inputMibs = [os.path.basename(os.path.splitext(x)[0]) for x in inputMibs]
[199] Fix | Delete
[200] Fix | Delete
if not inputMibs:
[201] Fix | Delete
sys.stderr.write('ERROR: MIB modules names not specified\r\n%s\r\n' % helpMessage)
[202] Fix | Delete
sys.exit(EX_USAGE)
[203] Fix | Delete
[204] Fix | Delete
if not dstFormat:
[205] Fix | Delete
dstFormat = 'pysnmp'
[206] Fix | Delete
[207] Fix | Delete
if dstFormat == 'pysnmp':
[208] Fix | Delete
if not mibSearchers:
[209] Fix | Delete
mibSearchers = PySnmpCodeGen.defaultMibPackages
[210] Fix | Delete
[211] Fix | Delete
if not mibStubs:
[212] Fix | Delete
mibStubs = [x for x in PySnmpCodeGen.baseMibs if x not in PySnmpCodeGen.fakeMibs]
[213] Fix | Delete
[214] Fix | Delete
if not mibBorrowers:
[215] Fix | Delete
mibBorrowers = [('http://mibs.snmplabs.com/pysnmp/notexts/@mib@', False),
[216] Fix | Delete
('http://mibs.snmplabs.com/pysnmp/fulltexts/@mib@', True)]
[217] Fix | Delete
[218] Fix | Delete
if not dstDirectory:
[219] Fix | Delete
dstDirectory = os.path.expanduser("~")
[220] Fix | Delete
if sys.platform[:3] == 'win':
[221] Fix | Delete
dstDirectory = os.path.join(dstDirectory, 'PySNMP Configuration', 'mibs')
[222] Fix | Delete
else:
[223] Fix | Delete
dstDirectory = os.path.join(dstDirectory, '.pysnmp', 'mibs')
[224] Fix | Delete
[225] Fix | Delete
# Compiler infrastructure
[226] Fix | Delete
[227] Fix | Delete
borrowers = [PyFileBorrower(x[1], genTexts=mibBorrowers[x[0]][1])
[228] Fix | Delete
for x in enumerate(getReadersFromUrls(*[m[0] for m in mibBorrowers], **dict(lowcaseMatching=False)))]
[229] Fix | Delete
[230] Fix | Delete
searchers = [PyFileSearcher(dstDirectory)]
[231] Fix | Delete
[232] Fix | Delete
for mibSearcher in mibSearchers:
[233] Fix | Delete
searchers.append(PyPackageSearcher(mibSearcher))
[234] Fix | Delete
[235] Fix | Delete
searchers.append(StubSearcher(*mibStubs))
[236] Fix | Delete
[237] Fix | Delete
codeGenerator = PySnmpCodeGen()
[238] Fix | Delete
[239] Fix | Delete
fileWriter = PyFileWriter(dstDirectory).setOptions(pyCompile=pyCompileFlag,
[240] Fix | Delete
pyOptimizationLevel=pyOptimizationLevel)
[241] Fix | Delete
[242] Fix | Delete
elif dstFormat == 'json':
[243] Fix | Delete
if not mibStubs:
[244] Fix | Delete
mibStubs = JsonCodeGen.baseMibs
[245] Fix | Delete
[246] Fix | Delete
if not mibBorrowers:
[247] Fix | Delete
mibBorrowers = [('http://mibs.snmplabs.com/json/notexts/@mib@', False),
[248] Fix | Delete
('http://mibs.snmplabs.com/json/fulltexts/@mib@', True)]
[249] Fix | Delete
[250] Fix | Delete
if not dstDirectory:
[251] Fix | Delete
dstDirectory = os.path.join('.')
[252] Fix | Delete
[253] Fix | Delete
# Compiler infrastructure
[254] Fix | Delete
[255] Fix | Delete
borrowers = [AnyFileBorrower(x[1], genTexts=mibBorrowers[x[0]][1]).setOptions(exts=['.json'])
[256] Fix | Delete
for x in enumerate(getReadersFromUrls(*[m[0] for m in mibBorrowers], **dict(lowcaseMatching=False)))]
[257] Fix | Delete
[258] Fix | Delete
searchers = [AnyFileSearcher(dstDirectory).setOptions(exts=['.json']), StubSearcher(*mibStubs)]
[259] Fix | Delete
[260] Fix | Delete
codeGenerator = JsonCodeGen()
[261] Fix | Delete
[262] Fix | Delete
fileWriter = FileWriter(dstDirectory).setOptions(suffix='.json')
[263] Fix | Delete
[264] Fix | Delete
elif dstFormat == 'null':
[265] Fix | Delete
if not mibStubs:
[266] Fix | Delete
mibStubs = NullCodeGen.baseMibs
[267] Fix | Delete
[268] Fix | Delete
if not mibBorrowers:
[269] Fix | Delete
mibBorrowers = [('http://mibs.snmplabs.com/null/notexts/@mib@', False),
[270] Fix | Delete
('http://mibs.snmplabs.com/null/fulltexts/@mib@', True)]
[271] Fix | Delete
[272] Fix | Delete
if not dstDirectory:
[273] Fix | Delete
dstDirectory = ''
[274] Fix | Delete
[275] Fix | Delete
# Compiler infrastructure
[276] Fix | Delete
[277] Fix | Delete
codeGenerator = NullCodeGen()
[278] Fix | Delete
[279] Fix | Delete
searchers = [StubSearcher(*mibStubs)]
[280] Fix | Delete
[281] Fix | Delete
borrowers = [AnyFileBorrower(x[1], genTexts=mibBorrowers[x[0]][1])
[282] Fix | Delete
for x in enumerate(getReadersFromUrls(*[m[0] for m in mibBorrowers], **dict(lowcaseMatching=False)))]
[283] Fix | Delete
[284] Fix | Delete
fileWriter = CallbackWriter(lambda *x: None)
[285] Fix | Delete
[286] Fix | Delete
else:
[287] Fix | Delete
sys.stderr.write('ERROR: unknown destination format: %s\r\n%s\r\n' % (dstFormat, helpMessage))
[288] Fix | Delete
sys.exit(EX_USAGE)
[289] Fix | Delete
[290] Fix | Delete
if verboseFlag:
[291] Fix | Delete
sys.stderr.write("""Source MIB repositories: %s
[292] Fix | Delete
Borrow missing/failed MIBs from: %s
[293] Fix | Delete
Existing/compiled MIB locations: %s
[294] Fix | Delete
Compiled MIBs destination directory: %s
[295] Fix | Delete
MIBs excluded from code generation: %s
[296] Fix | Delete
MIBs to compile: %s
[297] Fix | Delete
Destination format: %s
[298] Fix | Delete
Parser grammar cache directory: %s
[299] Fix | Delete
Also compile all relevant MIBs: %s
[300] Fix | Delete
Rebuild MIBs regardless of age: %s
[301] Fix | Delete
Dry run mode: %s
[302] Fix | Delete
Create/update MIBs: %s
[303] Fix | Delete
Byte-compile Python modules: %s (optimization level %s)
[304] Fix | Delete
Ignore compilation errors: %s
[305] Fix | Delete
Generate OID->MIB index: %s
[306] Fix | Delete
Generate texts in MIBs: %s
[307] Fix | Delete
Keep original texts layout: %s
[308] Fix | Delete
Try various file names while searching for MIB module: %s
[309] Fix | Delete
""" % (', '.join(mibSources),
[310] Fix | Delete
', '.join([x[0] for x in mibBorrowers if x[1] == genMibTextsFlag]),
[311] Fix | Delete
', '.join(mibSearchers),
[312] Fix | Delete
dstDirectory,
[313] Fix | Delete
', '.join(sorted(mibStubs)),
[314] Fix | Delete
', '.join(inputMibs),
[315] Fix | Delete
dstFormat,
[316] Fix | Delete
cacheDirectory or 'not used',
[317] Fix | Delete
nodepsFlag and 'no' or 'yes',
[318] Fix | Delete
rebuildFlag and 'yes' or 'no',
[319] Fix | Delete
dryrunFlag and 'yes' or 'no',
[320] Fix | Delete
writeMibsFlag and 'yes' or 'no',
[321] Fix | Delete
dstFormat == 'pysnmp' and pyCompileFlag and 'yes' or 'no',
[322] Fix | Delete
dstFormat == 'pysnmp' and pyOptimizationLevel and 'yes' or 'no',
[323] Fix | Delete
ignoreErrorsFlag and 'yes' or 'no',
[324] Fix | Delete
buildIndexFlag and 'yes' or 'no',
[325] Fix | Delete
genMibTextsFlag and 'yes' or 'no',
[326] Fix | Delete
keepTextsLayout and 'yes' or 'no',
[327] Fix | Delete
doFuzzyMatchingFlag and 'yes' or 'no'))
[328] Fix | Delete
[329] Fix | Delete
# Initialize compiler infrastructure
[330] Fix | Delete
[331] Fix | Delete
mibCompiler = MibCompiler(
[332] Fix | Delete
SmiV1CompatParser(tempdir=cacheDirectory),
[333] Fix | Delete
codeGenerator,
[334] Fix | Delete
fileWriter
[335] Fix | Delete
)
[336] Fix | Delete
[337] Fix | Delete
try:
[338] Fix | Delete
mibCompiler.addSources(
[339] Fix | Delete
*getReadersFromUrls(
[340] Fix | Delete
*mibSources, **dict(fuzzyMatching=doFuzzyMatchingFlag)
[341] Fix | Delete
)
[342] Fix | Delete
)
[343] Fix | Delete
[344] Fix | Delete
mibCompiler.addSearchers(*searchers)
[345] Fix | Delete
[346] Fix | Delete
mibCompiler.addBorrowers(*borrowers)
[347] Fix | Delete
[348] Fix | Delete
processed = mibCompiler.compile(
[349] Fix | Delete
*inputMibs, **dict(noDeps=nodepsFlag,
[350] Fix | Delete
rebuild=rebuildFlag,
[351] Fix | Delete
dryRun=dryrunFlag,
[352] Fix | Delete
genTexts=genMibTextsFlag,
[353] Fix | Delete
textFilter=keepTextsLayout and (lambda symbol, text: text) or None,
[354] Fix | Delete
writeMibs=writeMibsFlag,
[355] Fix | Delete
ignoreErrors=ignoreErrorsFlag)
[356] Fix | Delete
)
[357] Fix | Delete
[358] Fix | Delete
if buildIndexFlag:
[359] Fix | Delete
mibCompiler.buildIndex(
[360] Fix | Delete
processed,
[361] Fix | Delete
dryRun=dryrunFlag,
[362] Fix | Delete
ignoreErrors=ignoreErrorsFlag
[363] Fix | Delete
)
[364] Fix | Delete
[365] Fix | Delete
except error.PySmiError:
[366] Fix | Delete
sys.stderr.write('ERROR: %s\r\n' % sys.exc_info()[1])
[367] Fix | Delete
sys.exit(EX_SOFTWARE)
[368] Fix | Delete
[369] Fix | Delete
else:
[370] Fix | Delete
if verboseFlag:
[371] Fix | Delete
sys.stderr.write('%sreated/updated MIBs: %s\r\n' % (dryrunFlag and 'Would be c' or 'C', ', '.join(
[372] Fix | Delete
['%s%s' % (x, x != processed[x].alias and ' (%s)' % processed[x].alias or '') for x in sorted(processed) if processed[x] == 'compiled'])))
[373] Fix | Delete
[374] Fix | Delete
sys.stderr.write('Pre-compiled MIBs %sborrowed: %s\r\n' % (dryrunFlag and 'Would be ' or '', ', '.join(
[375] Fix | Delete
['%s (%s)' % (x, processed[x].path) for x in sorted(processed) if processed[x] == 'borrowed'])))
[376] Fix | Delete
[377] Fix | Delete
sys.stderr.write(
[378] Fix | Delete
'Up to date MIBs: %s\r\n' % ', '.join(['%s' % x for x in sorted(processed) if processed[x] == 'untouched']))
[379] Fix | Delete
[380] Fix | Delete
sys.stderr.write('Missing source MIBs: %s\r\n' % ', '.join(
[381] Fix | Delete
['%s' % x for x in sorted(processed) if processed[x] == 'missing']))
[382] Fix | Delete
[383] Fix | Delete
sys.stderr.write(
[384] Fix | Delete
'Ignored MIBs: %s\r\n' % ', '.join(['%s' % x for x in sorted(processed) if processed[x] == 'unprocessed']))
[385] Fix | Delete
[386] Fix | Delete
sys.stderr.write('Failed MIBs: %s\r\n' % ', '.join(
[387] Fix | Delete
['%s (%s)' % (x, processed[x].error) for x in sorted(processed) if processed[x] == 'failed']))
[388] Fix | Delete
[389] Fix | Delete
exitCode = EX_OK
[390] Fix | Delete
[391] Fix | Delete
if any(x for x in processed.values() if x == 'missing'):
[392] Fix | Delete
exitCode = EX_MIB_MISSING
[393] Fix | Delete
[394] Fix | Delete
if any(x for x in processed.values() if x == 'failed'):
[395] Fix | Delete
exitCode = EX_MIB_FAILED
[396] Fix | Delete
[397] Fix | Delete
sys.exit(exitCode)
[398] Fix | Delete
[399] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function