我是 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,但我还没有弄清楚如何使用它。