linux下用代码获取主板序列号


/* ************************************************************************
*       Filename:  system_test.c
*    Description:
*        Version:  1.0
*        Created:  2015年04月12日 10时41分49秒
*       Revision:  none
*       Compiler:  gcc
*         Author:  YOUR NAME (),
*        Company:
* ************************************************************************/


#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
int main( int argc, char *argv[] )
{
	pid_t	pid;
	int	ret	= 0;
	int	fd[2]	= { 0 };

/* 创建管道 */
	ret = pipe( fd );
	if ( ret == -1 )
	{
		perror( "pipe" );
		_exit( 1 );
	}

/* 创建子进程,目的  1exec 2复制管道文件描述符 */
	pid = vfork();
	if ( pid < 0 )
	{
		perror( "vfork" );
	}else if ( pid == 0 )
	{
		dup2( fd[1], 1 );                       /* 标准输出重定向到管道的写端 */

		char str[50] = "dmidecode -s system-serial-number";
		execlp( "/bin/sh", "sh", "-c", str, NULL );
	}else  {
		char result[100] = "";
		read( fd[0], result, sizeof(result) );  /* 从管道的读端读取数据 */

		char msg[100] = "";
		sprintf( msg, "%c%c:%c%c:%c%c:%c%c:%c%c:%c%c:%c%c:%c%c:%c%c:%c%c:%c%c:%c%c:%c%c:%c%c:%c%c:%c%c",
			 result[7], result[8], result[10], result[11], result[13], result[14], result[16], result[17],
			 result[19], result[20], result[22], result[23], result[25],
			 result[26], result[28], result[29], result[31], result[32],
			 result[34], result[35], result[37], result[38], result[40],
			 result[41], result[43], result[44], result[46], result[47],
			 result[49], result[50], result[52], result[53] );
		printf( "---->%s\n", msg );
	}
	return(0);
}


 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值