Python 2.7 格式化多行PYsnmp 4.4打印输出到Python 2.7的行和列
我是python新手,希望在将打印输出格式化为行和列方面获得一些帮助。这些数据最终将被发送到csv文件 该脚本将从多个主机获取数据。行数以及接口名称和描述的长度是可变的 当前输出如下所示:Python 2.7 格式化多行PYsnmp 4.4打印输出到Python 2.7的行和列,python-2.7,csv,pretty-print,pysnmp,Python 2.7,Csv,Pretty Print,Pysnmp,我是python新手,希望在将打印输出格式化为行和列方面获得一些帮助。这些数据最终将被发送到csv文件 该脚本将从多个主机获取数据。行数以及接口名称和描述的长度是可变的 当前输出如下所示: hostname IF-MIB::ifDescr.1 = GigabitEthernet0/0/0<br/> hostname IF-MIB::ifAlias.1 = --> InterfaceDesc<br/> hostname IF-MIB::ifOperStatus.1 =
hostname IF-MIB::ifDescr.1 = GigabitEthernet0/0/0<br/>
hostname IF-MIB::ifAlias.1 = --> InterfaceDesc<br/>
hostname IF-MIB::ifOperStatus.1 = 'up'<br/>
hostname IF-MIB::ifDescr.2 = GigabitEthernet0/0/1<br/>
hostname IF-MIB::ifAlias.2 = --> InterfaceDesc<br/>
hostname IF-MIB::ifOperStatus.2 = 'up'<br/>
hostname IF-MIB::ifDescr.3 = GigabitEthernet0/0/2<br/>
hostname IF-MIB::ifAlias.3 = --> InterfaceDesc<br/>
hostname IF-MIB::ifOperStatus.3 = 'up'<br/>
我现在的打印代码在这里。我想保留打印语句中的错误
for errorIndication, errorStatus, errorIndex, varBinds in snmp_iter:
# Check for errors and print out results
if errorIndication:
print(errorIndication)
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
else:
for varBind in varBinds:
print(hostip),
print(' = '.join([x.prettyPrint() for x in varBind]))
完整脚本:
from pysnmp.hlapi import *
routers = ["router1"]
#adds routers to bulkCmd
def snmpquery (hostip):
snmp_iter = bulkCmd(SnmpEngine(),
CommunityData('Community'),
UdpTransportTarget((hostip, 161)),
ContextData(),
0, 50, # fetch up to 50 OIDs
ObjectType(ObjectIdentity('IF-MIB', 'ifDescr')),
ObjectType(ObjectIdentity('IF-MIB', 'ifAlias')),
ObjectType(ObjectIdentity('IF-MIB', 'ifOperStatus')),
lexicographicMode=False) # End bulk request once outside of OID child objects
for errorIndication, errorStatus, errorIndex, varBinds in snmp_iter:
# Check for errors and print out results
if errorIndication:
print(errorIndication)
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
else:
for rowId, varBind in enumerate(varBindTable):
oid, value = varBind
print('%20.20s' % value)
if not rowId and rowId % 3 == 0:
print('\n')
# calls snmpquery for all routers in list
for router in routers:
snmpquery(router)
非常感谢您提供的任何帮助。
谢谢 假设使用三个snmp表列初始化了
snmp\u iter
:
snmp_iter = bulkCmd(SnmpEngine(),
UsmUserData('usr-md5-des', 'authkey1', 'privkey1'),
Udp6TransportTarget(('demo.snmplabs.com', 161)),
ContextData(),
0, 25,
ObjectType(ObjectIdentity('IF-MIB', 'ifDescr')),
ObjectType(ObjectIdentity('IF-MIB', 'ifAlias')),
ObjectType(ObjectIdentity('IF-MIB', 'ifOperStatus')))
您可以确保(对于GETNEXT和GETBULK命令),pysnmp总是以逐行方式返回矩形表
知道您请求的列数(3)您应该能够逐行打印输出:
for rowId, varBind in enumerate(varBindTable):
oid, value = varBind
print('%20.20s' % value)
if not rowId and rowId % 3 == 0:
print('\n')
谢谢IIya,我应该定义varBindTable并给它分配一个varBinds值吗?点击send to尽快。是的,snmp_inter由三个snmp表初始化。我添加了完整的脚本
for rowId, varBind in enumerate(varBindTable):
oid, value = varBind
print('%20.20s' % value)
if not rowId and rowId % 3 == 0:
print('\n')