![是否没有一个程序/库来解析常见的 unix 实用程序基于文本的输出,例如 lshw、iwlist、dmidecode?](https://linux22.com/image/194055/%E6%98%AF%E5%90%A6%E6%B2%A1%E6%9C%89%E4%B8%80%E4%B8%AA%E7%A8%8B%E5%BA%8F%2F%E5%BA%93%E6%9D%A5%E8%A7%A3%E6%9E%90%E5%B8%B8%E8%A7%81%E7%9A%84%20unix%20%E5%AE%9E%E7%94%A8%E7%A8%8B%E5%BA%8F%E5%9F%BA%E4%BA%8E%E6%96%87%E6%9C%AC%E7%9A%84%E8%BE%93%E5%87%BA%EF%BC%8C%E4%BE%8B%E5%A6%82%20lshw%E3%80%81iwlist%E3%80%81dmidecode%EF%BC%9F.png)
我有一个 .net core 应用程序,它使用多个 UNIX 实用程序来查询硬件数据,例如lshw
、iwlist
、dmidecode
。
lshw
支持json
输出,所以这不是问题。我目前正在编写自己的解析代码,令我惊讶的是没有工具可以解析此类工具的输出。
我是对的吗?
答案1
TXR是一种从结构松散的多行文本中提取信息的语言。
我们以 的输出dmidecode
为例,将其转换为 JSON 对象列表:
$ txr dmi-data.txr dmi-data
{"Version":"VirtualBox","Address":"0xE0000","Characteristics":["ISA is supported","PCI is supported","Boot from CD is supported",
"Selectable boot is supported","8042 keyboard services are supported (int 9h)",
"CGA/mono video services are supported (int 10h)","ACPI is supported"],
"desc":"BIOS Information","Vendor":"innotek GmbH","ROM Size":"128 kB",
"Release Date":"12/01/2006","Runtime Size":"128 kB"}
{"Version":"1.2","Wake-up Type":"Power Switch","desc":"System Information",
"SKU Number":"Not Specified","Manufacturer":"innotek GmbH","Serial Number":"0",
"Product Name":"VirtualBox","Family":"Virtual Machine","UUID":"CE1C8E2B-F6C0-4E0B-9E40-7958B29D121F"}
{"Version":"1.2","desc":"Base Board Information","Features":["Board is a hosting board"],
"Type":"Motherboard","Chassis Handle":"0x0003","Contained Object Handles":"0",
"Manufacturer":"Oracle Corporation","Serial Number":"0","Asset Tag":"Not Specified",
"Product Name":"VirtualBox","Location In Chassis":"Not Specified"}
{"Version":"Not Specified","desc":"Chassis Information","Boot-up State":"Safe",
"Type":"Other","Security Status":"None","Manufacturer":"Oracle Corporation",
"Lock":"Not Present","Serial Number":"Not Specified","Thermal State":"Safe",
"Asset Tag":"Not Specified","Power Supply State":"Safe"}
{"desc":"OEM Strings","String 2":"vboxRev_140239","String 1":"vboxVer_6.1.14"}
{"desc":"OEM-specific Type","Header and Data":["80 08 08 00 2B C3 33 00"]}
该程序如下所示:
# dmidecode @nil
@(skip)
Table at @nil
@(collect :vars (obj))
Handle @nil
@desc
@ (require (not (mequal desc "Inactive" "End Of Table")))
@ (bind obj @(hash))
@ (repeat :gap 0)
@ (cases)
@\t@prop: @value
@ (do (set [obj prop] value))
@ (or)
@\t@prop:
@ (collect :gap 0)
@\t@\t@valueline
@ (end)
@ (do (set [obj prop] (vec-list valueline)))
@ (end)
@ (until)
Handle @nil
@ (end)
@ (do (set [obj "desc"] desc))
@(end)
@(do (put-jsons obj))
该dmi-data
文件包含:
# dmidecode 3.1
Getting SMBIOS data from sysfs.
SMBIOS 2.5 present.
10 structures occupying 450 bytes.
Table at 0x000E1000.
Handle 0x0000, DMI type 0, 20 bytes
BIOS Information
Vendor: innotek GmbH
Version: VirtualBox
Release Date: 12/01/2006
Address: 0xE0000
Runtime Size: 128 kB
ROM Size: 128 kB
Characteristics:
ISA is supported
PCI is supported
Boot from CD is supported
Selectable boot is supported
8042 keyboard services are supported (int 9h)
CGA/mono video services are supported (int 10h)
ACPI is supported
Handle 0x0001, DMI type 1, 27 bytes
System Information
Manufacturer: innotek GmbH
Product Name: VirtualBox
Version: 1.2
Serial Number: 0
UUID: CE1C8E2B-F6C0-4E0B-9E40-7958B29D121F
Wake-up Type: Power Switch
SKU Number: Not Specified
Family: Virtual Machine
Handle 0x0008, DMI type 2, 15 bytes
Base Board Information
Manufacturer: Oracle Corporation
Product Name: VirtualBox
Version: 1.2
Serial Number: 0
Asset Tag: Not Specified
Features:
Board is a hosting board
Location In Chassis: Not Specified
Chassis Handle: 0x0003
Type: Motherboard
Contained Object Handles: 0
Handle 0x0003, DMI type 3, 13 bytes
Chassis Information
Manufacturer: Oracle Corporation
Type: Other
Lock: Not Present
Version: Not Specified
Serial Number: Not Specified
Asset Tag: Not Specified
Boot-up State: Safe
Power Supply State: Safe
Thermal State: Safe
Security Status: None
Handle 0x0007, DMI type 126, 42 bytes
Inactive
Handle 0x0005, DMI type 126, 15 bytes
Inactive
Handle 0x0006, DMI type 126, 28 bytes
Inactive
Handle 0x0002, DMI type 11, 7 bytes
OEM Strings
String 1: vboxVer_6.1.14
String 2: vboxRev_140239
Handle 0x0008, DMI type 128, 8 bytes
OEM-specific Type
Header and Data:
80 08 08 00 2B C3 33 00
Handle 0xFEFF, DMI type 127, 4 bytes
End Of Table