【发布时间】:2015-05-27 17:35:10
【问题描述】:
总之,
我正在利用 pysnmp 获取多个值以创建包含路由表信息的 .CSV。
当我在 OID:.1.3.6.1.2.1.4.20.1.3 上执行 SNMPWALK 时,我得到:
IP-MIB::ipAdEntNetMask.10.30.0.0 = IpAddress: 255.255.255.254
IP-MIB::ipAdEntNetMask.10.30.0.2 = IpAddress: 255.255.255.254
IP-MIB::ipAdEntNetMask.10.30.0.14 = IpAddress: 255.255.255.254
IP-MIB::ipAdEntNetMask.10.30.0.18 = IpAddress: 255.255.255.254
IP-MIB::ipAdEntNetMask.10.30.0.22 = IpAddress: 255.255.255.254
IP-MIB::ipAdEntNetMask.10.30.0.26 = IpAddress: 255.255.255.254
IP-MIB::ipAdEntNetMask.10.30.0.30 = IpAddress: 255.255.255.254
IP-MIB::ipAdEntNetMask.10.30.0.32 = IpAddress: 255.255.255.254
IP-MIB::ipAdEntNetMask.10.30.0.65 = IpAddress: 255.255.255.248
IP-MIB::ipAdEntNetMask.10.30.0.97 = IpAddress: 255.255.255.248
IP-MIB::ipAdEntNetMask.10.30.0.128 = IpAddress: 255.255.255.255
IP-MIB::ipAdEntNetMask.127.0.0.50 = IpAddress: 255.0.0.0
在我的 pysnmp 脚本中使用相同的方法时,我得到以下输出:
-> snmpNetMAsk = (snmpDictClean(snmpNetMAsk, 10))
(Pdb) pprint.pprint(snmpNetMAsk)
[[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.3.10.30.0.0)),
IpAddress(hexValue='fffffffe'))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.3.10.30.0.2)),
IpAddress(hexValue='fffffffe'))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.3.10.30.0.14)),
IpAddress(hexValue='fffffffe'))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.3.10.30.0.18)),
IpAddress(hexValue='fffffffe'))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.3.10.30.0.22)),
IpAddress(hexValue='fffffffe'))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.3.10.30.0.26)),
IpAddress(hexValue='fffffffe'))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.3.10.30.0.30)),
IpAddress(hexValue='fffffffe'))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.3.10.30.0.32)),
IpAddress(hexValue='fffffffe'))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.3.10.30.0.65)),
IpAddress(hexValue='fffffff8'))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.3.10.30.0.97)),
IpAddress(hexValue='fffffff8'))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.3.10.30.0.128)),
IpAddress(hexValue='ffffffff'))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.3.127.0.0.50)),
IpAddress(hexValue='ff000000'))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.4.10.30.0.0)), Integer(1))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.4.10.30.0.2)), Integer(1))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.4.10.30.0.14)), Integer(1))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.4.10.30.0.18)), Integer(1))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.4.10.30.0.22)), Integer(1))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.4.10.30.0.26)), Integer(1))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.4.10.30.0.30)), Integer(1))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.4.10.30.0.32)), Integer(1))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.4.10.30.0.65)), Integer(1))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.4.10.30.0.97)), Integer(1))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.4.10.30.0.128)), Integer(1))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.4.127.0.0.50)), Integer(1))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.5.10.30.0.0)), Integer(18024))]]
如您所见,我所有的值都是 HEX 格式,然后复制到某个 Interger 中。我不完全理解这里发生了什么。在终端中运行 snmpwalk 并将其捕获到文件中,然后使用 python 脚本编辑文件似乎更容易。如果有办法使用 pysnnmp 维护字典中的值,我宁愿这样做。我只是在自己搜索了几天后才问这个。感谢您的帮助!
编辑:显示我正在使用的脚本的完整版本。
#!/usr/bin/python
import pprint
import pysnmp
from snmp_helper import *
def snmpDictClean(input_list, input_num):
output_dict = dict()
for varBindTableRow in input_list:
for name, val in varBindTableRow:
name = str(name)
val = str(val)
splitKey = name.split('.')
del splitKey[0:input_num]
splitKey = '.'.join(splitKey)
output_dict[splitKey] = val
return output_dict
def main():
ipCidrRouteIfIndex = '.1.3.6.1.2.1.4.24.4.1.5'
ifDescr = '.1.3.6.1.2.1.2.2.1.2'
ipAdEntIfIndex = '.1.3.6.1.2.1.4.20.1.2'
ipAdEntNetMask = '.1.3.6.1.2.1.4.20.1.3'
# SNMPv3 Connection Parameters
a_user = 'USER'
auth_key = 'AUTH_KEY'
encrypt_key = 'ENCRYPT_KEY'
snmp_user = (a_user, auth_key, encrypt_key)
ipAddr = [
'host1',
'host2',
]
for hlist in ipAddr:
snmpDevice = (hlist, 161)
snmpRoute = snmp_bulk_oid_v3(snmpDevice, snmp_user, oid=ipCidrRouteIfIndex)
snmpRoute = (snmpDictClean(snmpRoute, 11))
snmpIfDescr = snmp_bulk_oid_v3(snmpDevice, snmp_user, oid=ifDescr)
snmpIfDescr = (snmpDictClean(snmpIfDescr, 10))
snmpIpIndex = snmp_bulk_oid_v3(snmpDevice, snmp_user, oid=ipAdEntIfIndex)
snmpIpIndex = (snmpDictClean(snmpIpIndex, 10))
snmpNetMAsk = snmp_bulk_oid_v3(snmpDevice, snmp_user, oid=ipAdEntNetMask)
snmpNetMAsk = (snmpDictClean(snmpNetMAsk, 10))
import pdb; pdb.set_trace()
if __name__ == '__main__':
main()
我正在使用来自@ktbyers:https://github.com/ktbyers/pynet/tree/master/snmp 的 snmp_helper。我将其修改为使用 snmpbulk 而不是 get。
# Python
from __future__ import print_function
from pysnmp.entity.rfc3413.oneliner import cmdgen
import pprint
def snmp_bulk_oid_v3(snmp_device, snmp_user, oid='', auth_proto='sha',
encrypt_proto='des', display_errors=True):
# unpack snmp_user
a_user, auth_key, encrypt_key = snmp_user
auth_proto_map = {
'sha': cmdgen.usmHMACSHAAuthProtocol,
'md5': cmdgen.usmHMACMD5AuthProtocol,
'none': cmdgen.usmNoAuthProtocol
}
if auth_proto in auth_proto_map.keys():
auth_protocol = auth_proto_map[auth_proto]
else:
raise ValueError("Invalid authentication protocol specified: %s" % auth_proto)
encrypt_proto_map = {
'des': cmdgen.usmDESPrivProtocol,
'3des': cmdgen.usm3DESEDEPrivProtocol,
'aes128': cmdgen.usmAesCfb128Protocol,
'aes192': cmdgen.usmAesCfb192Protocol,
'aes256': cmdgen.usmAesCfb256Protocol,
'none': cmdgen.usmNoPrivProtocol,
}
if encrypt_proto in encrypt_proto_map.keys():
encrypt_protocol = encrypt_proto_map[encrypt_proto]
else:
raise ValueError("Invalid encryption protocol specified: %s" % encrypt_proto)
# Create a PYSNMP cmdgen object
cmdGen = cmdgen.CommandGenerator()
(errorIndication, errorStatus, errorIndex, varBindTable) = cmdGen.bulkCmd(
cmdgen.UsmUserData(a_user, auth_key, encrypt_key,
authProtocol=auth_protocol,
privProtocol=encrypt_protocol, ),
cmdgen.UdpTransportTarget(snmp_device),
0,
25,
oid,
lookupNames=True, lookupValues=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:
snmp_data = print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
'''
return varBindTable
【问题讨论】:
-
请提供一个简短、完整的程序来演示您遇到的错误。请参阅stackoverflow.com/help/mcve 了解更多信息。