我对pysnmp和snmp都很陌生,我试图让一个简单的脚本从网络上的两台路由器(Airport Extreme和Tomato Firmware路由器)转储统计信息。
此代码(来自在线示例)有效,但没有友好名称:
from pysnmp.entity.rfc3413.oneliner import cmdgen
cmdGen = cmdgen.CommandGenerator()
errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
cmdgen.CommunityData('public'),
cmdgen.UdpTransportTarget(('router', 161)),
cmdgen.MibVariable('IF-MIB', '').loadMibs(),
lexicographicMode=True, maxRows=100,
ignoreNonIncreasingOid=True
)
if errorIndication:
print(errorIndication)
else:
if errorStatus:
print('%s at %s' % (
errorStatus.prettyPrint(),
errorIndex and varBindTable[-1][int(errorIndex)-1] or '?'
)
)
else:
for varBindTableRow in varBindTable:
for name, val in varBindTableRow:
print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
生成此输出:
python foo.py
1.3.6.1.2.1.2.1.0 = 8
1.3.6.1.2.1.2.2.1.1.1 = 1
1.3.6.1.2.1.2.2.1.1.2 = 2
....
我希望产生类似snmpwalk的输出:
snmpwalk router -c public -v2c
SNMPv2-MIB::sysDescr.0 = STRING: Linux router 2.6.22.19 #20 Tue Apr 2 13:54:22 ICT 2013 mips
SNMPv2-MIB::sysObjectID.0 = OID: NET-SNMP-MIB::netSnmpAgentOIDs.10
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (55888889) 6 days, 11:14:48.89
....
我认为这只是让MIB正常使用的问题。我安装了pysnmp-mibs,但我还没弄明白如何使用它。