python:通过路由器配置搜索存在的某些行
我有一个路由器配置文件。路由器有数千个“接口”,我试图确保它在每个接口配置部分都有两行。典型的路由器配置文件如下所示:python:通过路由器配置搜索存在的某些行,python,regex,search,Python,Regex,Search,我有一个路由器配置文件。路由器有数千个“接口”,我试图确保它在每个接口配置部分都有两行。典型的路由器配置文件如下所示: <whitespaces> Interface_1 <whitespaces> description Interface_1 <whitespaces> etc etc' <whitespaces> etc etc etc <whitespaces> <config line that
<whitespaces> Interface_1
<whitespaces> description Interface_1
<whitespaces> etc etc'
<whitespaces> etc etc etc
<whitespaces> <config line that i am searching to confirm is present>
<whitespaces> etc etc etc etc
!
!
! random number of router lines
<whitespaces> <second config line that i am searching to confirm is
present>
!
<whitespaces> Interface_2
<whitespaces> description Interface_2
<whitespaces> etc etc
<whitespaces> etc etc etc
<whitespaces> <config line that I am searching to confirm is present>
<whitespaces> etc etc etc etc
! random number of router lines
<whitespaces> <second config line that i am searching to confirm is
present>
etc
垂死的家伙。这是我第一次向这个委员会发帖。我会接受任何人的想法/逻辑流程/陈述/想法。我经常遇到这种类型的搜索情况,我不知道路由器配置中缺少什么,但我知道它必须在点A和点B之间的某个地方,但没有其他东西可以“抓住”。。。。
'''
“配置文件不会太大。概念是通过配置文件进行循环搜索,使用搜索逻辑,但每个文件都不是太大。假设这个问题的配置文件很小,但我明白你的意思 示例配置与此类似:(今晚我将发布示例) 接口1 线 线 线 ip地址x.x.x.x 线 线 Dhcp y.y.y.y 线 ! ! 接口2 线 线 线 ip地址z.z.z.z 线 线 Dhcp y.y.y.y 线 ! ! 但是它的静态配置,我的“行”可以是30行或者10行,或者其他的。但我要找的两个词是:“ip地址”和“dhcp”,后面加上什么,这两个词是独一无二的
基本上,我正在检查以确保配置中同时配置了ip地址和dhcp 我将根据您掌握的信息进行尝试。大致的想法如下
^[\s]*(接口\ud)
不确定您的文件实际上是什么样子,但如果实际上是空白,您可以使用itertools.groupby
通过Interface\uuu
对行进行剥离和分组:
lines = ["<second config line that i am searching to confirm is present>"
,"<config line that i am searching to confirm is present>"]
from itertools import groupby
with open("out.txt") as f:
d = {}
groups = groupby(f, lambda x: x.strip().startswith("Interface_"))
for k, v in groups:
if k:
key = list(v)[0].strip()
data = " ".join(next(groups, " ")[1])
d[key] = all(l in data for l in lines)
print(d)
或将正则表达式传递给groupby:
lines = ["<second config line that i am searching to confirm is present>"
,"<config line that i am searching to confirm is present>"]
from itertools import groupby
import re
with open("out.txt") as f:
d = {}
groups = groupby(f, lambda x: re.search("<whitespaces>\s+Interface_\d+",x))
for k, v in groups:
if k:
key = list(v)[0].strip().split()[1]
data = " ".join(next(groups, " ")[1])
d[key] = all(l in data for l in lines)
print(d)
一旦您有东西要传递给groupby,它会将文件分割成多个部分,您还可以用编译的正则表达式搜索替换
搜索中的
import re
regs = [re.compile("<second config line that i am searching to confirm is present>")
,re.compile("<config line that i am searching to confirm is present>")]
from itertools import groupby
with open("out.txt") as f:
d = {}
groups = groupby(f, lambda x: x.startswith("<whitespaces> Interface_"))
for k, v in groups:
if k:
key = list(v)[0].strip().split()[1]
data = " ".join(next(groups, " ")[1])
d[key] = all(reg.search(data) for reg in regs)
这不会一次将所有数据读入内存,因此如果您有数百万行,它将非常有效。将文件读入行列表
with open('C:\\PYTHON\\router.txt') as config:
lines = config.readlines()
去掉空白
lines = [ln.strip() for ln in lines]
按接口名称获取字典中接口行的索引
interfaces = {name: num for num, name in enumerate(lines)
if name.startswith('Interface_')}
检查您想要的行是否存在
for name in interfaces.keys():
idx = interfaces[name]+2 # Shouldn't this be 3 to skip the description?
if not 'config item string' in lines[idx]:
print name, 'does not have the right config!'
您的整个路由器配置1M/2M/100M有多大?我问这个问题的原因是——我想知道我是否可以将这些线保留在内存中1M/2M。我可能想考虑100M的选项,这个选项是毫无疑问的。你应该发布你的文件的真实摘录。我不认为张贴配置文件是一个选项:-这是一个非常敏感的信息。那些是空白吗?
{'Interface_2': True, 'Interface_1': True}
import re
regs = [re.compile("<second config line that i am searching to confirm is present>")
,re.compile("<config line that i am searching to confirm is present>")]
from itertools import groupby
with open("out.txt") as f:
d = {}
groups = groupby(f, lambda x: x.startswith("<whitespaces> Interface_"))
for k, v in groups:
if k:
key = list(v)[0].strip().split()[1]
data = " ".join(next(groups, " ")[1])
d[key] = all(reg.search(data) for reg in regs)
groups = groupby(f, lambda x: x.startswith("<whitespaces> Interface_"))
with open('C:\\PYTHON\\router.txt') as config:
lines = config.readlines()
lines = [ln.strip() for ln in lines]
interfaces = {name: num for num, name in enumerate(lines)
if name.startswith('Interface_')}
for name in interfaces.keys():
idx = interfaces[name]+2 # Shouldn't this be 3 to skip the description?
if not 'config item string' in lines[idx]:
print name, 'does not have the right config!'