From 11d830b496d490bd22e978f9da30b2e6dfd53290 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 23 Jul 2026 15:39:59 +0800 Subject: [PATCH] =?UTF-8?q?fix(snmp):=20=E4=BF=AE=E5=A4=8D=20SNMP=20?= =?UTF-8?q?=E9=87=87=E9=9B=86=20ARP=20=E8=A1=A8=E6=97=B6=E5=8F=AA=E8=A1=A5?= =?UTF-8?q?=20MAC=20=E4=B8=8D=E8=A1=A5=E5=8E=82=E5=95=86=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题原因: - snmp_service.py get_arp_table 第267行条件为 if ip_addr and not ip_addr.mac_address: - 只给原本没有 MAC 的 IP 设置 MAC + 厂商 - 如果 IP 已有 MAC(来自 Ping/ARP 扫描)但没有 vendor, 则永远不会补全厂商 解决方案: - 拆分为两个 if: 1. if not ip_addr.mac_address: 写入 MAC 2. if not ip_addr.vendor and ip_addr.mac_address: 补全厂商 - 确保已有 MAC 但无厂商的 IP 也能被补全 - 已有 backfill_vendor.py 脚本可批量回填历史数据 --- backend/app/services/snmp_service.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/backend/app/services/snmp_service.py b/backend/app/services/snmp_service.py index bfe24f8..ba12566 100644 --- a/backend/app/services/snmp_service.py +++ b/backend/app/services/snmp_service.py @@ -258,16 +258,18 @@ class SNMPService: db.commit() - # 更新 IP 资产台账中的 MAC 地址 + # 更新 IP 资产台账中的 MAC 地址和厂商信息 for entry in arp_entries: from app.models.network import IPAddress as IPAddressModel ip_addr = db.query(IPAddressModel).filter( IPAddressModel.ip_address == entry['ip_address'] ).first() - if ip_addr and not ip_addr.mac_address: - ip_addr.mac_address = entry['mac_address'] - # 识别厂商 - ip_addr.vendor = EnhancedScanService.get_mac_vendor(entry['mac_address']) + if ip_addr: + if not ip_addr.mac_address: + ip_addr.mac_address = entry['mac_address'] + # 始终尝试补全厂商(有 MAC 但没厂商时也需要) + if not ip_addr.vendor and ip_addr.mac_address: + ip_addr.vendor = EnhancedScanService.get_mac_vendor(ip_addr.mac_address) db.commit() device.last_successful_poll = now