简体   繁体   English

我是否需要使用这些 OID 的不同 MIB?

[英]Do I need different MIB with those OID?

I got this legacy powershell script that retrieve information via SNMP, I'm trying to port it in Python using snimpy .我得到了这个通过 SNMP 检索信息的传统powershell脚本,我正在尝试使用snimpy将它移植到 Python 中。

$PrintersIP = '10.100.7.47', '10.100.7.48'
Function Get-SNMPInfo ([String[]]$Printers) {
    Begin {
        $SNMP = New-Object -ComObject olePrn.OleSNMP
    }
    Process {
        Foreach ($IP in $Printers) {
            $SNMP.Open($IP,"public",2,3000)
            [PSCustomObject][Ordered]@{
                Name        = $SNMP.Get(".1.3.6.1.2.1.1.5.0")
                IP          = $IP
                UpTime      = [TimeSpan]::FromSeconds(($SNMP.Get(".1.3.6.1.2.1.1.3.0"))/100)
                Model       = $SNMP.Get(".1.3.6.1.2.1.25.3.2.1.3.1")
                Description = $SNMP.Get(".1.3.6.1.2.1.1.1.0")
                #Contact     = $SNMP.Get(".1.3.6.1.2.1.1.4.0")
                #SN          = $SNMP.Get(".1.3.6.1.2.1.43.5.1.1.17.1")
                #Location    = $SNMP.Get(".1.3.6.1.2.1.1.6.0")
                #TonerName   = $SNMP.Get("43.11.1.1.6.1.1")
            }
        }
    }
    End {
        $SNMP.Close()
    }
}
Get-SNMPInfo $PrintersIP | ft -AutoSize *

Snimpy usage吝啬的用法

From the section Usage of the official documentation they use the load method to load MIB a file.官方文档的用法部分,他们使用load方法加载 MIB 文件。

from snimpy.manager import Manager as M
from snimpy.manager import load

load("IF-MIB")
m = M("localhost")
print(m.ifDescr[0])

Finding OID name查找OID名称

I can't find the OID name for some of the identifiers.我找不到某些标识符的OID名称。 For instance:例如:

Question

  • Depending on the OID 's name I'm trying to use, do I need to load different MIB file ?根据我尝试使用的OID名称,是否需要加载不同的 MIB 文件? (eg Printer-MIB , IF-MIB , etc.) (例如Printer-MIBIF-MIB等)
  • Where can I find the missing OID 's name我在哪里可以找到丢失的OID的名称

If you use the load() method then scalars and row names from it will be made available as an instance attributes hence you're able to query for 'sysContact' etc, but as the 'sysDescr' and 'sysName' are not part of the IF-MIB you will not be able to get it.如果您使用 load() 方法,那么它的标量和行名称将作为实例属性可用,因此您可以查询 'sysContact' 等,但因为 'sysDescr' 和 'sysName' 不是您将无法获得 IF-MIB。

You will need to load in the relevant MIB such as SNMPv2-MIB or attempt to get the value via the OID directly.您需要加载相关的 MIB,例如 SNMPv2-MIB 或尝试直接通过 OID 获取值。

UPDATE: I've had a look and snimpy and it loooks like pysnmp is doing the collection, so you could always user that directly.更新:我已经看了一眼,看起来 pysnmp 正在收集,所以你总是可以直接使用它。 The sample below is collecting a new different SNMP values some by OID and others via the named variable within the MIB (you will need to have the relevant MIB available if you want to get via the name).下面的示例收集了一些新的不同 SNMP 值,一些是通过 OID 的,另一些是通过 MIB 中的命名变量(如果要通过名称获取,则需要相关的 MIB 可用)。 This sample is pretty much taken from the pySNMP documentation此示例几乎取自pySNMP 文档

from pysnmp.entity.rfc3413.oneliner import cmdgen

cmdGen = cmdgen.CommandGenerator()

errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
    cmdgen.CommunityData('public'),
    cmdgen.UdpTransportTarget(('demo.snmplabs.com', 161)),
    '1.3.6.1.2.1.1.1.0',            # sysDescr
    '1.3.6.1.2.1.1.2.0',            # sysObjectId
    '1.3.6.1.2.1.1.3.0',            # upTime
    '1.3.6.1.2.1.1.4.0',            # Contact
    '1.3.6.1.2.1.1.5.0',            # sysName
    '1.3.6.1.2.1.1.6.0',            # Location
    cmdgen.MibVariable('SNMPv2-MIB', 'sysDescr', 0),     #.1.3.6.1.2.1.1.1.0 sysDescr
    cmdgen.MibVariable('SNMPv2-MIB', 'sysName', 0)     #.1.3.6.1.2.1.1.5.0 sysName
)



# Check for errors and print out results
if errorIndication:
    print(errorIndication)
else:
    if errorStatus:
        print('%s at %s' % (
            errorStatus.prettyPrint(),
            errorIndex and varBinds[int(errorIndex)-1] or '?'
            )
        )
    else:
        for name, val in varBinds:
            print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM