服务器固件版本字典构建异常:NIC.Integrated与NIC.Embedded值混淆
问题描述
我正在尝试为服务器已安装的固件版本创建字典,目标是让FQDD作为键,对应值为Current Version。其他设备都能正常处理,但网卡相关的键(NIC.Integrated和NIC.Embedded)出现问题:NIC.Integrated键被错误赋予了NIC.Embedded键的Current Version值。我尝试通过ElementName做条件判断,但问题仍未解决。
我的代码
sw_info = {} sw = {} with open(f"sw_info.log", "r") as f: for line in f: try: k, v = line.strip().split("=") sw_info[k.strip()] = v.strip() except: pass try: if "Broadcom Adv. Dual 10GBASE-T Ethernet" in sw_info['ElementName']: sw.update({sw_info['FQDD']: sw_info['Current Version']}) print(sw_info['Current Version']) else: sw.update({sw_info['FQDD']: sw_info['Current Version']}) except: pass for key, value in sw.items(): print(key,': ', value)
固件版本日志文件内容
-------------------------SOFTWARE INVENTORY------------------------ ComponentType = FIRMWARE ElementName = Power Supply.Slot.1 FQDD = PSU.Slot.1 InstallationDate = 2022-09-03T18:32:38Z Current Version = 00.21.33 HashValue = NA ------------------------------------------------------------------- ComponentType = FIRMWARE ElementName = Power Supply.Slot.2 FQDD = PSU.Slot.2 InstallationDate = 2022-09-03T18:32:38Z Current Version = 00.21.33 HashValue = NA ------------------------------------------------------------------- ComponentType = FIRMWARE ElementName = Integrated Remote Access Controller FQDD = iDRAC.Embedded.1-1 InstallationDate = NA Rollback Version = 5.10.50.00 HashValue = NA ------------------------------------------------------------------- ComponentType = FIRMWARE ElementName = Integrated Remote Access Controller FQDD = iDRAC.Embedded.1-1 InstallationDate = 2022-09-03T18:32:38Z Current Version = 5.10.50.00 HashValue = NA ------------------------------------------------------------------- ComponentType = FIRMWARE ElementName = Broadcom Adv. Dual 10GBASE-T Ethernet - 84:16:0C:C3:4C:B1 FQDD = NIC.Integrated.1-2-1 InstallationDate = 2022-09-03T19:20:02Z Current Version = 22.00.07.60 HashValue = 30df8d850c067b99136a6f1593cbbe74e4ce771a0e54283966b6de44ebcb495e ------------------------------------------------------------------- ComponentType = FIRMWARE ElementName = Broadcom Adv. Dual 10GBASE-T Ethernet - 84:16:0C:C3:4C:B0 FQDD = NIC.Integrated.1-1-1 InstallationDate = 2022-09-03T19:20:01Z Current Version = 22.00.07.60 HashValue = 30df8d850c067b99136a6f1593cbbe74e4ce771a0e54283966b6de44ebcb495e ------------------------------------------------------------------- ComponentType = FIRMWARE ElementName = Broadcom Gigabit Ethernet BCM5720 - EC:2A:72:30:4E:9C FQDD = NIC.Embedded.1-1-1 InstallationDate = 2022-09-03T19:19:59Z Current Version = 22.00.6 HashValue = 56fa85676e6d570f714fb659f202371f1c570263b680e2d40d16059acfa9e3e6 ------------------------------------------------------------------- ComponentType = FIRMWARE ElementName = Broadcom Gigabit Ethernet BCM5720 - EC:2A:72:30:4E:9D FQDD = NIC.Embedded.2-1-1 InstallationDate = 2022-09-03T19:20:00Z Current Version = 22.00.6 HashValue = 56fa85676e6d570f714fb659f202371f1c570263b680e2d40d16059acfa9e3e6 ------------------------------------------------------------------- ComponentType = FIRMWARE ElementName = Broadcom Adv. Dual 10GBASE-T Ethernet - 84:16:0C:C3:4C:B1 FQDD = NIC.Integrated.1-2-1 InstallationDate = NA Available Version = 22.00.07.60 HashValue = 30df8d850c067b99136a6f1593cbbe74e4ce771a0e54283966b6de44ebcb495e ------------------------------------------------------------------- ComponentType = FIRMWARE ElementName = Broadcom Adv. Dual 10GBASE-T Ethernet - 84:16:0C:C3:4C:B0 FQDD = NIC.Integrated.1-1-1 InstallationDate = NA Available Version = 22.00.07.60 HashValue = 30df8d850c067b99136a6f1593cbbe74e4ce771a0e54283966b6de44ebcb495e ------------------------------------------------------------------- ComponentType = FIRMWARE ElementName = Broadcom Gigabit Ethernet BCM5720 - EC:2A:72:30:4E:9C FQDD = NIC.Embedded.1-1-1 InstallationDate = NA Available Version = 22.00.6 HashValue = 56fa85676e6d570f714fb659f202371f1c570263b680e2d40d16059acfa9e3e6 ------------------------------------------------------------------- ComponentType = FIRMWARE ElementName = Broadcom Gigabit Ethernet BCM5720 - EC:2A:72:30:4E:9D FQDD = NIC.Embedded.2-1-1 InstallationDate = NA Available Version = 22.00.6 HashValue = 56fa85676e6d570f714fb659f202371f1c570263b680e2d40d16059acfa9e3e6 ------------------------------------------------------------------- ComponentType = FIRMWARE ElementName = PERC H745 Front FQDD = RAID.SL.3-1 InstallationDate = 2022-09-03T18:35:21Z Current Version = 51.16.0-4076 HashValue = cdf6d5780d28c34e792c13e57c300ab46d964fdb1e1f189f7976044fa36d0092 ------------------------------------------------------------------- ComponentType = FIRMWARE ElementName = PERC H745 Front FQDD = RAID.SL.3-1 InstallationDate = NA Available Version = 51.16.0-4076 HashValue = cdf6d5780d28c34e792c13e57c300ab46d964fdb1e1f189f7976044fa36d0092 ------------------------------------------------------------------- ComponentType = BIOS ElementName = BIOS FQDD = BIOS.Setup.1-1 InstallationDate = 2022-09-03T18:35:21Z Current Version = 1.7.4 HashValue = 576f3516f12de1669552d56f0556b42a92e26b78db6c2c3486ec3e9cc144f0fe ------------------------------------------------------------------- ComponentType = BIOS ElementName = BIOS FQDD = BIOS.Setup.1-1 InstallationDate = NA Available Version = 1.7.4 HashValue = 576f3516f12de1669552d56f0556b42a92e26b78db6c2c3486ec3e9cc144f0fe ------------------------------------------------------------------- ComponentType = APPLICATION ElementName = Lifecycle Controller FQDD = USC.Embedded.1:LC.Embedded.1 InstallationDate = 2022-09-03T18:32:42Z Current Version = 5.10.50.00 HashValue = NA ------------------------------------------------------------------- ComponentType = FIRMWARE ElementName = System CPLD FQDD = CPLD.Embedded.1 InstallationDate = 2022-09-03T18:32:42Z Current Version = 1.0.7 HashValue = NA ------------------------------------------------------------------- ComponentType = APPLICATION ElementName = Identity Module(WWJ74) FQDD = PM.Embedded.1 InstallationDate = 2022-09-03T20:31:07Z Current Version = 2.00 HashValue = NA ------------------------------------------------------------------- ComponentType = FIRMWARE ElementName = TPM FQDD = TPM.Integrated.1-1 InstallationDate = 2022-09-03T20:02:58Z Current Version = 7.2.2.0 HashValue = NA ------------------------------------------------------------------- ComponentType = APPLICATION ElementName = Dell 64 Bit uEFI Diagnostics, version 4301, 4301A73, 4301.74 FQDD = Diagnostics.Embedded.1:LC.Embedded.1 InstallationDate = 2022-09-03T20:31:06Z Current Version = 4301A73 HashValue = NA ------------------------------------------------------------------- ComponentType = APPLICATION ElementName = Dell OS Driver Pack, 22.07.10, A00 FQDD = DriverPack.Embedded.1:LC.Embedded.1 InstallationDate = 2022-09-03T20:31:06Z Current Version = 22.07.10 HashValue = NA ------------------------------------------------------------------- ComponentType = APPLICATION ElementName = Dell EMC iDRAC Service Module Embedded Package v4.3.0.0, A00 FQDD = ServiceModule.Embedded.1 InstallationDate = 2022-09-03T20:31:07Z Current Version = 4.3.0.0 HashValue = NA ------------------------------------------------------------------- ComponentType = FIRMWARE ElementName = Backplane 1 FQDD = RAID.Backplane.Firmware.1 InstallationDate = 2022-09-03T19:02:40Z Current Version = 3.72 HashValue = b92e8c23b0caab9cc0a820f5a7aca1813b43c8ed93b533c76cd897dd7e2754d2 ------------------------------------------------------------------- ComponentType = FIRMWARE ElementName = Backplane 1 FQDD = RAID.Backplane.Firmware.1 InstallationDate = NA Available Version = 3.72 HashValue = b92e8c23b0caab9cc0a820f5a7aca1813b43c8ed93b533c76cd897dd7e2754d2 ------------------------------------------------------------------- ComponentType = FIRMWARE ElementName = Disk 1 in Backplane 1 of RAID Controller in SL 3 FQDD = Disk.Bay.1:Enclosure.Internal.0-1:RAID.SL.3-1 InstallationDate = 2022-09-03T18:35:22Z Current Version = BJ03 HashValue = NA ------------------------------------------------------------------- ComponentType = FIRMWARE ElementName = Disk 0 in Backplane 1 of RAID Controller in SL 3 FQDD = Disk.Bay.0:Enclosure.Internal.0-1:RAID.SL.3-1 InstallationDate = 2022-09-03T18:35:22Z Current Version = BJ03 HashValue = NA -------------------------------------------------------------------
解决方案
问题根源
- 设备数据交叉污染:原代码逐行读取时,
sw_info字典会持续累加所有行的键值对,不同设备的数据互相覆盖,导致网卡版本信息混乱。 - 重复条目覆盖有效值:同一个FQDD存在两条记录(一条带
Current Version,一条带Available Version),原代码会用后面的Available Version条目覆盖之前的Current Version值。 - 无效条件判断:ElementName的判断逻辑没有实际作用,无论条件是否成立都会执行相同的更新操作,反而容易引发异常。
修正后的代码
sw = {} with open("sw_info.log", "r") as f: # 按分隔线分割所有设备块 device_blocks = f.read().split("-------------------------------------------------------------------") for block in device_blocks: sw_info = {} # 处理每个块内的行 for line in block.strip().split("\n"): line = line.strip() if not line or line.startswith("-------------------------"): continue try: # 用split("=", 1)避免值中包含等号时出错 k, v = line.split("=", 1) sw_info[k.strip()] = v.strip() except ValueError: continue # 只处理包含FQDD和Current Version的有效条目 if "FQDD" in sw_info and "Current Version" in sw_info: sw[sw_info["FQDD"]] = sw_info["Current Version"] # 打印最终结果 for key, value in sw.items(): print(f"{key}: {value}")
改动说明
- 按设备块独立处理:用分隔线将日志分割为独立的设备块,每个块单独解析,彻底避免数据交叉污染。
- 重置临时存储字典:每个设备块处理前重新初始化
sw_info,确保只存储当前设备的信息。 - 过滤有效记录:只保留包含
FQDD和Current Version的记录,自动忽略带Available Version的更新候选条目。 - 健壮的键值分割:使用
split("=", 1)分割键值对,避免值中包含等号时出现解析错误。
内容的提问来源于stack exchange,提问作者Benn
相关产品推荐
相关产品推荐

