是否没有一个程序/库来解析常见的 unix 实用程序基于文本的输出,例如 lshw、iwlist、dmidecode?

是否没有一个程序/库来解析常见的 unix 实用程序基于文本的输出,例如 lshw、iwlist、dmidecode?

我有一个 .net core 应用程序,它使用多个 UNIX 实用程序来查询硬件数据,例如lshwiwlistdmidecodelshw支持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

相关内容