一、背景

在日常服务器系统安装以及pc桌面机器维护的过程中,经常会需要一些硬件信息,比如安装Windows需要知道raid卡信息,给pc加内存条需要知道内存是几代的以及频率型号是什么,有几个插槽等。

是否有一个简单的软件,运行之后直接展示这些信息呢。于是抱着试试的心态,使用gpt进行了编程之旅。

二、hardinfo.go代码

package main

import (
    "fmt"
    "github.com/jaypipes/ghw"
)

func main() {
    // 获取BIOS信息
    bios, err := ghw.BIOS()
    if err != nil {
        fmt.Printf("Error getting BIOS info: %v", err)
        return // 确保在出错时不继续执行
    }

    // 打印BIOS信息字段
    fmt.Printf("BIOS Information:\n")
    fmt.Printf("  Vendor: %s\n", bios.Vendor)
    fmt.Printf("  Version: %s\n", bios.Version)
    fmt.Printf("  Date: %s\n", bios.Date)

    // 获取主板信息
    chassis, err := ghw.Chassis()
    if err != nil {
        fmt.Printf("Error getting chassis info: %v", err)
        return
    }

    fmt.Printf("Chassis Information:\n")
    fmt.Printf("  Asset Tag: %s\n", chassis.AssetTag)
    fmt.Printf("  Serial Number: %s\n", chassis.SerialNumber)
    fmt.Printf("  Type: %s\n", chassis.Type)
    fmt.Printf("  Type Description: %s\n", chassis.TypeDescription)
    fmt.Printf("  Vendor: %s\n", chassis.Vendor)
    fmt.Printf("  Version: %s\n", chassis.Version)

    // 获取基础板信息
    baseboard, err := ghw.Baseboard()
    if err != nil {
        fmt.Printf("Error getting baseboard info: %v", err)
        return // 确保在出错时不继续执行
    }

    // 打印基础板信息字段
    fmt.Printf("Baseboard Information:\n")
    fmt.Printf("  Asset Tag: %s\n", baseboard.AssetTag)
    fmt.Printf("  Serial Number: %s\n", baseboard.SerialNumber)
    fmt.Printf("  Vendor: %s\n", baseboard.Vendor)
    fmt.Printf("  Product: %s\n", baseboard.Product)
    fmt.Printf("  Version: %s\n", baseboard.Version)

    // 获取产品信息
    product, err := ghw.Product()
    if err != nil {
        fmt.Printf("Error getting product info: %v", err)
        return // 确保在出错时不继续执行
    }

    // 打印产品信息字段
    fmt.Printf("Product Information:\n")
    fmt.Printf("  Family: %s\n", product.Family)
    fmt.Printf("  Name: %s\n", product.Name)
    fmt.Printf("  Serial Number: %s\n", product.SerialNumber)
    fmt.Printf("  UUID: %s\n", product.UUID)
    fmt.Printf("  SKU: %s\n", product.SKU)
    fmt.Printf("  Vendor: %s\n", product.Vendor)
    fmt.Printf("  Version: %s\n", product.Version)

    // 获取CPU信息
    cpu, err := ghw.CPU()
    if err != nil {
        fmt.Printf("Error getting CPU info: %v", err)
    }
    fmt.Printf("CPU:\n")
    fmt.Printf("  Model: %s\n", cpu.Processors[0].Model)
    fmt.Printf("  TotalCores: %d\n", cpu.TotalCores)
    fmt.Printf("  TotalThreads: %d\n", cpu.TotalThreads)
    fmt.Printf("  Processors:\n")
    for _, proc := range cpu.Processors {
        fmt.Printf("    ID: %d\n", proc.ID)
        fmt.Printf("    Vendor: %s\n", proc.Vendor)
        fmt.Printf("    Model: %s\n", proc.Model)
        fmt.Printf("    Cores: %d\n", proc.NumCores)
        fmt.Printf("    Threads: %d\n", proc.NumThreads)
        // fmt.Printf("    ClockSpeed: %.2f GHz\n", float64(proc.ClockSpeed)/1e9)
    }

    // 获取内存信息
    mem, err := ghw.Memory()
    if err != nil {
        fmt.Printf("Error getting memory info: %v", err)
    }
    fmt.Printf("\nMemory:\n")
    fmt.Printf("  TotalUsableBytes: %d\n", mem.TotalUsableBytes)
    fmt.Printf("  TotalPhysicalBytes: %d\n", mem.TotalPhysicalBytes)
    fmt.Printf("  SupportedPageSizes: %v\n", mem.SupportedPageSizes)
    for _, module := range mem.Modules {
        fmt.Printf("    Vendor: %s\n", module.Vendor)
        fmt.Printf("    SerialNumber: %s\n", module.SerialNumber)
        fmt.Printf("    SizeBytes: %d\n", module.SizeBytes)
        // fmt.Printf("    Frequency: %d MHz\n", module.ConfiguredClockSpeed)
    }

    // 获取块设备(磁盘控制器)信息
    block, err := ghw.Block()
    if err != nil {
        fmt.Printf("Error getting block device info: %v", err)
    }
    fmt.Printf("\nBlock Devices:\n")
    for _, disk := range block.Disks {
        fmt.Printf("  Name: %s\n", disk.Name)
        fmt.Printf("    Model: %s\n", disk.Model)
        fmt.Printf("    SerialNumber: %s\n", disk.SerialNumber)
        fmt.Printf("    SizeBytes: %d\n", disk.SizeBytes)
        fmt.Printf("    Partitions: %v\n", disk.Partitions)
    }

    // 获取网络控制器信息
    net, err := ghw.Network()
    if err != nil {
        fmt.Printf("Error getting network info: %v", err)
    }
    fmt.Printf("\nNetwork Controllers:\n")
    for _, nic := range net.NICs {
        fmt.Printf("  Name: %s\n", nic.Name)
        fmt.Printf("    MacAddress: %s\n", nic.MacAddress)
        fmt.Printf("    IsVirtual: %v\n", nic.IsVirtual)
    }

}

三、编译打包build_all.sh脚本

#!/bin/bash

export GOROOT=/usr/local/go
export GOPATH=/opt/go/
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

# 定义目标操作系统和架构
targets=(
  "darwin/amd64"
  "darwin/arm64"
  "linux/386"
  "linux/amd64"
  "linux/arm"
  "linux/arm64"
  "windows/386"
  "windows/amd64"
)

# 获取Go文件名
go_file=$(ls *.go)
binary_name="${go_file%.*}"

# 编译目标文件
for target in "${targets[@]}"; do
  os_name=${target%/*}
  arch_name=${target#*/}

  output_name="${binary_name}_${os_name}_${arch_name}"
  if [ "$os_name" == "windows" ]; then
    output_name="${output_name}.exe"
  fi

  if [ "$os_name" == "linux" ]; then
    env GOOS=$os_name GOARCH=$arch_name go build -ldflags "-linkmode external -extldflags '-static'" -o $output_name $go_file
  else
    env GOOS=$os_name GOARCH=$arch_name go build -o $output_name $go_file
  fi

  if [ $? -eq 0 ]; then
    echo "Successfully built $output_name"
  else
    echo "Failed to build $output_name"
  fi
done

  • 编译
go mod init hardinfo
./build_all.sh

四、下载

https://d2.sddts.cn/download/jingan/hardinfo

五、测试

(1)dell 工作站

root@workstation:/bin# ./hardinfo 
BIOS Information:
  Vendor: Dell Inc.
  Version: A11
  Date: 04/22/2016
Chassis Information:
  Asset Tag: 
  Serial Number: 1598888
  Type: 7
  Type Description: Tower
  Vendor: Dell Inc.
  Version: 
Baseboard Information:
  Asset Tag: 
  Serial Number: /1598888/              /
  Vendor: Dell Inc.
  Product: 
  Version: 
Product Information:
  Family: 
  Name: Precision T7610
  Serial Number: 1598888
  UUID: 4c4c4544-0035-3910-8038-b1c04f383838
  SKU: Precision T7610
  Vendor: Dell Inc.
  Version: 01
CPU:
  Model: Intel(R) Xeon(R) CPU E5-2696 v2 @ 2.50GHz
  TotalCores: 24
  TotalThreads: 48
  Processors:
    ID: 0
    Vendor: GenuineIntel
    Model: Intel(R) Xeon(R) CPU E5-2696 v2 @ 2.50GHz
    Cores: 12
    Threads: 24
    ID: 1
    Vendor: GenuineIntel
    Model: Intel(R) Xeon(R) CPU E5-2696 v2 @ 2.50GHz
    Cores: 12
    Threads: 24

Memory:
  TotalUsableBytes: 135100547072
  TotalPhysicalBytes: 139586437120
  SupportedPageSizes: [1073741824 2097152]

Block Devices:
  Name: dm-0
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 21474836480
    Partitions: []
  Name: dm-1
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 21474836480
    Partitions: []
  Name: dm-10
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 21474836480
    Partitions: []
  Name: dm-11
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 53687091200
    Partitions: []
  Name: dm-12
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 21474836480
    Partitions: []
  Name: dm-13
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 214748364800
    Partitions: []
  Name: dm-14
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 21474836480
    Partitions: []
  Name: dm-15
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 86000009216
    Partitions: []
  Name: dm-16
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 21474836480
    Partitions: []
  Name: dm-17
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 214748364800
    Partitions: []
  Name: dm-18
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 107374182400
    Partitions: []
  Name: dm-19
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 21474836480
    Partitions: []
  Name: dm-2
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 21474836480
    Partitions: []
  Name: dm-20
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 8589934592
    Partitions: []
  Name: dm-21
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 103079215104
    Partitions: []
  Name: dm-22
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 3825205248
    Partitions: []
  Name: dm-23
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 374538764288
    Partitions: []
  Name: dm-24
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 374538764288
    Partitions: []
  Name: dm-25
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 374538764288
    Partitions: []
  Name: dm-26
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 34359738368
    Partitions: []
  Name: dm-27
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 21474836480
    Partitions: []
  Name: dm-28
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 107374182400
    Partitions: []
  Name: dm-29
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 21474836480
    Partitions: []
  Name: dm-3
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 21474836480
    Partitions: []
  Name: dm-30
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 21474836480
    Partitions: []
  Name: dm-31
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 53687091200
    Partitions: []
  Name: dm-32
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 34359738368
    Partitions: []
  Name: dm-4
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 21474836480
    Partitions: []
  Name: dm-5
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 21474836480
    Partitions: []
  Name: dm-6
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 21474836480
    Partitions: []
  Name: dm-7
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 21474836480
    Partitions: []
  Name: dm-8
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 21474836480
    Partitions: []
  Name: dm-9
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 21474836480
    Partitions: []
  Name: nvme0c0n1
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 1000081457152
    Partitions: []
  Name: nvme0n1
    Model: WD_BLACK AN1500
    SerialNumber: WUBT21211244
    SizeBytes: 1000081457152
    Partitions: []
  Name: sda
    Model: Netac_SSD_512GB
    SerialNumber: AA000000000570000602
    SizeBytes: 512110190592
    Partitions: [sda1 (1007KB)  sda2 (1GB) [vfat] sda3 (476GB) [LVM2_member]]
WARNING: ethtool not installed. Cannot grab NIC capabilities

Network Controllers:
  Name: bonding_masters
    MacAddress: 
    IsVirtual: false
  Name: enp0s25
    MacAddress: 90:b1:1c:a7:b8:c4
    IsVirtual: false
  Name: enp7s0
    MacAddress: 90:b1:1c:a7:bc:1d
    IsVirtual: false
  Name: fwbr113i0
    MacAddress: 
    IsVirtual: true
  Name: fwbr116i0
    MacAddress: 
    IsVirtual: true
  Name: fwbr117i0
    MacAddress: 
    IsVirtual: true
  Name: fwbr118i0
    MacAddress: 
    IsVirtual: true
  Name: fwbr217i0
    MacAddress: 
    IsVirtual: true
  Name: fwbr220i0
    MacAddress: 
    IsVirtual: true
  Name: fwbr220i1
    MacAddress: 
    IsVirtual: true
  Name: fwln113i0
    MacAddress: 
    IsVirtual: true
  Name: fwln116i0
    MacAddress: 
    IsVirtual: true
  Name: fwln117i0
    MacAddress: 
    IsVirtual: true
  Name: fwln118i0
    MacAddress: 
    IsVirtual: true
  Name: fwln217i0
    MacAddress: 
    IsVirtual: true
  Name: fwln220i0
    MacAddress: 
    IsVirtual: true
  Name: fwln220i1
    MacAddress: 
    IsVirtual: true
  Name: fwpr113p0
    MacAddress: 
    IsVirtual: true
  Name: fwpr116p0
    MacAddress: 
    IsVirtual: true
  Name: fwpr117p0
    MacAddress: 
    IsVirtual: true
  Name: fwpr118p0
    MacAddress: 
    IsVirtual: true
  Name: fwpr217p0
    MacAddress: 
    IsVirtual: true
  Name: fwpr220p0
    MacAddress: 
    IsVirtual: true
  Name: fwpr220p1
    MacAddress: 
    IsVirtual: true
  Name: tap113i0
    MacAddress: 
    IsVirtual: true
  Name: tap116i0
    MacAddress: 
    IsVirtual: true
  Name: tap117i0
    MacAddress: 
    IsVirtual: true
  Name: tap118i0
    MacAddress: 
    IsVirtual: true
  Name: tap217i0
    MacAddress: 
    IsVirtual: true
  Name: tap220i0
    MacAddress: 
    IsVirtual: true
  Name: tap220i1
    MacAddress: 
    IsVirtual: true
  Name: vmbr0
    MacAddress: 
    IsVirtual: true

(2)dell vostr 3710

[root@192-168-124-151 bin]# hardinfo_linux_amd64 
BIOS Information:
  Vendor: Dell Inc.
  Version: 1.5.2
  Date: 09/01/2022
Chassis Information:
  Asset Tag: 
  Serial Number: 25SM4T3
  Type: 3
  Type Description: Desktop
  Vendor: Dell Inc.
  Version: 
Baseboard Information:
  Asset Tag: 
  Serial Number: /25SM4T3/CNFCW0027L045Y/
  Vendor: Dell Inc.
  Product: 072TMP
  Version: A00
WARNING: Unable to read product_family: open /sys/class/dmi/id/product_family: no such file or directory
WARNING: Unable to read product_sku: open /sys/class/dmi/id/product_sku: no such file or directory
Product Information:
  Family: unknown
  Name: Vostro 3710
  Serial Number: 25SM4T3
  UUID: 4C4C4544-0035-5310-804D-B2C04F345433
  SKU: unknown
  Vendor: Dell Inc.
  Version: 
CPU:
  Model: 12th Gen Intel(R) Core(TM) i5-12400
  TotalCores: 6
  TotalThreads: 12
  Processors:
    ID: 0
    Vendor: GenuineIntel
    Model: 12th Gen Intel(R) Core(TM) i5-12400
    Cores: 6
    Threads: 12

Memory:
  TotalUsableBytes: 67234996224
  TotalPhysicalBytes: 68585259008
  SupportedPageSizes: [1073741824 2097152]

Block Devices:
  Name: nvme0n1
    Model: Micron 2210S NVMe 512GB
    SerialNumber:         22273969C79A
    SizeBytes: 512110190592
    Partitions: [nvme0n1p1 (500MB) [vfat] mounted@/boot/efi nvme0n1p2 (450GB) [xfs] mounted@/ nvme0n1p3 (27GB) [swap]]

Network Controllers:
  Name: br0
    MacAddress: 
    IsVirtual: true
  Name: eth0
    MacAddress: cc:96:e5:00:ee:c0
    IsVirtual: false
  Name: virbr0
    MacAddress: 
    IsVirtual: true
  Name: virbr0-nic
    MacAddress: 
    IsVirtual: true
  Name: vnet0
    MacAddress: 
    IsVirtual: true
  Name: vnet1
    MacAddress: 
    IsVirtual: true
  Name: vnet10
    MacAddress: 
    IsVirtual: true
  Name: vnet2
    MacAddress: 
    IsVirtual: true
  Name: vnet3
    MacAddress: 
    IsVirtual: true
  Name: vnet4
    MacAddress: 
    IsVirtual: true
  Name: vnet5
    MacAddress: 
    IsVirtual: true
  Name: vnet6
    MacAddress: 
    IsVirtual: true
  Name: vnet7
    MacAddress: 
    IsVirtual: true
  Name: vnet8
    MacAddress: 
    IsVirtual: true
  Name: vnet9
    MacAddress: 
    IsVirtual: true

(3)inspur

[root@localhost bin]# /bin/hardinfo_linux_amd64 
BIOS Information:
  Vendor: American Megatrends Inc.
  Version: 4.1.14
  Date: 06/21/2018
Chassis Information:
  Asset Tag: xxxx
  Serial Number: 0
  Type: 23
  Type Description: Rack mount chassis
  Vendor: Inspur
  Version: 0
Baseboard Information:
  Asset Tag: xxxx
  Serial Number: xxxx
  Vendor: Inspur
  Product: xxxx
  Version: NP5570M4
Product Information:
  Family: Default string
  Name: NP5570M4
  Serial Number: xxxx
  UUID: xxxx
  SKU: Default string
  Vendor: Inspur
  Version: 01
CPU:
  Model: Intel(R) Xeon(R) CPU E5-2609 v4 @ 1.70GHz
  TotalCores: 8
  TotalThreads: 8
  Processors:
    ID: 0
    Vendor: GenuineIntel
    Model: Intel(R) Xeon(R) CPU E5-2609 v4 @ 1.70GHz
    Cores: 8
    Threads: 8

Memory:
  TotalUsableBytes: 7504183296
  TotalPhysicalBytes: 8455716864
  SupportedPageSizes: [1073741824 2097152]

Block Devices:
  Name: dm-0
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 214748364800
    Partitions: []
  Name: dm-1
    Model: unknown
    SerialNumber: unknown
    SizeBytes: 2676834172928
    Partitions: []
  Name: sda
    Model: INSPUR
    SerialNumber: 66c92bf0002e911624d329ae038d54aa
    SizeBytes: 107374116864
    Partitions: [sda1 (300MB) [vfat] mounted@/boot/efi sda2 (1024MB) [ext4] mounted@/boot sda3 (10GB) [swap] sda4 (89GB) [ext4] mounted@/]
  Name: sdb
    Model: INSPUR
    SerialNumber: 66c92bf0002e911624d32a0408afdc9d
    SizeBytes: 2891586797568
    Partitions: []
  Name: sr0
    Model: ATAPI_DVD_D_DH16D8SH
    SerialNumber: 2L8849501767
    SizeBytes: 1073741312
    Partitions: []

Network Controllers:
  Name: eno1
    MacAddress: 6c:92:bf:c6:06:38
    IsVirtual: false
  Name: enp4s0f1
    MacAddress: 6c:92:bf:c6:06:39
    IsVirtual: false

(5)Nettrix

D:\360安全浏览器下载>hardinfo_windows_amd64.exe
BIOS Information:
  Vendor: American Megatrends Inc.
  Version: ALASKA - 1072009
  Date:
Chassis Information:
  Asset Tag: System Enclosure 0
  Serial Number: N/A
  Type: unknown
  Type Description:
  Vendor: Nettrix
  Version: To Be Filled By O.E.M.
Baseboard Information:
  Asset Tag: Base Board
  Serial Number: xxxx1880AH01xxxx
  Vendor: Nettrix
  Product: 60P16-US
  Version: 2400xxxx
Product Information:
  Family: unknown
  Name: R420 G30
  Serial Number: 980016130257xxxx
  UUID: 0BB9DEF7-BF02-11EB-8DE6-0894EFF7xxxx
  SKU:
  Vendor: Nettrix
  Version: Purley
CPU:
  Model: Intel(R) Xeon(R) Silver 4210R CPU @ 2.40GHz
  TotalCores: 10
  TotalThreads: 20
  Processors:
    ID: 0
    Vendor: GenuineIntel
    Model: Intel(R) Xeon(R) Silver 4210R CPU @ 2.40GHz
    Cores: 10
    Threads: 20

Memory:
  TotalUsableBytes: 68354887680
  TotalPhysicalBytes: 68719476736
  SupportedPageSizes: []
    Vendor: Micron
    SerialNumber: C832A5AC
    SizeBytes: 17179869184
    Vendor: Micron
    SerialNumber: C332A5AC
    SizeBytes: 17179869184
    Vendor: Micron
    SerialNumber: BE32A5AC
    SizeBytes: 17179869184
    Vendor: Micron
    SerialNumber: C932A5AC
    SizeBytes: 17179869184

Block Devices:
  Name: \\.\PHYSICALDRIVE0
    Model: AVAGO MR9361-8i SCSI Disk Device
    SerialNumber: 00dd6f8619ab93bb2d30a72811b00506
    SizeBytes: 12000675294720
    Partitions: [C: (560GB) [GPT: Basic Data] mounted@C: D: (11TB) [GPT: Basic Data] mounted@D:]

Network Controllers:
  Name: Microsoft Kernel Debug Network Adapter
    MacAddress:
    IsVirtual: true
  Name: 以太网 - Intel(R) Ethernet Connection X722 for 10GbE SFP+
    MacAddress: 08:94:EF:F7:3D:AE
    IsVirtual: false
  Name: 以太网 2 - Intel(R) Ethernet Connection X722 for 10GbE SFP+
    MacAddress: 08:94:EF:F7:3D:AD
    IsVirtual: false
  Name: 以太网 3 - Intel(R) Ethernet Connection X722 for 1GbE
    MacAddress: 08:94:EF:F7:3D:AF
    IsVirtual: false
  Name: 以太网 4 - Intel(R) Ethernet Connection X722 for 1GbE
    MacAddress: 08:94:EF:F7:3D:B0
    IsVirtual: false
  Name: Microsoft ISATAP Adapter
    MacAddress:
    IsVirtual: true
  Name: Microsoft Teredo Tunneling Adapter
    MacAddress:
    IsVirtual: true
    
最后修改:2024 年 06 月 05 日
如果觉得我的文章对你有用,请随意赞赏