You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

处理防火墙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:

  1. Top-level Block Extraction: Instead of grabbing the first end line, we track indentation depth to find the correct closing end for the entire config firewall vip section.
  2. VIP Edit Boundary Detection: For each edit line, we follow indentation levels to navigate nested blocks. We stop when we hit a next line at the same depth as the original edit—this ensures we capture the full VIP entry including any nested sub-configs.
  3. Whitespace Respect: By calculating line_depth as 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:21:49