处理防火墙VIP配置列表的Python代码及比较时不忽略前导空格的问询
Extracting Nested Firewall VIP Configuration Blocks in Python
When working with nested Fortinet-style configuration blocks (like your VIP example), using simple index() calls to find closing end or next lines falls apart quickly—nested sub-blocks (like config realservers) will throw off boundary detection. The key here is to respect leading spaces to track indentation depth, which lets us correctly map the start and end of each VIP block.
Here's a complete, corrected version of your code that handles nested structures and preserves whitespace:
confCompleta = ['config firewall vip\n', ' edit "VIP"\n', ' config realservers\n', ' edit 1\n', ' set ip \n', ' next\n', ' end\n', ' next\n', 'end\n'] # Step 1: Correctly extract the entire top-level "config firewall vip" block posIni = confCompleta.index('config firewall vip\n') current_depth = len(confCompleta[posIni]) - len(confCompleta[posIni].lstrip()) posFim = posIni for i in range(posIni + 1, len(confCompleta)): line = confCompleta[i] line_depth = len(line) - len(line.lstrip()) # We've found the top-level closing "end" when depth matches initial and line is "end" if line_depth == current_depth and line.strip() == 'end': posFim = i + 1 break # Update depth if we enter a nested block elif line_depth > current_depth: current_depth = line_depth blocoVip = confCompleta[posIni:posFim] # Step 2: Find start and end indices for each VIP edit block indiceInicialVip = [i for i, s in enumerate(blocoVip) if ' edit ' in s] indiceFinalVip = [] for ini_idx in indiceInicialVip: initial_line = blocoVip[ini_idx] initial_depth = len(initial_line) - len(initial_line.lstrip()) current_depth = initial_depth final_idx = ini_idx + 1 while final_idx < len(blocoVip): line = blocoVip[final_idx] line_depth = len(line) - len(line.lstrip()) # Adjust depth when exiting a nested block if line_depth < current_depth: current_depth = line_depth # Stop when we hit "next" at the same depth as the initial edit line if current_depth == initial_depth and line.strip() == 'next': break # Adjust depth when entering a nested block elif line_depth > current_depth: current_depth = line_depth final_idx += 1 indiceFinalVip.append(final_idx) # Step 3: Extract each individual VIP block vip_blocks = [blocoVip[ini:final+1] for ini, final in zip(indiceInicialVip, indiceFinalVip)] # Optional: Print the results to verify for idx, block in enumerate(vip_blocks): print(f"VIP Block {idx+1}:") for line in block: print(line, end='')
Key Improvements:
- Top-level Block Extraction: Instead of grabbing the first
endline, we track indentation depth to find the correct closingendfor the entireconfig firewall vipsection. - VIP Edit Boundary Detection: For each
editline, we follow indentation levels to navigate nested blocks. We stop when we hit anextline at the same depth as the originaledit—this ensures we capture the full VIP entry including any nested sub-configs. - Whitespace Respect: By calculating
line_depthas the count of leading spaces, we never ignore whitespace, which is critical for correctly parsing nested config structures.
Expected Output:
When you run this code, you'll get the full VIP block including its nested realservers config:
VIP Block 1: edit "VIP" config realservers edit 1 set ip next end next
内容的提问来源于stack exchange,提问作者Tiago Aquino
相关产品推荐
相关产品推荐

