我正在尝试审核大量 f5 配置,但我很难解析下面的数据,我尝试修改下面的代码,但它抛出了一个错误。我是 python 的完全菜鸟,这是我第一次自动化这样的任务。谢谢
数据:
Ltm::Virtual Server: acme.com
Availability : offline
State : enabled
Reason : The children pool member(s) are down
Destination : 10.1.1.2:80
Ltm::Virtual Server: foo.com
Availability : available
State : enabled
Reason : The virtual server is available
Destination : 10.100.11.15:80
Ltm::Virtual Server: hamhamspam.com
Availability : offline
State : enabled
Reason : The children pool member(s) are down
Destination : 10.200.8.17:443
预期产出
Virtual Server Availability State Reason Destination
acme.com offline enabled The children pool member(s) are down 10.1.1.2:80
foo.com available enabled The virtual server is available 10.100.11.15:80
hamhamspam.com offline enabled The children pool member(s) are down 10.200.8.17:443
import csv
def convert_to_dict(line, header):
d = {}
for cell in header:
d[cell] = ''
row = line.strip().split(':')
for cell in row:
if cell:
key, value = cell.split(':')
d[key] = value
return d
def extract_fields(logfile):
fields = set()
for line in logfile:
row = line.strip().split(':')
for cell in row:
if cell:
key, value = cell.split(':')
fields.add(key)
logfile.seek(0)
return sorted(list(fields))
if __name__ == '__main__':
with open('ltm.txt', 'r') as logfile:
with open('report.csv', 'wb') as csvfile:
csvwriter = csv.writer(csvfile)
header = extract_fields(logfile)
csvwriter.writerow(header)
for line in logfile:
d = convert_to_dict(line, header)
csvwriter.writerow([d[cell] for cell in header])
慕运维8079593
喵喵时光机
相关分类