Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我需要不同的MIB和那些OID吗?_Python_Powershell_Snmp_Mib_Snimpy - Fatal编程技术网

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

Python 我需要不同的MIB和那些OID吗?,python,powershell,snmp,mib,snimpy,Python,Powershell,Snmp,Mib,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 ($

我得到了这个通过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 *
卑鄙的用法 从中,他们使用
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文件?(例如,
    打印机MIB
    IF-MIB
    等)
  • 在哪里可以找到丢失的
    OID
    的名称
如果使用load()方法,则其中的标量和行名称将作为实例属性提供,因此您可以查询“sysContact”等,但由于“sysDescr”和“sysName”不是If-MIB的一部分,您将无法获取它

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

更新: 我看了一下snimpy,它看起来像是pysnmp正在收集,所以你可以直接使用它。下面的示例正在收集一个新的不同的SNMP值,有些是通过OID收集的,有些是通过MIB中的命名变量收集的(如果希望通过名称获取,则需要有相关的MIB可用)。这个样本几乎是从


谢谢,您如何知道在哪里可以找到
OID
?我已经为您扩展了我的答案,并给出了一个使用pysnmp通过OID检索的示例
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()))