Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../bin
File: mibcopy.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 copying tool
[7] Fix | Delete
#
[8] Fix | Delete
import os
[9] Fix | Delete
import sys
[10] Fix | Delete
import getopt
[11] Fix | Delete
import shutil
[12] Fix | Delete
from datetime import datetime
[13] Fix | Delete
from pysmi.reader import FileReader, getReadersFromUrls
[14] Fix | Delete
from pysmi.writer import CallbackWriter
[15] Fix | Delete
from pysmi.parser import SmiV1CompatParser
[16] Fix | Delete
from pysmi.codegen import JsonCodeGen
[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
[26] Fix | Delete
# Defaults
[27] Fix | Delete
quietFlag = False
[28] Fix | Delete
verboseFlag = False
[29] Fix | Delete
mibSources = []
[30] Fix | Delete
dstDirectory = None
[31] Fix | Delete
cacheDirectory = ''
[32] Fix | Delete
dryrunFlag = False
[33] Fix | Delete
ignoreErrorsFlag = False
[34] Fix | Delete
[35] Fix | Delete
helpMessage = """\
[36] Fix | Delete
Usage: %s [--help]
[37] Fix | Delete
[--version]
[38] Fix | Delete
[--verbose]
[39] Fix | Delete
[--quiet]
[40] Fix | Delete
[--debug=<%s>]
[41] Fix | Delete
[--mib-source=<URI>]
[42] Fix | Delete
[--cache-directory=<DIRECTORY>]
[43] Fix | Delete
[--ignore-errors]
[44] Fix | Delete
[--dry-run]
[45] Fix | Delete
<SOURCE [SOURCE...]> <DESTINATION>
[46] Fix | Delete
Where:
[47] Fix | Delete
URI - file, zip, http, https, ftp, sftp schemes are supported.
[48] Fix | Delete
Use @mib@ placeholder token in URI to refer directly to
[49] Fix | Delete
the required MIB module when source does not support
[50] Fix | Delete
directory listing (e.g. HTTP).
[51] Fix | Delete
""" % (
[52] Fix | Delete
sys.argv[0],
[53] Fix | Delete
'|'.join([x for x in sorted(debug.flagMap)])
[54] Fix | Delete
)
[55] Fix | Delete
[56] Fix | Delete
# TODO(etingof): add the option to copy MIBs into enterprise-indexed subdirs
[57] Fix | Delete
[58] Fix | Delete
try:
[59] Fix | Delete
opts, inputMibs = getopt.getopt(
[60] Fix | Delete
sys.argv[1:], 'hv',
[61] Fix | Delete
['help', 'version', 'verbose', 'quiet', 'debug=',
[62] Fix | Delete
'mib-source=', 'mib-stub=',
[63] Fix | Delete
'cache-directory=', 'ignore-errors', 'dry-run']
[64] Fix | Delete
)
[65] Fix | Delete
[66] Fix | Delete
except getopt.GetoptError:
[67] Fix | Delete
sys.exit(EX_USAGE)
[68] Fix | Delete
[69] Fix | Delete
for opt in opts:
[70] Fix | Delete
if opt[0] == '-h' or opt[0] == '--help':
[71] Fix | Delete
sys.stderr.write("""\
[72] Fix | Delete
Synopsis:
[73] Fix | Delete
SNMP SMI/MIB files copying tool. When given MIB file(s) or directory(ies)
[74] Fix | Delete
on input and a destination directory, the tool parses MIBs to figure out
[75] Fix | Delete
their canonical MIB module name and the latest revision date, then
[76] Fix | Delete
copies MIB module on input into the destination directory under its
[77] Fix | Delete
MIB module name *if* there is no such file already or its revision date
[78] Fix | Delete
is older.
[79] Fix | Delete
[80] Fix | Delete
Documentation:
[81] Fix | Delete
http://snmplabs.com/pysmi
[82] Fix | Delete
%s
[83] Fix | Delete
""" % helpMessage)
[84] Fix | Delete
sys.exit(EX_OK)
[85] Fix | Delete
[86] Fix | Delete
if opt[0] == '-v' or opt[0] == '--version':
[87] Fix | Delete
from pysmi import __version__
[88] Fix | Delete
[89] Fix | Delete
sys.stderr.write("""\
[90] Fix | Delete
SNMP SMI/MIB library version %s, written by Ilya Etingof <etingof@gmail.com>
[91] Fix | Delete
Python interpreter: %s
[92] Fix | Delete
Software documentation and support at http://snmplabs.com/pysmi
[93] Fix | Delete
%s
[94] Fix | Delete
""" % (__version__, sys.version, helpMessage))
[95] Fix | Delete
sys.exit(EX_OK)
[96] Fix | Delete
[97] Fix | Delete
if opt[0] == '--quiet':
[98] Fix | Delete
quietFlag = True
[99] Fix | Delete
[100] Fix | Delete
if opt[0] == '--verbose':
[101] Fix | Delete
verboseFlag = True
[102] Fix | Delete
[103] Fix | Delete
if opt[0] == '--debug':
[104] Fix | Delete
debug.setLogger(debug.Debug(*opt[1].split(',')))
[105] Fix | Delete
[106] Fix | Delete
if opt[0] == '--mib-source':
[107] Fix | Delete
mibSources.append(opt[1])
[108] Fix | Delete
[109] Fix | Delete
if opt[0] == '--cache-directory':
[110] Fix | Delete
cacheDirectory = opt[1]
[111] Fix | Delete
[112] Fix | Delete
if opt[0] == '--ignore-errors':
[113] Fix | Delete
ignoreErrorsFlag = True
[114] Fix | Delete
[115] Fix | Delete
if not mibSources:
[116] Fix | Delete
mibSources = ['file:///usr/share/snmp/mibs',
[117] Fix | Delete
'http://mibs.snmplabs.com/asn1/@mib@']
[118] Fix | Delete
[119] Fix | Delete
if len(inputMibs) < 2:
[120] Fix | Delete
sys.stderr.write('ERROR: MIB source and/or destination arguments not given\r\n%s\r\n' % helpMessage)
[121] Fix | Delete
sys.exit(EX_USAGE)
[122] Fix | Delete
[123] Fix | Delete
dstDirectory = inputMibs.pop()
[124] Fix | Delete
[125] Fix | Delete
if os.path.exists(dstDirectory) and not os.path.isdir(dstDirectory):
[126] Fix | Delete
sys.stderr.write('ERROR: given destination is not a directory\r\n%s\r\n' % helpMessage)
[127] Fix | Delete
sys.exit(EX_USAGE)
[128] Fix | Delete
[129] Fix | Delete
try:
[130] Fix | Delete
os.makedirs(dstDirectory, mode=0o755)
[131] Fix | Delete
[132] Fix | Delete
except OSError:
[133] Fix | Delete
pass
[134] Fix | Delete
[135] Fix | Delete
# Compiler infrastructure
[136] Fix | Delete
[137] Fix | Delete
codeGenerator = JsonCodeGen()
[138] Fix | Delete
[139] Fix | Delete
mibParser = SmiV1CompatParser(tempdir=cacheDirectory)
[140] Fix | Delete
[141] Fix | Delete
fileWriter = CallbackWriter(lambda *x: None)
[142] Fix | Delete
[143] Fix | Delete
[144] Fix | Delete
def getMibRevision(mibDir, mibFile):
[145] Fix | Delete
[146] Fix | Delete
mibCompiler = MibCompiler(
[147] Fix | Delete
mibParser,
[148] Fix | Delete
codeGenerator,
[149] Fix | Delete
fileWriter
[150] Fix | Delete
)
[151] Fix | Delete
[152] Fix | Delete
mibCompiler.addSources(
[153] Fix | Delete
FileReader(mibDir, recursive=False, ignoreErrors=ignoreErrorsFlag),
[154] Fix | Delete
*getReadersFromUrls(*mibSources)
[155] Fix | Delete
)
[156] Fix | Delete
[157] Fix | Delete
try:
[158] Fix | Delete
processed = mibCompiler.compile(
[159] Fix | Delete
mibFile, **dict(noDeps=True, rebuild=True, fuzzyMatching=False, ignoreErrors=ignoreErrorsFlag)
[160] Fix | Delete
)
[161] Fix | Delete
[162] Fix | Delete
except error.PySmiError:
[163] Fix | Delete
sys.stderr.write('ERROR: %s\r\n' % sys.exc_info()[1])
[164] Fix | Delete
sys.exit(EX_SOFTWARE)
[165] Fix | Delete
[166] Fix | Delete
for canonicalMibName in processed:
[167] Fix | Delete
if (processed[canonicalMibName] == 'compiled' and
[168] Fix | Delete
processed[canonicalMibName].path == 'file://' + os.path.join(mibDir, mibFile)):
[169] Fix | Delete
[170] Fix | Delete
try:
[171] Fix | Delete
revision = datetime.strptime(processed[canonicalMibName].revision, '%Y-%m-%d %H:%M')
[172] Fix | Delete
[173] Fix | Delete
except Exception:
[174] Fix | Delete
revision = datetime.fromtimestamp(0)
[175] Fix | Delete
[176] Fix | Delete
return canonicalMibName, revision
[177] Fix | Delete
[178] Fix | Delete
raise error.PySmiError('Can\'t read or parse MIB "%s"' % os.path.join(mibDir, mibFile))
[179] Fix | Delete
[180] Fix | Delete
[181] Fix | Delete
def shortenPath(path, maxLength=45):
[182] Fix | Delete
if len(path) > maxLength:
[183] Fix | Delete
return '...' + path[-maxLength:]
[184] Fix | Delete
else:
[185] Fix | Delete
return path
[186] Fix | Delete
[187] Fix | Delete
mibsSeen = mibsCopied = mibsFailed = 0
[188] Fix | Delete
[189] Fix | Delete
mibsRevisions = {}
[190] Fix | Delete
[191] Fix | Delete
for srcDirectory in inputMibs:
[192] Fix | Delete
[193] Fix | Delete
if verboseFlag:
[194] Fix | Delete
sys.stderr.write('Reading "%s"...\r\n' % srcDirectory)
[195] Fix | Delete
[196] Fix | Delete
if os.path.isfile(srcDirectory):
[197] Fix | Delete
mibFiles = [(os.path.abspath(os.path.dirname(srcDirectory)), os.path.basename(srcDirectory))]
[198] Fix | Delete
[199] Fix | Delete
else:
[200] Fix | Delete
mibFiles = [(os.path.abspath(dirName), mibFile)
[201] Fix | Delete
for dirName, _, mibFiles in os.walk(srcDirectory)
[202] Fix | Delete
for mibFile in mibFiles]
[203] Fix | Delete
[204] Fix | Delete
for srcDirectory, mibFile in mibFiles:
[205] Fix | Delete
[206] Fix | Delete
mibsSeen += 1
[207] Fix | Delete
[208] Fix | Delete
# TODO(etingof): also check module OID to make sure there is no name collision
[209] Fix | Delete
[210] Fix | Delete
try:
[211] Fix | Delete
mibName, srcMibRevision = getMibRevision(srcDirectory, mibFile)
[212] Fix | Delete
[213] Fix | Delete
except error.PySmiError as ex:
[214] Fix | Delete
if verboseFlag:
[215] Fix | Delete
sys.stderr.write('Failed to read source MIB "%s": %s\r\n' % (os.path.join(srcDirectory, mibFile), ex))
[216] Fix | Delete
[217] Fix | Delete
if not quietFlag:
[218] Fix | Delete
sys.stderr.write('FAILED %s\r\n' % shortenPath(os.path.join(srcDirectory, mibFile)))
[219] Fix | Delete
[220] Fix | Delete
mibsFailed +=1
[221] Fix | Delete
[222] Fix | Delete
continue
[223] Fix | Delete
[224] Fix | Delete
if mibName in mibsRevisions:
[225] Fix | Delete
dstMibRevision = mibsRevisions[mibName]
[226] Fix | Delete
[227] Fix | Delete
else:
[228] Fix | Delete
try:
[229] Fix | Delete
_, dstMibRevision = getMibRevision(dstDirectory, mibName)
[230] Fix | Delete
[231] Fix | Delete
except error.PySmiError as ex:
[232] Fix | Delete
if verboseFlag:
[233] Fix | Delete
sys.stderr.write('MIB "%s" is not available at the '
[234] Fix | Delete
'destination directory "%s": %s\r\n' % (os.path.join(srcDirectory, mibFile),
[235] Fix | Delete
dstDirectory, ex))
[236] Fix | Delete
[237] Fix | Delete
dstMibRevision = datetime.fromtimestamp(0)
[238] Fix | Delete
[239] Fix | Delete
mibsRevisions[mibName] = dstMibRevision
[240] Fix | Delete
[241] Fix | Delete
if dstMibRevision >= srcMibRevision:
[242] Fix | Delete
if verboseFlag:
[243] Fix | Delete
sys.stderr.write('Destination MIB "%s" has the same or newer revision as the '
[244] Fix | Delete
'source MIB "%s"\r\n' % (os.path.join(dstDirectory, mibName),
[245] Fix | Delete
os.path.join(srcDirectory, mibFile)))
[246] Fix | Delete
if not quietFlag:
[247] Fix | Delete
sys.stderr.write('NOT COPIED %s (%s)\r\n' % (
[248] Fix | Delete
shortenPath(os.path.join(srcDirectory, mibFile)), mibName))
[249] Fix | Delete
[250] Fix | Delete
continue
[251] Fix | Delete
[252] Fix | Delete
mibsRevisions[mibName] = srcMibRevision
[253] Fix | Delete
[254] Fix | Delete
if verboseFlag:
[255] Fix | Delete
sys.stderr.write('Copying "%s" (revision "%s") -> "%s" (revision "%s")\r\n' % (
[256] Fix | Delete
os.path.join(srcDirectory, mibFile), srcMibRevision,
[257] Fix | Delete
os.path.join(dstDirectory, mibName), dstMibRevision))
[258] Fix | Delete
[259] Fix | Delete
try:
[260] Fix | Delete
shutil.copy(os.path.join(srcDirectory, mibFile), os.path.join(dstDirectory, mibName))
[261] Fix | Delete
[262] Fix | Delete
except Exception as ex:
[263] Fix | Delete
if verboseFlag:
[264] Fix | Delete
sys.stderr.write('Failed to copy MIB "%s" -> "%s" (%s): "%s"\r\n' % (
[265] Fix | Delete
os.path.join(srcDirectory, mibFile), os.path.join(dstDirectory, mibName), mibName, ex))
[266] Fix | Delete
[267] Fix | Delete
if not quietFlag:
[268] Fix | Delete
sys.stderr.write('FAILED %s (%s)\r\n' % (
[269] Fix | Delete
shortenPath(os.path.join(srcDirectory, mibFile)), mibName))
[270] Fix | Delete
[271] Fix | Delete
mibsFailed += 1
[272] Fix | Delete
[273] Fix | Delete
else:
[274] Fix | Delete
if not quietFlag:
[275] Fix | Delete
sys.stderr.write('COPIED %s (%s)\r\n' % (
[276] Fix | Delete
shortenPath(os.path.join(srcDirectory, mibFile)), mibName))
[277] Fix | Delete
[278] Fix | Delete
mibsCopied +=1
[279] Fix | Delete
[280] Fix | Delete
if not quietFlag:
[281] Fix | Delete
sys.stderr.write("MIBs seen: %d, copied: %d, failed: %d\r\n" % (mibsSeen, mibsCopied, mibsFailed))
[282] Fix | Delete
[283] Fix | Delete
sys.exit(EX_OK)
[284] Fix | Delete
[285] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function