Linux 程序获取硬件信息 HardWare LiSter源码修改适配

lshw源码包下载地址:https://www.ezix.org/download/?package=lshw.ezix.org
功能与lshw命令相同,但是我们可以通过修改源码的方式更加灵活的获取我们想要的信息。

(解压后直接make,然后进入src/目录执行./lshw获取电脑的硬件信息包括内存,主板,CPU,硬件,网卡,显卡,声卡等等信息)

源码编译后执行lshw和命令lshw结果是一样的
lshw命令如下(看得到信息非常详细但是对于不同的需求来说要求是不一样的,这些信息已经有些冗余了对我来说):

deepin@deepin-PC:~/$ lshw 
WARNING: you should run this program as super-user.
deepin-pc                   
    description: Computer
    product: N/A
    version: Talas Talas 0 Talas Talas 0 Talas Talas 0 Talas Talas 0
    width: 64 bits
    capabilities: smp
  *-core
       description: Motherboard
       physical id: 0
     *-memory
          description: System memory
          physical id: 0
          size: 7524MiB
     *-cpu:0
          product: Sunway
          vendor: Jiangnan Computer Technology Research Institute
          physical id: 1
          bus info: cpu@0
          version: SW6A
          width: 64 bits
          capabilities: bwx fix cix mvi pat pmi
     *-cpu:1 DISABLED
          product: Sunway
          vendor: Jiangnan Computer Technology Research Institute
          physical id: 2
          bus info: cpu@1
          version: SW6A
          width: 64 bits
          capabilities: bwx fix cix mvi pat pmi
     *-cpu:2 DISABLED
          product: Sunway
          vendor: Jiangnan Computer Technology Research Institute
          physical id: 3
          bus info: cpu@2
          version: SW6A
          width: 64 bits
          capabilities: bwx fix cix mvi pat pmi
     *-cpu:3 DISABLED
          product: Sunway
          vendor: Jiangnan Computer Technology Research Institute
          physical id: 4
          bus info: cpu@3
          version: SW6A
          width: 64 bits
          capabilities: bwx fix cix mvi pat pmi
     *-pci
          description: PCI bridge
          product: PEX 8748 48-Lane, 12-Port PCI Express Gen 3 (8 GT/s) Switch, 27 x 27mm FCBGA
          vendor: PLX Technology, Inc.
          physical id: 100
          bus info: pci@0000:01:00.0
          version: ca
          width: 32 bits
          clock: 33MHz
          capabilities: pci normal_decode bus_master cap_list
          resources: memory:c0e0600000-c0e063ffff ioport:c600001000(size=8192) memory:c0e0000000-c0e05fffff ioport:c3c0000000(size=8388608)
        *-pci:0
             description: PCI bridge
             product: PEX 8748 48-Lane, 12-Port PCI Express Gen 3 (8 GT/s) Switch, 27 x 27mm FCBGA
             vendor: PLX Technology, Inc.
             physical id: 1
             bus info: pci@0000:02:01.0
             version: ca
             width: 32 bits
             clock: 33MHz
             capabilities: pci normal_decode bus_master cap_list
             resources: ioport:c600001000(size=4096) memory:c0e0000000-c0e02fffff ioport:c3c0000000(size=2097152)
           *-sata
                description: SATA controller
                product: ASM1062 Serial ATA Controller
                vendor: ASMedia Technology Inc.
                physical id: 0
                bus info: pci@0000:03:00.0
                version: 02
                width: 32 bits
                clock: 33MHz
                capabilities: sata ahci_1.0 bus_master cap_list rom
                configuration: driver=ahci latency=0
                resources: irq:21 ioport:c600001020(size=8) ioport:c600001030(size=4) ioport:c600001028(size=8) ioport:c600001034(size=4) ioport:c600001000(size=32) memory:c0e0010000-c0e00101ff memory:c0e0000000-c0e000ffff
        *-pci:1
             description: PCI bridge
             product: PEX 8748 48-Lane, 12-Port PCI Express Gen 3 (8 GT/s) Switch, 27 x 27mm FCBGA
             vendor: PLX Technology, Inc.
             physical id: 2
             bus info: pci@0000:02:02.0
             version: ca
             width: 32 bits
             clock: 33MHz
             capabilities: pci normal_decode bus_master cap_list
        *-pci:2
             description: PCI bridge
             product: PEX 8748 48-Lane, 12-Port PCI Express Gen 3 (8 GT/s) Switch, 27 x 27mm FCBGA
             vendor: PLX Technology, Inc.
             physical id: 8
             bus info: pci@0000:02:08.0
             version: ca
             width: 32 bits
             clock: 33MHz
             capabilities: pci normal_decode bus_master cap_list
             resources: ioport:c3c0200000(size=1048576)
           *-usb
                description: USB controller
                product: EJ188/EJ198 USB 3.0 Host Controller
                vendor: Etron Technology, Inc.
                physical id: 0
                bus info: pci@0000:05:00.0
                version: 00
                width: 64 bits
                clock: 33MHz
                capabilities: xhci bus_master cap_list
                configuration: driver=xhci_hcd latency=0
                resources: iomemory:30-2f irq:27 memory:c3c0200000-c3c0207fff
        *-pci:3
             description: PCI bridge
             product: PEX 8748 48-Lane, 12-Port PCI Express Gen 3 (8 GT/s) Switch, 27 x 27mm FCBGA
             vendor: PLX Technology, Inc.
             physical id: 9
             bus info: pci@0000:02:09.0
             version: ca
             width: 32 bits
             clock: 33MHz
             capabilities: pci normal_decode bus_master cap_list
             resources: ioport:c600002000(size=4096) memory:c0e0300000-c0e05fffff ioport:c3c0300000(size=2097152)
           *-network
                description: Ethernet interface
                product: I210 Gigabit Network Connection
                vendor: Intel Corporation
                physical id: 0
                bus info: pci@0000:06:00.0
                logical name: enp6s0
                version: 03
                serial: 00:0b:2f:18:50:3d
                size: 100Mbit/s
                capacity: 1Gbit/s
                width: 32 bits
                clock: 33MHz
                capabilities: bus_master cap_list ethernet physical tp 10bt 10bt-fd 100bt 100bt-fd 1000bt-fd autonegotiation
                configuration: autonegotiation=on broadcast=yes driver=igb driverversion=5.4.0-k duplex=full firmware=3.25, 0x800005cf ip=192.168.6.48 latency=0 link=yes multicast=yes port=twisted pair speed=100Mbit/s
                resources: irq:17 memory:c0e0300000-c0e037ffff ioport:c600002000(size=32) memory:c0e0380000-c0e0383fff
        *-pci:4
             description: PCI bridge
             product: PEX 8748 48-Lane, 12-Port PCI Express Gen 3 (8 GT/s) Switch, 27 x 27mm FCBGA
             vendor: PLX Technology, Inc.
             physical id: 10
             bus info: pci@0000:02:10.0
             version: ca
             width: 32 bits
             clock: 33MHz
             capabilities: pci normal_decode bus_master cap_list
             resources: ioport:c3c0500000(size=1048576)
           *-usb
                description: USB controller
                product: EJ188/EJ198 USB 3.0 Host Controller
                vendor: Etron Technology, Inc.
                physical id: 0
                bus info: pci@0000:07:00.0
                version: 00
                width: 64 bits
                clock: 33MHz
                capabilities: xhci bus_master cap_list
                configuration: driver=xhci_hcd latency=0
                resources: iomemory:30-2f irq:28 memory:c3c0500000-c3c0507fff
        *-pci:5
             description: PCI bridge
             product: PEX 8748 48-Lane, 12-Port PCI Express Gen 3 (8 GT/s) Switch, 27 x 27mm FCBGA
             vendor: PLX Technology, Inc.
             physical id: 11
             bus info: pci@0000:02:11.0
             version: ca
             width: 32 bits
             clock: 33MHz
             capabilities: pci normal_decode bus_master cap_list
     *-display
          description: VGA compatible controller
          product: Oland [Radeon HD 8570 / R7 240/340 / R520 OEM]
          vendor: Advanced Micro Devices, Inc. [AMD/ATI]
          physical id: 5
          bus info: pci@0001:20:00.0
          version: 87
          width: 64 bits
          clock: 33MHz
          capabilities: vga_controller bus_master cap_list rom
          configuration: driver=radeon latency=0
          resources: iomemory:30-2f iomemory:30-2f irq:281 memory:f3c0000000-f3cfffffff memory:f3d0000000-f3d003ffff ioport:f600001000(size=256) memory:f0e0000000-f0e001ffff
     *-multimedia UNCLAIMED
          description: Audio device
          product: Oland/Hainan/Cape Verde/Pitcairn HDMI Audio [Radeon HD 7000 Series]
          vendor: Advanced Micro Devices, Inc. [AMD/ATI]
          physical id: 0.1
          bus info: pci@0001:20:00.1
          version: 00
          width: 64 bits
          clock: 33MHz
          capabilities: cap_list
          configuration: latency=0
          resources: iomemory:30-2f memory:f3d0040000-f3d0043fff
WARNING: output may be incomplete or inaccurate, you should run this program as super-user.

以下是源码修改后(print.cc中的输出函数Myprint_修改),不同的需求修改的也不一样,大家根据需求自己阅读源码自行修改
print.cc文件

//新增函数
void GetInfo(hwNode & node,string &buf,string id)
{
        //cpu 只取一条
        if(id == "cpu")
        {
                if(!node.getProduct().empty())
                {
                        buf += string("cpu处理器") + "=";
                        buf += node.getProduct();
                        buf += string("\n");
                        return;
                }
        }
        //其他取三条
        if (node.getDescription() != "")
        {
                buf += node.getDescription();
        }
        if (node.getProduct() != "")
        {
                buf += string(",") + node.getProduct();
        }

        if (node.getVendor() != "")
        {     
                buf += string(",") + node.getVendor();
        }

        //内存或硬盘 获取大小
        if(id == "memory" || id == "disk")
        {
                if (node.getSize() > 0)
                {
                        switch (node.getClass())
                        {
                                case hw::disk: 
                                        buf += string(",") + decimalkilos(node.getSize()) + "B";         
                                        break;
                                case hw::display:
                                case hw::memory:
                                case hw::address:
                                case hw::storage:
                                case hw::volume:
                                        buf += string(",") + kilobytes(node.getSize());  
                                        break;
                                case hw::processor:
                                case hw::bus:
                                case hw::system:
                                case hw::network:
                                case hw::power:
                                        break;
                                default:
                                        break;
                        }
                }
        }
        buf += string("\n");
}

//修改原有输出函数
void Myprint_(hwNode & node,string &buf,bool html,int level)
{
        if (visible(node.getClassName()))
        {
                if(node.getId() == "core")
                {
                        buf += string("主板信息") + "=";
                        GetInfo(node,buf,"core");
                }
                else if(node.getId() == "firmware")
                {
                        buf += string("BIOS") + "=";
                        GetInfo(node,buf,"firmware");
                }
                else if(node.getId() == "ide")
                {
                        buf += string("IDE ATA/ATAPI控制器") + "=";
                        GetInfo(node,buf,"ide");
                }
                else if(node.getId() == "display")
                {
                        buf += string("显卡") + "=";
                        GetInfo(node,buf,"display");
                }
                else if(node.getId() == "network")
                {
                        buf += string("网卡") + "=";
                        GetInfo(node,buf,"network");
                }
                else if(node.getId() == "multimedia")
                {
                        buf += string("声卡") + "=";
                        GetInfo(node,buf,"multimedia");
                }
                else if(node.getId() == "cdrom")
                {
                        buf += string("光驱") + "=";
                        GetInfo(node,buf,"cdrom");
                }
                else if(node.getId().find("cpu") != std::string::npos)
                {
                        //cpu 处理一次
                        if(buf.find("cpu处理器") == std::string::npos)
                        {
                                GetInfo(node,buf,"cpu");
                        }
                }
                else if(node.getId() == "memory")
                {
                        buf += string("内存") + "=";
                        GetInfo(node,buf,"memory");
                }
                else if(node.getId() == "disk")
                {
                        buf += string("硬盘") + "=";
                        GetInfo(node,buf,"disk");
                }
        }

        //递归获取相关属性
        for (unsigned int i = 0; i < node.countChildren(); i++)
        {
                Myprint_(*node.getChild(i), buf,html, visible(node.getClassName()) ? level + 1 : 1);
        }
}

可以看到修改源码后执行lshw获取到的信息已经简化了很多,相对于源码的输出信息,这里只有我关心的一小部分精简了出来

root@deepin-PC:/home/deepin/lshw-B.02.19.2# cd src/
root@deepin-PC:/home/deepin/lshw-B.02.19.2/src# ls
core  gui  jedec.ods  lshw  lshw.1  lshw.cc  lshw.o  lshw.sgml	mac-prefixes  Makefile	manuf.txt  oui.txt  pci.ids  pnp.ids  pnpid.txt  po  usb.ids
root@deepin-PC:/home/deepin/lshw-B.02.19.2/src# ./lshw 
主板信息=Motherboard
内存=System memory,7524MiB
cpu处理器=SW1621 CPU @ 1.60GHZ
硬盘=ATA Disk,Phison SATA SSD,256GB
网卡=Ethernet interface,I210 Gigabit Network Connection,Intel Corporation
显卡=VGA compatible controller,Oland [Radeon HD 8570 / R7 240/340 / R520 OEM],Advanced Micro Devices, Inc. [AMD/ATI]
声卡=Audio device,Oland/Hainan/Cape Verde/Pitcairn HDMI Audio [Radeon HD 7000 Series],Advanced Micro Devices, Inc. [AMD/ATI]

root@deepin-PC:/home/deepin/lshw-B.02.19.2/src# su deepin
deepin@deepin-PC:~/lshw-B.02.19.2/src$ ./lshw 
WARNING: you should run this program as super-user.
主板信息=Motherboard
内存=System memory,7524MiB
cpu处理器=SW1621 CPU @ 1.60GHZ
网卡=Ethernet interface,I210 Gigabit Network Connection,Intel Corporation
显卡=VGA compatible controller,Oland [Radeon HD 8570 / R7 240/340 / R520 OEM],Advanced Micro Devices, Inc. [AMD/ATI]
声卡=Audio device,Oland/Hainan/Cape Verde/Pitcairn HDMI Audio [Radeon HD 7000 Series],Advanced Micro Devices, Inc. [AMD/ATI]

WARNING: output may be incomplete or inaccurate, you should run this program as super-user.
deepin@deepin-PC:~/lshw-B.02.19.2/src$ 

项目中使用 编译为静态库liblshw.a直接使用

#include "hw.h"
#include "main.h"
#include "print.h"
#include "config.h"
#include "version.h"
#include "options.h"
#include "osutils.h"

//lshw 源码移植添加到工程(编译报错未定义) 源码中的函数status
void status(const char *message)
{
    static size_t lastlen = 0;

    if(enabled("output:quiet") || disabled("output:verbose"))
        return;

    if (isatty(2))
    {
        fprintf(stderr, "\r");
        for (size_t i = 0; i < lastlen; i++)
            fprintf(stderr, " ");
        fprintf(stderr, "\r%s\r", message);
        fflush(stderr);
        lastlen = strlen(message);
    }
}

void getHardWareInfo(ThreadControl *threadcontrol)
{
    Json::Value data;
    //这里往下是需要初始化一些属性
#ifndef NONLS
    setlocale (LC_ALL, "");
    bindtextdomain (PACKAGE, LOCALEDIR);
    bind_textdomain_codeset (PACKAGE, "UTF-8");
    textdomain (PACKAGE);
#endif

    disable("isapnp");
    disable("output:json");
    disable("output:db");
    disable("output:xml");
    disable("output:html");
    disable("output:hwpath");
    disable("output:businfo");
    disable("output:X");
    disable("output:quiet");
    disable("output:sanitize");
    disable("output:numeric");
    enable("output:time");

    // define some aliases for nodes classes
    alias("disc", "disk");
    alias("cpu", "processor");
    alias("lan", "network");
    alias("net", "network");
    alias("video", "display");
    alias("sound", "multimedia");
    alias("modem", "communication");

    string buf = "";
    hwNode computer("computer",hw::system);
    scan_system(computer);
    //buf返回的就是我们过滤出来的信息,接着提取信息(对buf)即可
    Myprint_(computer,buf,enabled("output:html"));
}

总结:
1.可以看到root和普通用户去执行lshw获取的信息时有些差别的(更直观),普通用户没能获取到硬盘信息,当然在不同环境下并不是所有的都是硬盘获取不到,具体的自己试验
2.源码和修改后编译输出的信息可以看到原先非常详细但是太多了我们并不需要这么多,修改后只有十余行左右都是我们需要的信息
3.已经在不同架构上适配完成,包括mips,aarch,sw_64,x86_64分别对应的是中标麒麟,银河麒麟,申威uos,方得(使用源码适配是因为有些系统是没有lshw命令的,为了通用性并且已经提到了lshw输出的信息对于我来说有些冗余了)
4.lshw命令可以使用lshw -json以及lshw -xml就是讲信息格式转为json或者xml但是我们项目中用到的全部为json所以不想为了一个功能而引入xml并且输出的json格式是有点问题的(这里并不知道为什么,源码编完后使用lshw -json后输出的格式是有问题的,少了括号还是什么我记不太清出了,反正在使用json解析时出了问题,输出xml格式是没有问题的,可以根据需要使用命令获取所需)所以这里没有使用-xml或者-json而是直接修改源码来获取需要的信息(过滤不需要的信息)

posted @   J1nu  阅读(67)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
点击右上角即可分享
微信分享提示