linux 获取cpu和硬盘id

直接代码,有的电脑cpuid可能没有,代码头文件没有理清但是满足编译需求

std::string getcpuid() 来获取cpu id信息
std::string get_harddisk_info()来获取硬盘id


#include <unistd.h>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <arpa/inet.h>
#include <string>
#include <fstream>
#include <ifaddrs.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
//#include <linux/if.h>
#include <stdio.h>
#include <linux/hdreg.h>
#include <sys/stat.h>
#include <fcntl.h> 
#include <net/if.h>
#include <sys/types.h>
#include <stdlib.h>
//
std::string getcpuid()
{
   std::string strCPUId;
   unsigned long s1,s2;
   char buf[32] = {0};

   asm volatile(
       "movl $0x01, %%eax;"
       "xorl %%edx, %%edx;"
       "cpuid;"
       "movl %%edx, %0;"
       "movl %%eax, %1;"
       :"=m"(s1), "=m"(s2)
   );

   std::cout << "" << std::endl;
   if (s1) {
       memset(buf, 0, 32);
       snprintf(buf, 32, "%08X", s1);
       strCPUId += buf;
   }

   //std::cout << "cpuid 2" << std::endl;
   if (s2) {
       memset(buf, 0, 32);
       snprintf(buf, 32, "%08X", s2);
       strCPUId += buf;
   }

   std::cout << "" << std::endl;
   /*
   asm volatile(
       "movl $0x03, %%eax;"
       "xorl %%ecx, %%ecx;"
       "xorl %%edx, %%edx;"
       "cpuid;"
       "movl %%edx, %0;"
       "movl %%ecx, %1;"
       :"=m"(s1), "=m"(s2)
   );

         std::cout << "cpuid 4" << std::endl;
   if (s1) {
       memset(buf, 0, 32);
       snprintf(buf, 32, "%08X", s1);
       strCPUId += buf;
   }

         std::cout << "cpuid 5" << std::endl;
   if (s2) {
       memset(buf, 0, 32);
       snprintf(buf, 32, "%08X", s2);
       strCPUId += buf;
   }
   */
   return strCPUId;
}

static void parse_board_serial(const char * file_name, const char * match_words, std::string & board_serial)
{
    board_serial.c_str();
 
    std::ifstream ifs(file_name, std::ios::binary);
    if (!ifs.is_open())
    {
        return;
    }
 
    char line[4096] = { 0 };
    while (!ifs.eof())
    {
        ifs.getline(line, sizeof(line));
        if (!ifs.good())
        {
            break;
        }
 
        const char * board = strstr(line, match_words);
        if (NULL == board)
        {
            continue;
        }
        board += strlen(match_words);
 
        while ('\0' != board[0])
        {
            if (' ' != board[0])
            {
                board_serial.push_back(board[0]);
            }
            ++board;
        }
 
        if ("None" == board_serial)
        {
            board_serial.clear();
            continue;
        }
 
        if (!board_serial.empty())
        {
            break;
        }
    }
 
    ifs.close();
}
 
static bool get_board_serial_by_system(std::string & board_serial)
{
    board_serial.c_str();
 
    const char * dmidecode_result = ".dmidecode_result.txt";
    char command[512] = { 0 };
    snprintf(command, sizeof(command), "sudo dmidecode -t 2 | grep Serial > %s", dmidecode_result);
 
    if (0 == system(command))
    {
        parse_board_serial(dmidecode_result, "Serial Number:", board_serial);
    }
 
    unlink(dmidecode_result);
 
    return(!board_serial.empty());
}
 
static bool get_board_serial_number(std::string & board_serial)
{
    //if (0 == getuid())
    {
        if (get_board_serial_by_system(board_serial))
        {
            return(true);
        }
    }
    return(false);
}
 
std::string get_harddisk_info()
{
    std::string board_serial;
    if (get_board_serial_number(board_serial))
    {
        //printf("board_serial: [%s]\n", board_serial.c_str());

        return board_serial;
    }
    else
    {
        //printf("can not get board id\n");
    }

    return "";
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在golang中,要完整详细地获取CPU ID硬盘等各种硬件信息,通常需要使用操作系统(OS)提供的相关接口和系统工具。下面分别介绍如何获取这些信息: 1. 获取CPU ID: 要获取CPU ID,可以使用golang的相关库如`github.com/shirou/gopsutil/cpu`。 该库提供了获取CPU信息的接口。下面是一个示例代码片段: ```go package main import ( "fmt" "github.com/shirou/gopsutil/cpu" ) func main() { infos, err := cpu.Info() if err != nil { fmt.Printf("Failed to get CPU info: %v", err) return } for _, info := range infos { fmt.Printf("CPU ID: %v\n", info.SerialNumber) // 获取CPU序列号 } } ``` 2. 获取硬盘信息: 要获取硬盘信息,可以使用golang的库`github.com/shirou/gopsutil/disk`。该库提供了获取硬盘信息的接口。下面是一个示例代码片段: ```go package main import ( "fmt" "github.com/shirou/gopsutil/disk" ) func main() { partitions, err := disk.Partitions(false) // 获取分区信息 if err != nil { fmt.Printf("Failed to get disk partitions: %v", err) return } for _, partition := range partitions { fmt.Printf("Partition: %v\n", partition.Device) info, err := disk.Usage(partition.Mountpoint) // 获取分区使用情况 if err != nil { fmt.Printf("Failed to get disk usage: %v", err) continue } fmt.Printf("Usage: %v\n", info.Used) } } ``` 以上代码用于获取硬盘的分区信息和使用情况。 除了使用golang的库,也可以通过调用外部系统工具的方式获取硬件信息。例如,可以使用`os/exec`库调用`wmic`命令(Windows环境下)或`lshw`命令(Linux环境下)来获取更详细的硬件信息。 例如,在Linux环境下,可以使用以下代码来获取硬件信息: ```go package main import ( "bytes" "fmt" "os/exec" ) func main() { cmd := exec.Command("lshw", "-short") var out bytes.Buffer cmd.Stdout = &out err := cmd.Run() if err != nil { fmt.Printf("Failed to get hardware info: %v", err) return } fmt.Printf("Hardware Info:\n%s", out.String()) } ``` 以上代码使用`lshw -short`命令获取硬件信息。 需要注意的是,具体获取硬件信息的方式和结果可能因操作系统而异。可以根据实际情况选择合适的方法来获取硬件信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可峰科技

生活不易

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值