Linux下检索系统的硬件相关信息(dmidecode)

当想要以可读格式检索系统的硬件相关信息,不得不讲一下今天的主角,dmidecod命令不仅显示系统的当前硬件配置,例如:处理器,RAM(DIMM),BIOS,内存,序列号等等,还能显示最大支持的CPU和内存。

这方便了完全不知道硬件的情况下,可以检索此信息,而不必探究实际的硬件。

dmidecode命令

我们完全不知道一个命令的时候时,可以通过man手册查看。

man dmidecode

或者我们可以https://linux.die.net/man/8/dmidecode查阅,在某些Linux / Unix系统中,dmidecode命令可能需要root权限才能运行,例如用于运行以下命令的当前Linux系统需要root权限。不然不与root权限运行将报以下信息:

在这里插入图片描述

可以看到上面显示权限被拒绝。

下面我们以root权限运行简单的dmidecode命令以获取硬件信息。

dmidecode | more

在这里插入图片描述
不带选项执行 dmidecode 通常会输出所有的硬件信息。显示的信息太多,只截屏一小段。

Dmi Types

使用dmidecode命令时,如果不加任何参数,则打印出所有类型的信息;而加上“-t type_num”或者“-t keywords”可以查看某个类型信息。

		Type   Information
		----------------------------------------
          0		BIOS
          1		System
          2		Base Board
          3		Chassis
          4		Processor
          5		Memory Controller
          6		Memory Module
          7		Cache
          8		Port Connector
          9		System Slots
         10		On Board Devices
         11		OEM Strings
         12		System Configuration Options
         13		BIOS Language
         14		Group Associations
         15		System Event Log
         16		Physical Memory Array
         17		Memory Device
         18		32-bit Memory Error
         19		Memory Array Mapped Address
         20		Memory Device Mapped Address
         21		Built-in Pointing Device
         22		Portable Battery
         23		System Reset
         24		Hardware Security
         25		System Power Controls
         26		Voltage Probe
         27		Cooling Device
         28		Temperature Probe
         29		Electrical Current Probe
         30		Out-of-band Remote Access
         31		Boot Integrity Services
         32		System Boot
         33		64-bit Memory Error
         34		Management Device
         35		Management Device Component
         36		Management Device Threshold Data
         37		Memory Channel
         38		IPMI Device
         39		Power Supply

假如我们要获取有关Baseboard的信息,我们可以执行以下任何命令。

dmidecode -t baseboard

在这里插入图片描述
或者可以这样输入,效果跟上面一样的。

dmidecode -t 2

查看主板的序列号,以C/C++方式怎么实现呢?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <net/if.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <ifaddrs.h>
#include <time.h>
#include <string>
#include <sstream>
#include <unistd.h>


//dmidecode -t 2|grep Serial|awk -F: '{print $2}'
void GetBoardSerial(char *szBuffer, size_t nBufferLen)
{
    char command[512] = {0};
    snprintf(command, sizeof(command), "%s -t %s|%s %s|%s -F: '%s'", "dmidecode", "2", "grep", "Serial", "awk","{print $2}");
    FILE *fp = popen(command, "r");
    if (fp == NULL)
	{
		printf("popen failed!\n");
		return;
	}

    fread(szBuffer, nBufferLen-1, 1, fp);
	printf("Serial Number: %s\n",szBuffer);
    fclose(fp);

}

int main(int argc, char *argv[])
{
	char szBoardSerial[64] = {0};

    GetBoardSerial(szBoardSerial, sizeof(szBoardSerial));

	return 0;
}

编译输出:
在这里插入图片描述

dmidecode命令有个很有用的选项 -t,可以按指定类型输出相关信息,假如要获得处理器方面的信息,例如:

查看内存信息: dmidecode -t memory
获取BIOS信息: dmidecode -t BIOS

获取UUID

每个系统都在系统上设置了通用唯一标识符(UUID)值。UUID是一个128位的值,通常以小写的十六进制数字显示在5个组中,并用破折号隔开。

下面我们使用dmidecode命令实用程序获取UUID

dmidecode -t 1|grep UUID|awk -F: ‘{print $2}’

使用编程语言自然能够轻松获得一个uuid(或guid),下面我们使用C/C++获取

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <net/if.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <ifaddrs.h>
#include <time.h>
#include <string>
#include <sstream>
#include <unistd.h>

void ErrorLog(const char *szErrorMsg)
{
    time_t now = time(NULL);
    struct tm lt;
    localtime_r(&now, &lt);
	const char *strLogPath = "./.__SystemUUID__.log";
    FILE *fp = fopen(strLogPath, "a+");
    if (fp == NULL)
	{		
		return;
	}	
    
    fprintf(fp, "[%04d-%02d-%02d %02d:%02d:%02d] Error: %s\n", 
                    lt.tm_year+1900, lt.tm_mon+1, lt.tm_mday, lt.tm_hour, lt.tm_min, lt.tm_sec, szErrorMsg);
    fclose(fp);
}


//dmidecode -t 1|grep UUID|awk -F: '{print $2}'
void GetSystemUUID(char *szBuffer, size_t nBufferLen)
{
    char command[512] = {0};
    snprintf(command, sizeof(command), "%s -t %s|%s %s|%s -F: '%s'", "dmidecode", "1", "grep", "UUID", "awk","{print $2}");
    FILE *fp = popen(command, "r");
    if (fp == NULL)
	{
		ErrorLog("popen failed!");
		return;
	}
    fread(szBuffer, nBufferLen-1, 1, fp);
	printf("SystemUUID: %s\n",szBuffer);
    fclose(fp);

}
int main(int argc, char *argv[])
{
    char szSystemUUID[64] = {0};

    GetSystemUUID(szSystemUUID, sizeof(szSystemUUID));

	return 0;
}




编译输出:

在这里插入图片描述

总结

当我们查询服务器的硬件参数,可以考虑使用linux系统自带工具dmidecode命令,使用时候切换到root权限,让你在Linux系统下获取有关硬件方面的信息。更多dmidecode使用可以参考man手册。

欢迎关注微信公众号【程序猿编码】,添加本人微信号(17865354792),欢迎进入技术交流群。我们一起学习进步!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值