"""测试 ONU 状态解析""" import re def parse_onu_status(output: str): """解析 ONU 状态""" devices = {} lines = output.split('\n') for line in lines: mac_match = re.search(r'([0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4})', line, re.IGNORECASE) if mac_match: mac = mac_match.group(1).lower() if 'Up' in line: devices[mac] = 'online' elif 'Offline' in line: devices[mac] = 'offline' return devices # 测试数据 test_output = """ 1484-7790-5200 12 <1000 Onu1/0/1:1 WA6520H-EGPON/A 106/ Up N/A 1484-7790-4e20 13 <1000 Onu1/0/1:2 WA6520H-EGPON/A 106/ Up N/A 1484-7790-3900 N/A N/A Onu1/0/1:3 N/A N/A Offline N/A """ result = parse_onu_status(test_output) print("解析结果:") for mac, status in result.items(): print(f" {mac}: {status}")