pysnmp 获取设备 mib

snmpwalk 指令获取设备 mib

snmpwalk -v 1 -c public ip .1 > ip.mibs

pysnmp 获取设备 mib

安装环境

pip3 install pysnmp
pip3 install pysnmp-mibs

代码(snmpwalk.py):

from pysnmp.entity.rfc3413.oneliner import cmdgen
import sys

#debug模式,可以观察mib文件是如何寻找的
#from pysnmp import debug
#debug.setLogger(debug.Debug('mibbuild'))

arg_len = len(sys.argv)
if arg_len == 2:
    ip = sys.argv[1]
else:
    print("Usage:\n python3 snmpwalk.py [ip]]")
    sys.exit(0)

cmdGen = cmdgen.CommandGenerator()

errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
    cmdgen.CommunityData('public'),
    cmdgen.UdpTransportTarget((ip, 161)),
    cmdgen.MibVariable('IF-MIB', '').loadMibs(),
    lexicographicMode=True, maxRows=100000,
    ignoreNonIncreasingOid=True,
    lookupNames=True, lookupValues=True
)


if errorIndication:
    print(errorIndication)
else:
    if errorStatus:
        print('%s at %s' % (
            errorStatus.prettyPrint(),
            errorIndex and varBindTable[-1][int(errorIndex)-1] or '?'
            )
        )
    else:
        with open('{0}.mib'.format(ip),'w') as f:
            for varBindTableRow in varBindTable:
                for name, val in varBindTableRow:
                    mib_text = '%s = %s' % (name.prettyPrint(), val.prettyPrint())
                    print(mib_text)
                    f.write(mib_text+'\n')

运行:

python3 snmpwalk.py 192.168.1.1

打包

使用pyinstaller打包后提示找不到IF-MIB.py
需要在snmpwalk.py同级目录下新建 snmpwalk.spec

# -*- mode: python ; coding: utf-8 -*-
from PyInstaller.utils.hooks import collect_data_files
from PyInstaller.utils.hooks import collect_submodules

block_cipher = None


a = Analysis(['snmpwalk.py'],
             pathex=['/usr/local/lib/python3.5/dist-packages/', '/home/brian/test/snmpwalk'],
             binaries=[],
             datas=[],
             hiddenimports=collect_submodules('pysmi')+\
                 collect_submodules('pysnmp_mibs')+\
                 collect_submodules('pysnmp'),
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
x=Tree('/usr/local/lib/python3.5/dist-packages/pysnmp/smi/mibs',prefix='pysnmp/smi/mibs',excludes='.py')
y=Tree('/usr/local/lib/python3.5/dist-packages/pysmi',prefix='pysmi',excludes='.py')
z=Tree('/usr/local/lib/python3.5/dist-packages/pysnmp_mibs',prefix='pysnmp_mibs',excludes='.py')


pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          x,y,z,
          name='snmpwalk',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=True )

之后执行:

pyinstaller snmpwalk.spec

参考

[1]. 如何获取pysnmp以使用pysnmp-mib进行友好输出?
[2]. pysnmp.smi.error.MibNotFoundError
[3]. 无法让 pysnmp 与 pyinstaller 一起使用(Can't get pysnmp to work with pyinstaller)

posted @ 2022-05-26 18:37  BrianSun  阅读(826)  评论(0编辑  收藏  举报