首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PySNMP:动态字段

PySNMP:动态字段
EN

Stack Overflow用户
提问于 2018-02-08 01:40:44
回答 1查看 133关注 0票数 0

我正在尝试使用PySNMP遍历一个表,并且只拉回特定的字段。目前我有以下代码,它工作得很好。

代码语言:javascript
复制
def walkTable(community, hostname, port, tableName, fields):
    for (errorIndication,
         errorStatus,
         errorIndex,
         varBinds) in nextCmd(SnmpEngine(),
                              CommunityData(community, mpModel=0),
                              UdpTransportTarget((hostname, port)),
                              ContextData(),
                              ObjectType(ObjectIdentity(tableName, 'ifAlias')),
                              ObjectType(ObjectIdentity(tableName, 'ifHCInOctets')),
                              ObjectType(ObjectIdentity(tableName, 'ifHCOutOctets')),
                              lexicographicMode=False):

        if errorIndication:
            print(errorIndication)
            break
        elif errorStatus:
            print('%s at %s' % (errorStatus.prettyPrint(),
                                errorIndex and varBinds[int(errorIndex)-1][0] or '?'))
            break
        else:
            for varBind in varBinds:
                fooBar(varBind)

上面的函数接受一个名为field的变量。这是一个字符串数组,我想从表中拉出字段,但我不知道如何动态地执行此操作。

目前我尝试的是

代码语言:javascript
复制
def walkTable(community, hostname, port, tableName, fields):
    for (errorIndication,
         errorStatus,
         errorIndex,
         varBinds) in nextCmd(SnmpEngine(),
                              CommunityData(community, mpModel=0),
                              UdpTransportTarget((hostname, port)),
                              ContextData(),
                              for x in fields:
                                   ObjectType(ObjectIdentity(tableName, x))
                              lexicographicMode=False):

        if errorIndication:
            print(errorIndication)
            break
        elif errorStatus:
            print('%s at %s' % (errorStatus.prettyPrint(),
                                errorIndex and varBinds[int(errorIndex)-1][0] or '?'))
            break
        else:
            for varBind in varBinds:
                fooBar(varBind)

这不管用。有没有办法为nextCmd()动态创建ObjectType?

EN

回答 1

Stack Overflow用户

发布于 2018-02-08 06:28:42

试着这样做:

代码语言:javascript
复制
...
nextCmd(SnmpEngine(),
        CommunityData(community, mpModel=0),
        UdpTransportTarget((hostname, port)),
        ContextData(),
        *[ObjectType(ObjectIdentity(tableName, field)) for field in fields])
...

挑剔:如果您想要更高的性能,请保留持久的SnmpEngine实例,并将其重用于所有的SNMP调用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48670140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档