gpt4 book ai didi

python - 我需要不同的 MIB 和那些 OID 吗?

转载 作者:太空宇宙 更新时间:2023-11-04 05:23:10 25 4
gpt4 key购买 nike

我得到了这个通过 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 用法

来自section Usage of the official documentation他们使用 load 方法加载 MIB 文件。

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

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

查找 OID 名称

我找不到某些标识符的 OID 名称。例如:

问题

  • 根据我尝试使用的 OID 的名称,我是否需要加载不同的 MIB 文件? (例如 Printer-MIBIF-MIB 等)
  • 在哪里可以找到丢失的 OID 的名称

最佳答案

如果您使用 load() 方法,那么它的标量和行名称将作为实例属性提供,因此您可以查询“sysContact”等,但因为“sysDescr”和“sysName”是不是 IF-MIB 的一部分,您将无法获得它。

您需要加载相关的 MIB,例如 SNMPv2-MIB 或尝试直接通过 OID 获取值。

更新:我看了一下,很傲慢,它看起来像 pysnmp 正在收集,所以你总是可以直接使用它。下面的示例正在收集一个新的不同的 SNMP 值,一些是通过 OID 和其他通过 MIB 中的命名变量收集的(如果你想通过名称获取,你需要有相关的 MIB 可用)。此示例几乎取自 pySNMP documentation

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()))

关于python - 我需要不同的 MIB 和那些 OID 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39640966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com