2015-03-31 76 views
0

我正在尝试使用PySNMP进行SNMP-walk。我写了下面的脚本,它可以工作,但对于我“走路”的每个设备,只有大约10行的OID可以解析为“真实姓名”。使PySNMP解决对象ID

from pysnmp.entity.rfc3413.oneliner import cmdgen 
from os.path import exists 
import sys 
import os 

# Turn on debugging 
#debug.setLogger(debug.Debug('msgproc', 'secmod')) 

# Enter parameters 
target_IP   = raw_input('Target IP (192.168.13.100): ') or '192.168.13.100' 
target_port   = raw_input('Target port (161): ') or 161 
max_timeout   = int(raw_input('Maximum timeout in seconds (1):')) or 1 
max_retries   = int(raw_input('Maximum number of retries (0):')) or 0 
target_file_name = raw_input('Target filename (.txt is added): ') + '.txt' 

# Check for already existing file 
path = 'walks/' 
if not os.path.exists(path): 
    os.makedirs(path) 
if exists(path+target_file_name): 
    sys.exit("The file '%s' already exists. Try again." % target_file_name) 
else: 
    target_file = open(path+target_file_name, 'w+') 

# Initialize counter to zero 
counter = 0 

# Create command generator 
cmdGen = cmdgen.CommandGenerator() 

# Get data 

errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
    cmdgen.CommunityData('public'), 
    cmdgen.UdpTransportTarget((target_IP, target_port), timeout=max_timeout, retries=max_retries), 
    '1.3', 
    lexicographicMode=True, 
    #maxRows=1000, 
    ignoreNonIncreasingOid=True, 
    lookupNames=True 
) 

# Print errors and values to file 
if errorIndication: 
    print(errorIndication) 
else: 
    # Print error messages 

    if errorStatus: 
     print('%s at %s' % (
      errorStatus.prettyPrint(), 
      errorIndex and varBindTable[-1][int(errorIndex)-1] or '?' 
      ) 
     ) 
    else: 
     # Print values 
     for varBindTableRow in varBindTable: 
      for name, val in varBindTableRow: 
       counter += 1 
       target_file.write("(%s)\t %s value = \t%s\n" % (counter, name.prettyPrint(), val.prettyPrint())) 

     # Finish the operation     
     target_file.close() 
     print('Writing to %s successful. %d lines have been written' % (target_file_name, counter)) 
     sys.exit(0) 

结果是一个包含很多行的文件。 第一entrys是这样的:

(1) SNMPv2-MIB::sysDescr."0" value = Hirschmann MAR 
(2) SNMPv2-MIB::sysObjectID."0" value = 1.5.6.9.1.1.248.4.10.90 
(3) SNMPv2-MIB::sysUpTime."0" value = 2626357 
(4) SNMPv2-MIB::sysContact."0" value =  Hirschmann Automation and Control GmbH 
(5) SNMPv2-MIB::sysName."0" value = mar1030.plc-s7-15000 
(6) SNMPv2-MIB::sysLocation."0" value = Hirschmann MAR 
(7) SNMPv2-MIB::sysServices."0" value = 2 
(8) SNMPv2-MIB::sysORLastChange."0" value = 300 

但随后的OID不再被解决:

(9) SNMPv2-SMI::mib-2."2.1.0" value = 27 
(10)  SNMPv2-SMI::mib-2."2.2.1.1.1" value = 1 
(11)  SNMPv2-SMI::mib-2."2.2.1.1.2" value = 2 
(12)  SNMPv2-SMI::mib-2."2.2.1.1.3" value = 3 
..... 

什么原因造成这一点,我能做些什么呢?

回答

0

要使用pysnmp解析MIB,必须在进行SNMP查询之前将它们加载到pysnmp引擎中。您的脚本为SNMPv2-MIB执行MIB解析,因为它是由pysnmp引擎自动加载的。

要使用带有pysnmp的纯文本MIB,必须将它们从纯文本格式(例如ASN.1)转换为pysnmp格式(Python代码)。您可以使用build-pysnmp-mib shell脚本(基于libsmi)或下载并安装预编译的通用MIB包(pysnmp-mibs Python包)。

要加载一些或所有可用的MIB,您可以使用MibVariable.loadMibs(* mibs)方法。例如,请参阅this script

+0

好的,我已经安装了MIB包。现在,当我使用'cmdgen.MibVariable('IF-MIB','').loadMibs()'作为参数时,我得到了一个更大的文件,但是 - 据我了解 - 只有“IF分支”。因此,如果我输入INTERNET-MIB,它应该从顶部向下走整棵树,这可能需要一些时间。我是否理解正确? – vicco 2015-04-01 07:34:24