Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用regex解析Pysnmp_Python_Regex_Pysnmp_Python Webbrowser - Fatal编程技术网

Python 用regex解析Pysnmp

Python 用regex解析Pysnmp,python,regex,pysnmp,python-webbrowser,Python,Regex,Pysnmp,Python Webbrowser,我需要解析Pysnmp输出。我想从web浏览器运行脚本。因此,代码如下所示: #! /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 r

我需要解析Pysnmp输出。我想从web浏览器运行脚本。因此,代码如下所示:

#! /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>"
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
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
ae2
xe-4/0/0
etc.
它在web浏览器上生成如下输出:

#! /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>"
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
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
ae2
xe-4/0/0
etc.
但如果我在shell中运行脚本,它会逐行打印,如下所示:

#! /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>"
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
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
ae2
xe-4/0/0
etc.
我想让我的脚本从我的设备上获取数据,该设备工作正常,但我想用regex解析输出,以便它在web浏览器上打印出所有没有点的界面,例如-ae2、xe-4/0/0,并逐行打印每个界面,如下所示:

#! /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>"
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
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
ae2
xe-4/0/0
etc.
我已经在regexr.com上检查了我的正则表达式,它与我想要的行匹配。请把它付诸行动 如有任何意见,将不胜感激

非常感谢

if re.search("(xe|ae)[^.]*$", line):
    print line.split('.')[0] + "\n"

在浏览器中应该可以做到这一点。

可能是regexp引擎与pysnmp值不匹配,因为pysnmp在var绑定中返回的不是一个普通的Python字符串。相反,它是一个pyasn1对象,有时可以安全地将其str()转换为Python字符串,但并不总是如此

要获取所有原始SNMP八位字节字符串数据(在Python2中键入“str”或在Python3中键入“bytes”),可以使用OctetString.asOctets()方法(例如line.asOctets())。这仅适用于八进制字符串值,而不适用于整数或其他SNMP类型