
开发工具【免费下载链接】jcCLI tool and python library that converts the output of popular command-line tools, file-types, and common strings to JSON, YAML, or Dictionaries. This allows piping of output to tools like jq and simplifying automation scripts.项目地址https://gitcode.com/gh_mirrors/jc/jc点击查看免费下载导读dmidecode是 Linux 系统上读取 SMBIOS/DMI 表并输出硬件信息的经典命令其文本输出格式固定但难以直接交给jq、Python 脚本或监控系统处理。本文介绍 jc 项目内置的dmidecode解析器jc.parsers.dmidecode版本 1.5讲解如何用一行命令把dmidecode的输出转换为结构化 JSON完整说明输出 Schema、parse()函数参数、raw 模式差异并结合源码解析其字段命名、类型转换与多行值处理原理。读完本文你将能在命令行与 Python 模块两种场景下快速获得 BIOS、处理器、内存、主板等硬件信息的机器可读数据。用法命令行与 Python 模块两种方式CLI 方式原文档给出的两种命令行用法完全等价推荐前者——它更清晰地表达了「把dmidecode的 stdout 交给 jc」的数据流$ dmidecode | jc --dmidecode或直接让 jc 代为执行命令$ jc dmidecodedmidecode命令通常需要 root 权限才能读取完整的 DMI 表实际使用时请根据运行环境sudo dmidecode调整。该解析器仅面向 Linux 平台因为info类中声明的兼容平台为[linux]见 jc/parsers/dmidecode.py。模块方式在 Python 中导入 jc 并调用统一入口import jc result jc.parse(dmidecode, dmidecode_command_output)jc.parse会通过 jc/lib.py 中注册的解析器列表按名称定位到dmidecode模块因此传入的字符串名称必须是dmidecode。输出 Schema每条 DMI 记录的结构解析结果是一个数组数组中的每个元素对应dmidecode输出里的一个 DMI 条目如 BIOS Information、Processor Information、Memory Device 等[ { handle: string, type: integer, bytes: integer, description: string, values: { // null if empty lowercase_no_spaces_keys: string, multiline_key_values: [ string, ] } } ]字段语义handleDMI 条目句柄对应输出中的Handle 0x0000保留原始十六进制字符串形式如0x0000typeDMI 类型号对应DMI type 0在最终处理阶段被转换为整数bytes该条目的数据长度同样转换为整数description条目的人类可读名称如BIOS Information、Processor Informationvalues键值对字典保存该条目下所有属性若条目没有任何属性例如Inactive、End Of Table这类空条目则为null。处理示例BIOS Information 条目原文档给出了完整的处理结果示例下面完整呈现-p为 pretty 输出# dmidecode | jc --dmidecode -p [ { handle: 0x0000, type: 0, bytes: 24, description: BIOS Information, values: { vendor: Phoenix Technologies LTD, version: 6.00, release_date: 04/13/2018, address: 0xEA490, runtime_size: 88944 bytes, rom_size: 64 kB, characteristics: [ ISA is supported, PCI is supported, PC Card (PCMCIA) is supported, PNP is supported, APM is supported, BIOS is upgradeable, BIOS shadowing is allowed, ESCD support is available, Boot from CD is supported, Selectable boot is supported, EDD is supported, Print screen service is supported (int 5h), 8042 keyboard services are supported (int 9h), Serial services are supported (int 14h), Printer services are supported (int 17h), CGA/mono video services are supported (int 10h), ACPI is supported, Smart battery is supported, BIOS boot specification is supported, Function key-initiated network boot is supported, Targeted content distribution is supported ], bios_revision: 4.6, firmware_revision: 0.0 } }, ... ]可以看到type: 0与bytes: 24已从原始字符串转换为整数对比dmidecode原始输出中的DMI type 0, 24 bytes而handle仍保持0x0000字符串形式characteristics这类由缩进子行构成的多行属性被转换为字符串数组。raw 模式保留原始字符串添加-r参数后type与bytes不再转换保持原始字符串形态# dmidecode | jc --dmidecode -p -r [ { handle: 0x0000, type: 0, bytes: 24, description: BIOS Information, values: { vendor: Phoenix Technologies LTD, version: 6.00, release_date: 04/13/2018, address: 0xEA490, runtime_size: 88944 bytes, rom_size: 64 kB, characteristics: [ ISA is supported, PCI is supported, PC Card (PCMCIA) is supported, PNP is supported, APM is supported, BIOS is upgradeable, BIOS shadowing is allowed, ESCD support is available, Boot from CD is supported, Selectable boot is supported, EDD is supported, Print screen service is supported (int 5h), 8042 keyboard services are supported (int 9h), Serial services are supported (int 14h), Printer services are supported (int 17h), CGA/mono video services are supported (int 10h), ACPI is supported, Smart battery is supported, BIOS boot specification is supported, Function key-initiated network boot is supported, Targeted content distribution is supported ], bios_revision: 4.6, firmware_revision: 0.0 } }, ... ]对比两个示例可确认raw 模式与处理模式在结构上完全一致唯一区别就是type/bytes的类型。parse() 函数参数详解def parse(data, rawFalse, quietFalse)这是模块的主解析函数参数说明参数类型默认值含义datastring必填待解析的dmidecode文本输出rawbooleanFalse为True时返回未经类型处理的原始输出type/bytes保持字符串quietbooleanFalse为True时抑制兼容性警告信息返回值List of Dictionaries即按 Schema 组织的结构化数据或 raw 数据。调用示例import jc result jc.parse(dmidecode, dmidecode_command_output) # 处理模式 raw_result jc.parse(dmidecode, dmidecode_command_output, rawTrue) # 原始模式源码级解析原理从文本到 JSON 的四个关键环节1. 前置检查与头部剔除parse()首先执行两类校验见 jc/parsers/dmidecode.pyjc.utils.compatibility(__name__, info.compatible, quiet)检查当前操作系统是否为 Linux非 Linux 平台且未设置quietTrue时会向 STDERR 输出警告提示该解析器仅支持 Linux 平台的命令输出实现见 jc/utils.pyjc.utils.input_type_check(data)校验输入类型是否为字符串或字节。随后调用jc.utils.has_data(data)见 jc/utils.py判断输入是否包含非空白字符再按行拆分并剔除开头的版本信息头部行如# dmidecode 3.2、SMBIOS 2.7 present.等见 jc/parsers/dmidecode.py。2. 逐行状态机识别条目、描述、键值主解析循环使用三个布尔状态跟踪当前行所处阶段item_header、item_values、value_list按行特征分流以Handle开头且以bytes结尾的行是条目头从中提取handle、type、bytes三个字段jc/parsers/dmidecode.py条目头之后紧接的普通行作为description单个制表符\t缩进、含冒号且不以冒号结尾的行是普通键值对单个制表符缩进、以冒号结尾的行是多行属性的起始行其后续\t\t缩进的行会逐个收集为数组元素如characteristics值得注意的是源码还处理了「混合多行对象」当某个键之后出现\t\t缩进行时这些行会被收集到{key}_data数组中见 jc/parsers/dmidecode.py。在仓库测试夹具tests/fixtures/centos-7.7/dmidecode.json中可以看到实际产物例如 Memory Controller Information 条目中associated_memory_slots的值为15同时存在associated_memory_slots_data数组列出 15 个槽位句柄。3. 键名规范化所有属性名都会被转换为「小写、空格替换为下划线」的 snake_case 形式jc/parsers/dmidecode.py例如Release Date→release_date、Runtime Size→runtime_size、Wake-up Type→wake-up_type。多行属性的键同样遵循此规则如Supported SRAM Types→supported_sram_types。4. 最终处理_process()非 raw 模式下结果交由_process()收尾jc/parsers/dmidecode.py对int_list {type, bytes}中的字段调用jc.utils.convert_to_int()尝试转为整数转换失败时保持原值返回None或原值见 jc/utils.py将空的values字典置为None从而让Inactive、End Of Table这类无属性条目在 JSON 中呈现为values: null。测试验证覆盖多发行版真实输出仓库测试 tests/test_dmidecode.py 用三份真实系统快照验证了解析器行为test_dmidecode_nodata空输入返回[]对应parse(, quietTrue)test_dmidecode_centos_7_7比对 tests/fixtures/centos-7.7/dmidecode.out 与其 JSON 期望文件test_dmidecode_ubuntu_18_4、test_dmidecode_fedora32同理覆盖 Ubuntu 18.04 与 Fedora 32。从夹具输出可以看出真实dmidecode输出可能包含大量条目CentOS 7.7 的夹具包含 620 个结构体、覆盖 type 0 到 type 127 的多种条目类型解析器需要同时处理普通键值、多行数组、混合_data数组与空条目等多种形态这正是上述状态机设计的价值所在。实战场景建议得到结构化数据后可将其接入常规 JSON 工具链例如# 提取处理器信息DMI type 4的型号与当前频率 dmidecode | jc --dmidecode | jq .[] | select(.type 4) | .values | {version, current_speed} # 统计物理内存条数量 dmidecode | jc --dmidecode | jq [.[] | select(.type 17)] | length在 Python 中则可直接基于字典列表做筛选统计用于硬件资产盘点、基线巡检或告警脚本。需要注意解析结果以dmidecode输出的内容为准不同厂商固件对某些字段可能填充None、Not Specified、Unknown等占位值这些字符串会原样保留在values中脚本处理时应做好容错。赞分享开发工具【免费下载链接】jcCLI tool and python library that converts the output of popular command-line tools, file-types, and common strings to JSON, YAML, or Dictionaries. This allows piping of output to tools like jq and simplifying automation scripts.项目地址https://gitcode.com/gh_mirrors/jc/jc点击查看免费下载相关推荐Laravel Sail多服务配置技巧Redis、Memcached、RabbitMQ集成Laravel Sail多服务配置技巧Redis、Memcached、RabbitMQ集成 Laravel Sail是Laravel官方提供的Docker开发开发工具jc nmcli 解析器将 NetworkManager nmcli 输出转换为 JSON/YAML/字典的完整指南jc nmcli 解析器将 NetworkManager nmcli 输出转换为 JSON/YAML/字典的完整指南 在 Linux 网络自动化中 nmcl开发工具gpt-oss-20b-WFP8-AFP8-KVFP8扩展指南如何调整模型参数以适应不同硬件gpt oss 20b WFP8 AFP8 KVFP8扩展指南如何调整模型参数以适应不同硬件 想要在不同硬件上高效运行gpt oss 20b WFP8 AFP开发工具上一篇GPT-Author本地Jupyter环境配置脱离云端的AI写作解决方案下一篇OBS浏览器源插件完整指南实战教程与深度解析创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考