【问题标题】:Formatting multiple lines of PYsnmp 4.4 print output to rows and columns Python 2.7将多行 PYsnmp 4.4 打印输出格式化为 Python 2.7 的行和列
【发布时间】:2018-05-22 03:36:03
【问题描述】:

我是 python 新手,正在寻找有关将打印输出格式化为行和列的帮助。这些数据最终会被发送到 csv 文件中。

脚本将从多个主机获取数据。行数是可变的,接口名称和描述的长度也是可变的。

目前的输出如下所示:

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/>

我正在尝试将其格式化为以下行和列以及每行的标题(主机名、接口、接口描述和状态)。

hostname        interface              interface desc       status
hostname        GigabitEthernet0/0/0   InterfaceDesc        up
hostname        GigabitEthernet0/0/1   InterfaceDesc        up
hostname        GigabitEthernet0/0/2   InterfaceDesc        up

我目前拥有的打印代码在这里。我想保留打印语句以防出错。

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)

非常感谢您提供的任何帮助。 谢谢!

【问题讨论】:

    标签: python-2.7 csv pretty-print pysnmp


    【解决方案1】:

    假设snmp_iter 初始化为三个 SNMP 表列:

    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')))
    

    您可以确定(对于 GETNEXTGETBULK 命令)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 的值吗?
    • 点击发送到很快。是的 snmp_inter 使用三个 SNMP 表进行初始化。我添加了完整的脚本
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多