来自fi的python审核行

2024-06-09 10:57:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试审核一个文件并存储每个部分的结果,以下是文件示例:

-------------- Task 1 -------------- Device Name: hostname_A
Status: Completed
Results: Action completed successfully...

****** Enable Mode Results ******
hostname_A#show run | in abcd
abcd RO 1
hostname_A#show ip access-list 1
Standard IP access list 1
    10 permit 1.1.1.1
    20 permit 2.2.2.2
    30 permit 3.3.3.3

-------------- Task 2 -------------- Device Name: hostname_B
Status: Completed
Results: Action completed successfully...

****** Enable Mode Results ******
hostname_A#show run | in abcd
hostname_A#show ip access-list 1
Standard IP access list 1
     10 permit 1.1.1.1
     20 permit 2.2.2.2

-------------- Task 3-------------- Device Name: hostname_C
And so on

下面是我迄今为止创建的代码,但仍在努力工作:

^{pr2}$

结果如下:

hostname_A, community=Passed, ACL=Passed; 
hostname_B, community=Failed, ACL=Failed; 
hostname_C, community=Passed, ACL=Failed;
and so on

这里有很多'--Task---'部分,其思想是检查每个“设备名”的结果并将其存储在另一个文件中。我正在努力找到一个更好的工作方式。有什么建议吗?谢谢您!在


Tags: 文件namecommunitytaskaccessdeviceshowresults
2条回答

我想你想要更像这样的东西:

line = infile.readline()
while ' - Task' not in line: #find the first task (assuming it's not always the first line, ie some random '\n' lines in the beginning
    line = infile.readline()

deviceDict = {}
#parse the first line however you want
field_order = ('hostname', 'community', 'ACL')
#now you can jump into the for loop
for line in infile:
    taskblock = ''
    try: #keep while loop in a try block, explained below
        while ' - Task' not in line:
             taskblock = taskblock + line
             line = next(infile)

    #you can now parse taskblock as a string any way you want

    for i in field_order: #this is where you output, assuming you parsed aboved
            outfile.write(i + ' = ' + deviceDict[i])
            outfile.write(',' if i != 'ACL' else ';\n') #'ACL' can be changed to whatever your last element is
    deviceDict = {}
    if ' - Task' in line: #this is necessary because if next(infile) is called in the end of file, line == whatever the last line was since there are no more tasks
        #this is where you can parse the task line again (use the same code as before)

这是一个供您使用的框架(大量的伪代码)。注释是可以放入特定代码的地方。while循环的作用是不断向taskblock添加行,直到它到达下一个任务行。因为文件对象是迭代器,所以我们可以对其调用next(infile)来获得下一行并进行迭代。我们需要在try块内执行while循环,因为如果在文件末尾调用next(f),它将引发StopIteration错误。您还可以执行以下操作:

^{pr2}$

它检查infile.readline()是否有效(如果到达行尾,它将返回空),但是如果txt文件中的任何其他行是空的,那么这可能会导致问题。在

至于您的if '2.2.2.2' and '3.3.3.3' in each_line_in_Task_Block:行,正如它在注释中所说的,它不会返回您期望的结果,您可以使用

if all(permit in taskblock for permit in ['2.2.2.2','3.3.3.3']):

达到同样的效果。在

我希望你觉得这个有用!在

import re

device_dict = {}

with open("file_to_be_audited.txt", "r") as infile:
  with open ('audited_file.txt', "w") as outfile:
    for line in infile:
        if "Device Name" in line:
            device_dict['hostname'] = re.search(r'Device Name: (.+)', line).group(0)
            outfile.write("\n") 
            outfile.write(device_dict['hostname'] + "\n") 
            #tried to have a cleaner code storing the dictionaries values and saving it all once at the end to the file, but didn´t get it working and as a solution, saving it at each item

        if "abcd RO 1" in line:
            device_dict['community'] = 'Passed'
            outfile.write('Community: ' + device_dict['community'] + "\n")
            # the same for all the other dicitonaries items, saving it at each time instead of the endb of the code

        if "Standard IP access list 1" in line:
            acl_lines = ' '
            while True:
                line = next(infile)
                acl_lines = acl_lines + line 
                if 'permit' not in line:
                    break
            if all (permit in acl_lines for permit in ['2.2.2.2', '3.3.3.3']):
                device_dict['ACL'] = 'Passed'
                outfile.write('ACL 1    : ' + device_dict['ACL'] + "\n")    
            else:
                device_dict['ACL'] = 'Failed'
                outfile.write('ACL 1    : ' + device_dict['ACL'] + "\n")    

下面是这个代码的输出文件:

^{pr2}$

相关问题 更多 >