我有一个文本文件,其中包含以下信息:
interfaces {
ge-2/0/0 {
description "site1;;hostname1;ge-16/0/9;;;TRUST;";
unit 0 {
family ethernet-switching {
port-mode trunk;
}
}
}
ge-2/0/2 {
description "site2;;hostname2;ge-16/0/8;;;TRUST;";
unit 0 {
family ethernet-switching {
port-mode trunk;
}
}
}在其他人的帮助下,我已经能够提取接口id (ge-2/0/0)以及描述。
代码如下:
from ciscoconfparse import CiscoConfParse
parse = CiscoConfParse("testconfig.txt", syntax="junos")
intfs = parse.find_objects_w_parents(r'^interfaces', r'^\s+ge-')
for intfobj in intfs:
intf_name = intfobj.text.strip()
descr = intfobj.re_match_iter_typed(r'description\s+"(\S.+?)"$', group=1)
print ('Intf: {0}, {1}'.format(intf_name, descr))这给了我一个结果:
Intf: ge-2/0/0, site1;;hostname1;ge-16/0/9;;;TRUST;
Intf: ge-2/0/2, site2;;hostname2;ge-16/0/8;;;TRUST;到目前为止,这对我来说是巨大的,我真的认为我将能够弄清楚如何更深入地挖掘界面来提取"port-mode“。
到目前为止,我的尝试都失败了。
这是我试图挖掘出这些信息的总体思路,但无济于事:
ltype = parse.find_objects_w_parents(r'^interfaces', r'^\s+ge-', r'^unit\s0', r'^family\sethernet-switching')
for ltypeobj in ltype:
pmode = intfobj.re_match_iter_typed(r'port-mode\s+"(\S.+?)"$', group=1)
print ('Port Mode: {0}'.format(pmode))我得到了以下内容,但我就是不能弄明白。
Traceback (most recent call last):
File "convert.py", line 11, in <module>
ltype = parse.find_objects_w_parents(r'^interfaces', r'^\s+ge-', r'^unit\s0', r'^family\sethernet-switching')
TypeError: find_objects_w_parents() takes from 3 to 4 positional arguments but 5 were given任何关于实现这一目标的建议都将不胜感激。
发布于 2018-09-20 18:21:32
检查此示例是否适用于您
import re
for i in re.findall('(ge-\d/\d/\d).*\n\s+description\s+(.*)(\n\s+.*){2}\n\s+port-mode\s(.*);', x):
print 'interface: ', i[0]
print 'description: ', i[1]
print 'port mode: ', i[-1]输出:
interface: ge-2/0/0
description: "site1;;hostname1;ge-16/0/9;;;TRUST;";
port mode: trunk
interface: ge-2/0/2
description: "site2;;hostname2;ge-16/0/8;;;TRUST;";
port mode: trunkhttps://stackoverflow.com/questions/52413186
复制相似问题