0

我需要解析 Pysnmp 输出。我想从网络浏览器运行脚本。所以代码如下:

#! /usr/bin/python
#
print "Content-Type: text/html\n\n"
print '<html><head><meta content="text/html; charset=UTF-8" />'
print '<title>Interface searcher</title><p>'

import re

from pysnmp.entity.rfc3413.oneliner import cmdgen

cmdGen = cmdgen.CommandGenerator()

errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
    cmdgen.CommunityData('blablablabla'),
    cmdgen.UdpTransportTarget(('x.x.x.x', 161)),
    'iso.3.6.1.2.1.2.2.1.2',
)

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:
                for line in val:
                    if re.search("(xe|ae)[^.]*$", line):
                       print line

print "</p></body></html>"

如果从 Web 浏览器运行此脚本,它不会产生输出。

如果我将最后一个循环更改为:

else:
    for varBindTableRow in varBindTable:
        for name, val in varBindTableRow:
            print(val)

它在 Web 浏览器上产生如下输出:

fxp0 lsi dsc lo0 tap gre ipip pime pimd mtun fxp0.0 lo0.0 em0 em0.0 lo0.16384 lo0.16385 em1 em1.0 cbp0 demux0 irb pip0 pp0 ae0 ae1 ae2 ae3 ae1.0 ae2.109 ae2.110 lsi.0 ae2.196 ae2.234 ae2.236 ae2.311 ae2.313 ae2.314 lsi.1 ae2.452 ae2.507 ae2.557 ae2.649 ae2.735 ae2.803 ae2.804 ae2.977 ae2.1097 ae2.1108 ae2.1194 ae2.1197 ae1 ae2.1306 ae2.1328 ae2.1435 ae2.1463 ae2 ae2.1503 ae2.1596 ae2.2020 ae2.2345 ae2.2465 ae2.32767 ae3.0 ae2.1801 ae2.1446 ae2.2420 ae2.2421 ae2.2422 lc-4/0/0 lc-4/0/0.32769 lc-4/1/0 lc-4/1/0.32769 lc-4/2/0 lc-4/2/0.32769 lc-4/3/0 lc-4/3/0.32769 pfh-4/0/0 pfh-4/0/0.16383 pfe-4/0/0 pfe-4/0/0.16383 pfe-4/1/0 pfe-4/1/0.16383 pfe-4/2/0 pfe-4/2/0.16383 pfe-4/3/0 pfe-4/3/0.16383 lc-5/0/0 lc-5/0/0.32769 lc-5/1/0 lc-5/1/0.32769 lc-5/2/0 lc-5/2/0.32769 lc-5/3/0 lc-5/3/0.32769 pfh-5/0/0 pfh-5/0/0.16383 pfe-5/0/0 pfe-5/0/0.16383 pfe-5/1/0 pfe-5/1/0.16383 pfe-5/2/0 pfe-5/2/0.16383 pfe-5/3/0 pfe-5/3/0.16383 xe-5/0/0 xe-5/0/1 xe-5/0/2 xe-5/0/3 lsi.1048838 xe-5/0/3.0 xe-5/1/0 xe-5/1/1 xe-5/1/2 xe-5/1/3 xe-5/2/0 xe-5/2/1 xe-5/2/2 xe-5/2/3 xe-5/3/0 xe-5/3/1 xe-5/3/2 xe-5/3/3 xe-5/3/3.0 xe-4/0/0 xe-4/0/1 xe-4/0/0.32767 xe-4/0/2 xe-4/0/3 lsi.1048839 xe-4/0/0.2465 lsi.1048840 xe-4/0/0.2422 xe-4/0/0.2421 xe-4/0/0.2420 xe-4/0/0.2345 xe-4/0/0.2020 xe-4/0/0.1801 xe-4/0/0.1596 xe-4/0/0.1503 xe-4/0/0.1463 xe-4/0/0.1446 xe-4/0/0.1435 xe-4/0/0.1328 xe-4/0/0.1306 xe-4/0/0.1293 xe-4/0/0.1197 xe-4/0/0.1194 xe-4/0/0.1108 xe-4/0/0 lsi.1048841

但是如果我从 shell 中运行我的脚本,它会逐行打印如下:

xe-4/1/3.3219
xe-4/2/1.1304
xe-4/2/1.250
gr-5/0/0.11
ae2
ae2.3461
xe-4/0/0.3462
xe-4/0/0.3461
xe-4/0/1
xe-4/0/1.3461
xe-4/0/2.3462
xe-4/0/2.3461
xe-4/0/3.3462
xe-4/0/3.3461
xe-4/2/1.1514
xe-4/2/1.1634

我希望我的脚本从工作正常的设备中获取数据,但我想用正则表达式解析输出,以便它在 Web 浏览器上打印出所有没有点的接口,例如ae2、xe-4/0/ 0并逐行打印每个接口,如下所示:

ae2
xe-4/0/0
etc.

我在 regexr.com 检查了我的正则表达式,它与我想要的行匹配。请在操作中查看它http://regexr.com/3agof 任何输入将不胜感激。

非常感谢

4

2 回答 2

0

Chances are that regexp engine does not match pysnmp values because what pysnmp returns in var-binds is not a plain Python string. Rather it is a pyasn1 object which may sometimes be safely str()'ed into Python string, but not always.

To get all original SNMP octet-string data (typed "str" in Python 2 or "bytes" in Python 3) you could use OctetString.asOctets() method (e.g. line.asOctets()). This only makes sense for OctetString values, not Integers or other SNMP types.

于 2015-02-26T21:03:45.647 回答
0
if re.search("(xe|ae)[^.]*$", line):
    print line.split('.')[0] + "\n"

这应该在浏览器中为您完成。

于 2015-02-26T18:55:35.860 回答