linux 获取cpu和硬盘id

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

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

std::string get_harddisk_info()来获取硬盘id

cpp 复制代码
#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 小时前
Linux基础项目开发1:量产工具——显示系统
linux·运维·服务器·韦东山量产工具
韩楚风7 小时前
【linux 多进程并发】linux进程状态与生命周期各阶段转换,进程状态查看分析,助力高性能优化
linux·服务器·性能优化·架构·gnu
陈苏同学7 小时前
4. 将pycharm本地项目同步到(Linux)服务器上——深度学习·科研实践·从0到1
linux·服务器·ide·人工智能·python·深度学习·pycharm
Ambition_LAO7 小时前
解决:进入 WSL(Windows Subsystem for Linux)以及将 PyCharm 2024 连接到 WSL
linux·pycharm
Pythonliu77 小时前
茴香豆 + Qwen-7B-Chat-Int8
linux·运维·服务器
你疯了抱抱我7 小时前
【RockyLinux 9.4】安装 NVIDIA 驱动,改变分辨率,避坑版本。(CentOS 系列也能用)
linux·运维·centos
追风赶月、7 小时前
【Linux】进程地址空间(初步了解)
linux
栎栎学编程7 小时前
Linux中环境变量
linux
挥剑决浮云 -8 小时前
Linux 之 安装软件、GCC编译器、Linux 操作系统基础
linux·服务器·c语言·c++·经验分享·笔记
小O_好好学9 小时前
CentOS 7文件系统
linux·运维·centos