[英]How to read the string from an .INI file and then read that string from a text file and print the entire line using Python?
[英]read line from match string in file .. python
我有一个从F5配置文件打印池名称的脚本。我想在多个F5配置文件上运行此脚本。.我希望能够从^ pool | ^}中读取(pool OR}使行开始)。 ..目前,我告诉脚本从第2348行读取时,我得到了所需的输出。
我想消除xrange(2348)中i的使用,因为其他文件F5配置文件较小。
它的工作方式是,如果我让脚本开始从^ pool OR ^}逐行读取文件
当前脚本:
import re
seenrule = 'false'
File = open("/home/t816874/F5conf/unzip/config/bigip.conf", "r")
for i in xrange(2348):
File.next()
for line in File:
if re.match("(^pool p)", line, re.IGNORECASE):
a = line.lstrip("pool p")
ONE = a[:-2]
print "Pool Name:", ONE
if re.findall(r'(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d] {1,3})', line):
if seenrule == 'false':
x = line.lstrip("members ")
print x
if re.match("(^rule )", line):
seenrule = 'true'
我的尝试:
import re
seenrule = 'false'
startline = 'false'
File = open("/home/t816874/F5conf/unzip/config/bigip.conf", "r")
if re.match("(^pool|^})", line):
startline = 'true'
for line in File:
if re.match("(^pool p)", line, re.IGNORECASE):
a = line.lstrip("pool p")
ONE = a[:-2]
print "Pool Name:", ONE
if re.findall(r'(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d] {1,3})', line):
if seenrule == 'false':
x = line.lstrip("members ")
print x
if re.match("(^rule )", line):
seenrule = 'true'
感谢您的帮助!
conf文件类似于:
node 172.0.0.0 {
screen ab2583-1.2
}
node 172.0.0.1 {
screen ab2583-1.3
}
node 172.0.0.3 {
screen ab2584-1.2
}
pool pWWW_abcd_HTTPS {
lb method member predictive
monitor all mWWW_staging.telecom_TCP
members {
0.0.0.0:8421 {}
0.0.0.1:18431 {
session user disabled
}
0.0.0.2:8421 {}
0.0.0.3:18431 {
session user disabled
}
}
}
pool pWWW_vcm2APP.defg_HTTP {
lb method member predictive
monitor all mWWW_vcm2APP.defg_TCP
members 0.0.0.5:27110 {}
}
node..
....
.
目前尚不清楚您想要什么,但我会尽力猜测。 您正在尝试获取每个池的成员。 您没有提及任何关于seenrule
-因此我将其删除。
如果您不想解析此数据结构,并且如果顶层块始终且排他地以'}\\n'
行结尾,则可以执行以下操作:
import re
f = open('/home/t816874/F5conf/unzip/config/bigip.conf', 'r')
# Iterate over lines.
for line in f:
# If pool block is met.
if line.startswith('pool p'):
pname = line.split()[1]
pmembers = []
# Iterate over block.
# Block is expected to be terminated by line '}\n'.
for pline in iter(lambda: next(f), '}\n'):
members = re.findall(r'\d{1,3}(?:\.\d{1,3}){3}:\d{1,5}', pline)
pmembers.extend(members)
print(pname, pmembers)
f.close()
输出:
pWWW_abcd_HTTPS ['0.0.0.0:8421', '0.0.0.1:18431', '0.0.0.2:8421', '0.0.0.3:18431']
pWWW_vcm2APP.defg_HTTP ['0.0.0.5:27110']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.