下面的代码用来模仿命令(在'server'上执行脚本)。
snmpget server 'NET-SNMP-EXTEND-MIB::nsExtendOutputFull."queryMonitoring"'
这是代码。
from pysnmp.hlapi import *
for (errorIndication,
errorStatus,
errorIndex,
varBinds) in nextCmd(
SnmpEngine(OctetString(hexValue='8000000001020304')),
UsmUserData(userName='x', authKey='x', privKey='x'),
UdpTransportTarget(('server', 161)),
ContextData(),
ObjectType(ObjectIdentity('NET-SNMP-EXTEND-MIB', 'nsExtendOutputFull', "queryMonitoring"))):
if errorIndication:
print(errorIndication)
break
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
break
else:
for varBind in varBinds:
print(' = '.join([x.prettyPrint() for x in varBind]))
问题是这样的
代码上面的命令产生了预期的279个结果 类似于这样的结果。
NET-SNMP-EXTEND-MIB::nsExtendOutLine."queryMonitoring".275 = (275, u'14:01:39', u'webserver3', u'0.0')
NET-SNMP-EXTEND-MIB::nsExtendOutLine."queryMonitoring".276 = (276, u'14:01:39', u'webserver2', u'0.0')
NET-SNMP-EXTEND-MIB::nsExtendOutLine."queryMonitoring".277 = (277, u'14:01:40', u'webserver4', u'1.0')
NET-SNMP-EXTEND-MIB::nsExtendOutLine."queryMonitoring".278 = (278, u'14:01:40', u'webserver1', u'76.5')
NET-SNMP-EXTEND-MIB::nsExtendOutLine."queryMonitoring".279 = (279, u'14:01:40', u'webserver3', u'0.0')
这个python脚本在不同的MIB中产生了额外的输出。
...
NET-SNMP-EXTEND-MIB::nsExtendOutLine."queryMonitoring".274 = (274, u'14:01:39', u'webserver1', u'74.5')
NET-SNMP-EXTEND-MIB::nsExtendOutLine."queryMonitoring".275 = (275, u'14:01:39', u'webserver3', u'0.0')
NET-SNMP-EXTEND-MIB::nsExtendOutLine."queryMonitoring".276 = (276, u'14:01:39', u'webserver2', u'0.0')
NET-SNMP-EXTEND-MIB::nsExtendOutLine."queryMonitoring".277 = (277, u'14:01:40', u'webserver4', u'1.0')
NET-SNMP-EXTEND-MIB::nsExtendOutLine."queryMonitoring".278 = (278, u'14:01:40', u'webserver1', u'76.5')
NET-SNMP-EXTEND-MIB::nsExtendOutLine."queryMonitoring".279 = (279, u'14:01:40', u'webserver3', u'0.0')
NET-SNMP-AGENT-MIB::nsCacheDefaultTimeout.0 = 5
NET-SNMP-AGENT-MIB::nsCacheEnabled.0 = false
NET-SNMP-AGENT-MIB::nsCacheTimeout.1.3.6.1.2.1.2.2 = 3
NET-SNMP-AGENT-MIB::nsCacheTimeout.1.3.6.1.2.1.4 = 5
NET-SNMP-AGENT-MIB::nsCacheTimeout.1.3.6.1.2.1.4.24.4 = 60
NET-SNMP-AGENT-MIB::nsCacheTimeout.1.3.6.1.2.1.4.24.7 = 60
NET-SNMP-AGENT-MIB::nsCacheTimeout.1.3.6.1.2.1.4.31.1 = 60
NET-SNMP-AGENT-MIB::nsCacheTimeout.1.3.6.1.2.1.4.31.3 = 60
NET-SNMP-AGENT-MIB::nsCacheTimeout.1.3.6.1.2.1.4.32 = 60
NET-SNMP-AGENT-MIB::nsCacheTimeout.1.3.6.1.2.1.4.34 = 30
NET-SNMP-AGENT-MIB::nsCacheTimeout.1.3.6.1.2.1.4.35 = 5
NET-SNMP-AGENT-MIB::nsCacheTimeout.1.3.6.1.2.1.4.36 = 60
NET-SNMP-AGENT-MIB::nsCacheTimeout.1.3.6.1.2.1.4.37 = 60
NET-SNMP-AGENT-MIB::nsCacheTimeout.1.3.6.1.2.1.5 = 5
NET-SNMP-AGENT-MIB::nsCacheTimeout.1.3.6.1.2.1.5.29 = 5
NET-SNMP-AGENT-MIB::nsCacheTimeout.1.3.6.1.2.1.5.30 = 5
NET-SNMP-AGENT-MIB::nsCacheTimeout.1.3.6.1.2.1.6 = 5
NET-SNMP-AGENT-MIB::nsCacheTimeout.1.3.6.1.2.1.6.13 = 5
NET-SNMP-AGENT-MIB::nsCacheTimeout.1.3.6.1.2.1.6.19 = 60
NET-SNMP-AGENT-MIB::nsCacheTimeout.1.3.6.1.2.1.6.20 = 60
NET-SNMP-AGENT-MIB::nsCacheTimeout.1.3.6.1.2.1.7 = 5
NET-SNMP-AGENT-MIB::nsCacheTimeout.1.3.6.1.2.1.7.5 = 5
NET-SNMP-AGENT-MIB::nsCacheTimeout.1.3.6.1.2.1.7.7 = 60
NET-SNMP-AGENT-MIB::nsCacheTimeout.1.3.6.1.2.1.10.7.2 = 60
NET-SNMP-AGENT-MIB::nsCacheTimeout.1.3.6.1.2.1.25.4.2 = 30
NET-SNMP-AGENT-MIB::nsCacheTimeout.1.3.6.1.2.1.25.6.3 = 30
NET-SNMP-AGENT-MIB::nsCacheTimeout.1.3.6.1.4.1.8072.1.31 = 5
NET-SNMP-AGENT-MIB::nsCacheStatus.1.3.6.1.2.1.2.2 = cached
NET-SNMP-AGENT-MIB::nsCacheStatus.1.3.6.1.2.1.4 = expired
NET-SNMP-AGENT-MIB::nsCacheStatus.1.3.6.1.2.1.4.24.4 = expired
NET-SNMP-AGENT-MIB::nsCacheStatus.1.3.6.1.2.1.4.24.7 = expired
NET-SNMP-AGENT-MIB::nsCacheStatus.1.3.6.1.2.1.4.31.1 = cached
NET-SNMP-AGENT-MIB::nsCacheStatus.1.3.6.1.2.1.4.31.3 = cached
NET-SNMP-AGENT-MIB::nsCacheStatus.1.3.6.1.2.1.4.32 = expired
NET-SNMP-AGENT-MIB::nsCacheStatus.1.3.6.1.2.1.4.34 = cached
NET-SNMP-AGENT-MIB::nsCacheStatus.1.3.6.1.2.1.4.35 = expired
NET-SNMP-AGENT-MIB::nsCacheStatus.1.3.6.1.2.1.4.36 = expired
NET-SNMP-AGENT-MIB::nsCacheStatus.1.3.6.1.2.1.4.37 = expired
NET-SNMP-AGENT-MIB::nsCacheStatus.1.3.6.1.2.1.5 = expired
NET-SNMP-AGENT-MIB::nsCacheStatus.1.3.6.1.2.1.5.29 = expired
NET-SNMP-AGENT-MIB::nsCacheStatus.1.3.6.1.2.1.5.30 = expired
NET-SNMP-AGENT-MIB::nsCacheStatus.1.3.6.1.2.1.6 = expired
NET-SNMP-AGENT-MIB::nsCacheStatus.1.3.6.1.2.1.6.13 = expired
NET-SNMP-AGENT-MIB::nsCacheStatus.1.3.6.1.2.1.6.19 = expired
NET-SNMP-AGENT-MIB::nsCacheStatus.1.3.6.1.2.1.6.20 = expired
NET-SNMP-AGENT-MIB::nsCacheStatus.1.3.6.1.2.1.7 = expired
NET-SNMP-AGENT-MIB::nsCacheStatus.1.3.6.1.2.1.7.5 = expired
NET-SNMP-AGENT-MIB::nsCacheStatus.1.3.6.1.2.1.7.7 = expired
NET-SNMP-AGENT-MIB::nsCacheStatus.1.3.6.1.2.1.10.7.2 = expired
NET-SNMP-AGENT-MIB::nsCacheStatus.1.3.6.1.2.1.25.4.2 = expired
NET-SNMP-AGENT-MIB::nsCacheStatus.1.3.6.1.2.1.25.6.3 = empty
NET-SNMP-AGENT-MIB::nsCacheStatus.1.3.6.1.4.1.8072.1.31 = expired
NET-SNMP-AGENT-MIB::nsDebugEnabled.0 = false
NET-SNMP-AGENT-MIB::nsDebugOutputAll.0 = false
NET-SNMP-AGENT-MIB::nsDebugDumpPdu.0 = false
NET-SNMP-AGENT-MIB::nsLogType.debug."/dev/null" = file
NET-SNMP-AGENT-MIB::nsLogType.debug."snmpd" = syslog
NET-SNMP-AGENT-MIB::nsLogMaxLevel.debug."/dev/null" = emergency
NET-SNMP-AGENT-MIB::nsLogMaxLevel.debug."snmpd" = emergency
NET-SNMP-AGENT-MIB::nsLogStatus.debug."/dev/null" = active
NET-SNMP-AGENT-MIB::nsLogStatus.debug."snmpd" = active
NET-SNMP-MIB::netSnmpObjects.9.1.1.2.7.103.114.112.100.101.109.111.0.3.3.4.114.101.97.100 = 2
NET-SNMP-MIB::netSnmpObjects.9.1.1.2.7.103.114.112.100.101.109.111.0.3.3.5.119.114.105.116.101 = 2
NET-SNMP-MIB::netSnmpObjects.9.1.1.2.7.103.114.112.100.101.109.111.0.3.3.6.110.111.116.105.102.121 = 2
NET-SNMP-MIB::netSnmpObjects.9.1.1.2.8.103.114.112.99.111.109.109.50.0.0.1.4.114.101.97.100 = 2
NET-SNMP-MIB::netSnmpObjects.9.1.1.2.8.103.114.112.99.111.109.109.50.0.0.1.5.119.114.105.116.101 = 2
NET-SNMP-MIB::netSnmpObjects.9.1.1.2.8.103.114.112.99.111.109.109.50.0.0.1.6.110.111.116.105.102.121 = 2
NET-SNMP-MIB::netSnmpObjects.9.1.1.2.8.103.114.112.99.111.109.109.51.0.0.1.4.114.101.97.100 = 2
NET-SNMP-MIB::netSnmpObjects.9.1.1.2.8.103.114.112.99.111.109.109.51.0.0.1.5.119.114.105.116.101 = 2
NET-SNMP-MIB::netSnmpObjects.9.1.1.2.8.103.114.112.99.111.109.109.51.0.0.1.6.110.111.116.105.102.121 = 2
NET-SNMP-MIB::netSnmpObjects.9.1.1.2.15.103.114.112.97.117.116.104.79.110.108.121.85.115.101.114.0.3.2.4.114.101.97.100 = 2
NET-SNMP-MIB::netSnmpObjects.9.1.1.2.15.103.114.112.97.117.116.104.79.110.108.121.85.115.101.114.0.3.2.5.119.114.105.116.101 = 2
NET-SNMP-MIB::netSnmpObjects.9.1.1.2.15.103.114.112.97.117.116.104.79.110.108.121.85.115.101.114.0.3.2.6.110.111.116.105.102.121 = 2
NET-SNMP-MIB::netSnmpObjects.9.1.1.2.15.103.114.112.105.110.116.101.114.110.97.108.85.115.101.114.0.3.2.4.114.101.97.100 = 2
NET-SNMP-MIB::netSnmpObjects.9.1.1.2.15.103.114.112.105.110.116.101.114.110.97.108.85.115.101.114.0.3.2.5.119.114.105.116.101 = 2
NET-SNMP-MIB::netSnmpObjects.9.1.1.2.15.103.114.112.105.110.116.101.114.110.97.108.85.115.101.114.0.3.2.6.110.111.116.105.102.121 = 2
NET-SNMP-MIB::netSnmpObjects.9.1.1.3.7.103.114.112.100.101.109.111.0.3.3.4.114.101.97.100 = _all_
NET-SNMP-MIB::netSnmpObjects.9.1.1.3.7.103.114.112.100.101.109.111.0.3.3.5.119.114.105.116.101 = _all_
NET-SNMP-MIB::netSnmpObjects.9.1.1.3.7.103.114.112.100.101.109.111.0.3.3.6.110.111.116.105.102.121 = _all_
NET-SNMP-MIB::netSnmpObjects.9.1.1.3.8.103.114.112.99.111.109.109.50.0.0.1.4.114.101.97.100 = all
NET-SNMP-MIB::netSnmpObjects.9.1.1.3.8.103.114.112.99.111.109.109.50.0.0.1.5.119.114.105.116.101 = none
NET-SNMP-MIB::netSnmpObjects.9.1.1.3.8.103.114.112.99.111.109.109.50.0.0.1.6.110.111.116.105.102.121 = none
NET-SNMP-MIB::netSnmpObjects.9.1.1.3.8.103.114.112.99.111.109.109.51.0.0.1.4.114.101.97.100 = systemonly
NET-SNMP-MIB::netSnmpObjects.9.1.1.3.8.103.114.112.99.111.109.109.51.0.0.1.5.119.114.105.116.101 = none
NET-SNMP-MIB::netSnmpObjects.9.1.1.3.8.103.114.112.99.111.109.109.51.0.0.1.6.110.111.116.105.102.121 = none
NET-SNMP-MIB::netSnmpObjects.9.1.1.3.15.103.114.112.97.117.116.104.79.110.108.121.85.115.101.114.0.3.2.4.114.101.97.100 = _all_
NET-SNMP-MIB::netSnmpObjects.9.1.1.3.15.103.114.112.97.117.116.104.79.110.108.121.85.115.101.114.0.3.2.5.119.114.105.116.101 = none
NET-SNMP-MIB::netSnmpObjects.9.1.1.3.15.103.114.112.97.117.116.104.79.110.108.121.85.115.101.114.0.3.2.6.110.111.116.105.102.121 = none
NET-SNMP-MIB::netSnmpObjects.9.1.1.3.15.103.114.112.105.110.116.101.114.110.97.108.85.115.101.114.0.3.2.4.114.101.97.100 = _all_
NET-SNMP-MIB::netSnmpObjects.9.1.1.3.15.103.114.112.105.110.116.101.114.110.97.108.85.115.101.114.0.3.2.5.119.114.105.116.101 = none
NET-SNMP-MIB::netSnmpObjects.9.1.1.3.15.103.114.112.105.110.116.101.114.110.97.108.85.115.101.114.0.3.2.6.110.111.116.105.102.121 = none
NET-SNMP-MIB::netSnmpObjects.9.1.1.4.7.103.114.112.100.101.109.111.0.3.3.4.114.101.97.100 = 4
NET-SNMP-MIB::netSnmpObjects.9.1.1.4.7.103.114.112.100.101.109.111.0.3.3.5.119.114.105.116.101 = 4
NET-SNMP-MIB::netSnmpObjects.9.1.1.4.7.103.114.112.100.101.109.111.0.3.3.6.110.111.116.105.102.121 = 4
NET-SNMP-MIB::netSnmpObjects.9.1.1.4.8.103.114.112.99.111.109.109.50.0.0.1.4.114.101.97.100 = 4
NET-SNMP-MIB::netSnmpObjects.9.1.1.4.8.103.114.112.99.111.109.109.50.0.0.1.5.119.114.105.116.101 = 4
NET-SNMP-MIB::netSnmpObjects.9.1.1.4.8.103.114.112.99.111.109.109.50.0.0.1.6.110.111.116.105.102.121 = 4
NET-SNMP-MIB::netSnmpObjects.9.1.1.4.8.103.114.112.99.111.109.109.51.0.0.1.4.114.101.97.100 = 4
NET-SNMP-MIB::netSnmpObjects.9.1.1.4.8.103.114.112.99.111.109.109.51.0.0.1.5.119.114.105.116.101 = 4
NET-SNMP-MIB::netSnmpObjects.9.1.1.4.8.103.114.112.99.111.109.109.51.0.0.1.6.110.111.116.105.102.121 = 4
NET-SNMP-MIB::netSnmpObjects.9.1.1.4.15.103.114.112.97.117.116.104.79.110.108.121.85.115.101.114.0.3.2.4.114.101.97.100 = 4
NET-SNMP-MIB::netSnmpObjects.9.1.1.4.15.103.114.112.97.117.116.104.79.110.108.121.85.115.101.114.0.3.2.5.119.114.105.116.101 = 4
NET-SNMP-MIB::netSnmpObjects.9.1.1.4.15.103.114.112.97.117.116.104.79.110.108.121.85.115.101.114.0.3.2.6.110.111.116.105.102.121 = 4
NET-SNMP-MIB::netSnmpObjects.9.1.1.4.15.103.114.112.105.110.116.101.114.110.97.108.85.115.101.114.0.3.2.4.114.101.97.100 = 4
NET-SNMP-MIB::netSnmpObjects.9.1.1.4.15.103.114.112.105.110.116.101.114.110.97.108.85.115.101.114.0.3.2.5.119.114.105.116.101 = 4
NET-SNMP-MIB::netSnmpObjects.9.1.1.4.15.103.114.112.105.110.116.101.114.110.97.108.85.115.101.114.0.3.2.6.110.111.116.105.102.121 = 4
NET-SNMP-MIB::netSnmpObjects.9.1.1.5.7.103.114.112.100.101.109.111.0.3.3.4.114.101.97.100 = 1
NET-SNMP-MIB::netSnmpObjects.9.1.1.5.7.103.114.112.100.101.109.111.0.3.3.5.119.114.105.116.101 = 1
NET-SNMP-MIB::netSnmpObjects.9.1.1.5.7.103.114.112.100.101.109.111.0.3.3.6.110.111.116.105.102.121 = 1
NET-SNMP-MIB::netSnmpObjects.9.1.1.5.8.103.114.112.99.111.109.109.50.0.0.1.4.114.101.97.100 = 1
NET-SNMP-MIB::netSnmpObjects.9.1.1.5.8.103.114.112.99.111.109.109.50.0.0.1.5.119.114.105.116.101 = 1
NET-SNMP-MIB::netSnmpObjects.9.1.1.5.8.103.114.112.99.111.109.109.50.0.0.1.6.110.111.116.105.102.121 = 1
NET-SNMP-MIB::netSnmpObjects.9.1.1.5.8.103.114.112.99.111.109.109.51.0.0.1.4.114.101.97.100 = 1
NET-SNMP-MIB::netSnmpObjects.9.1.1.5.8.103.114.112.99.111.109.109.51.0.0.1.5.119.114.105.116.101 = 1
NET-SNMP-MIB::netSnmpObjects.9.1.1.5.8.103.114.112.99.111.109.109.51.0.0.1.6.110.111.116.105.102.121 = 1
NET-SNMP-MIB::netSnmpObjects.9.1.1.5.15.103.114.112.97.117.116.104.79.110.108.121.85.115.101.114.0.3.2.4.114.101.97.100 = 1
NET-SNMP-MIB::netSnmpObjects.9.1.1.5.15.103.114.112.97.117.116.104.79.110.108.121.85.115.101.114.0.3.2.5.119.114.105.116.101 = 1
NET-SNMP-MIB::netSnmpObjects.9.1.1.5.15.103.114.112.97.117.116.104.79.110.108.121.85.115.101.114.0.3.2.6.110.111.116.105.102.121 = 1
NET-SNMP-MIB::netSnmpObjects.9.1.1.5.15.103.114.112.105.110.116.101.114.110.97.108.85.115.101.114.0.3.2.4.114.101.97.100 = 1
NET-SNMP-MIB::netSnmpObjects.9.1.1.5.15.103.114.112.105.110.116.101.114.110.97.108.85.115.101.114.0.3.2.5.119.114.105.116.101 = 1
NET-SNMP-MIB::netSnmpObjects.9.1.1.5.15.103.114.112.105.110.116.101.114.110.97.108.85.115.101.114.0.3.2.6.110.111.116.105.102.121 = 1
SNMPv2-MIB::snmpSetSerialNo.0 = 1369298840
SNMP-FRAMEWORK-MIB::snmpEngineID.0 = 0x80001f88805e2e49072f68445700000000
SNMP-FRAMEWORK-MIB::snmpEngineBoots.0 = 255
SNMP-FRAMEWORK-MIB::snmpEngineTime.0 = 69821
SNMP-FRAMEWORK-MIB::snmpEngineMaxMessageSize.0 = 1500
SNMP-MPD-MIB::snmpUnknownSecurityModels.0 = 0
SNMP-MPD-MIB::snmpInvalidMsgs.0 = 0
SNMP-MPD-MIB::snmpUnknownPDUHandlers.0 = 0
SNMP-TARGET-MIB::snmpTargetSpinLock.0 = 0
SNMP-TARGET-MIB::snmpTargetAddrTDomain."internal0" = SNMPv2-TM::snmpUDPDomain
SNMP-TARGET-MIB::snmpTargetAddrTAddress."internal0" = 0x7f00000100a2
SNMP-TARGET-MIB::snmpTargetAddrTimeout."internal0" = 0
SNMP-TARGET-MIB::snmpTargetAddrRetryCount."internal0" = 0
SNMP-TARGET-MIB::snmpTargetAddrTagList."internal0" = internal0
...
有什么办法可以将脚本的输出限制在279行以内?
谢谢
nextCmd()不是执行单个snmpget调用的正确函数。
我发现我必须使用getCmd()
for (errorIndication,
errorStatus,
errorIndex,
varBinds) in getCmd(
SnmpEngine(),
UsmUserData(userName='x', authKey='x', privKey='x'),
UdpTransportTarget(('server', 161)),
ContextData(),
ObjectType(ObjectIdentity('x'))):
if errorIndication:
print(errorIndication)
break
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
break
else:
for varBind in varBinds: