PySNMP Errors when working with MIB files
Question
I'm attempting to use MIB files in PySNMP. The code is fairly straightforward. Nothing complex. Just trying to get the information under an OID. The code I'm using is as follows:
#!/usr/local/bin/python2.7
from pysnmp.smi import builder, view, error
from pysnmp.entity.rfc3413.oneliner import cmdgen
cmdGen = cmdgen.CommandGenerator()
mibBuilder = builder.MibBuilder()
mibPath = mibBuilder.getMibPath() + ( '/path/to/command/mibs', )
mibBuilder.setMibPath( *mibPath )
mibBuilder.loadModules(
'MIB-File',
)
mibView = view.MibViewController( mibBuilder )
errorIndication, errorStatus, errorIndex, \
varBindTable = cmdGen.nextCmd(
cmdgen.CommunityData( 'Name', 'Community' ),
cmdgen.UdpTransportTarget( ( 'IP Address', 161 ) ),
( ( '', 'serverName' ), ),
)
print varBindTable
I know the data put into this is accurate as when I use a asynCommandGenerator.asyncNextCmd
using the same udpTransportTarget
and CommunityData
it works without issue. Plus the error I'm seeing is very specific to the MibBuilder Components.
The Error I'm seeing is:
Traceback (most recent call last):
File "./snmpcollectortest.py", line 11, in
'NS-MIB-smiv2',
File "/usr/lib/python2.7/site-packages/pysnmp-4.2.1-py2.7.egg/pysnmp/smi/builder.py", line 221, in loadModules
pysnmp.smi.error.SmiError: MIB module "/path/to/command/mibs/MIB-File.py" load error: MIB file "ASN1.py[co]" not found in search path
Update:
I found that I didn't have M2Crypto installed which is why I couldn't find ASN1.py. However I have corrected this and I'm still getting the same error.
Solution
The getMibPath()
/setMibPath()
methods are obsolete. They don't work unless you .egg pysnmp or its MIB modules.
You should always use the getMibSources()
/setMibSources()
methods instead. These work for both .egg and file-based setup.
mibPath = mibBuilder.getMibSources() + (builder.DirMibSource('/path/to/command/mibs'),)
mibBuilder.setMibSources(*mibPath)
mibBuilder.loadModules(
'MIB-File',
)
BTW, pysnmp does not require M2Crypto, what is required is pyasn1 and pycrypto (for SNMPv3 ciphering only).