c语言获取电脑信息

头铁,程序防止被盗,加一个硬件信息绑定一下,两行代码就能搞定,我得弄快点。。。

好吧,被教育了,压力好大的两行代码。。。

头文件

#ifndef __COMPUTER_INFO_H__
#define __COMPUTER_INFO_H__
int get_board_serial_number(char *board_serial);
int get_mac_address(char *mac_address);
int get_cpu_id(char *cpu_id);
#endif

源文件

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h> /* Definition of AT_* constants */
#include <unistd.h>

#include <net/if.h>
#include <net/if_arp.h>
#include <sys/ioctl.h>
#include <errno.h>

#include <sys/types.h>
#include <sys/stat.h>

static int get_cpu_id(char *cpu_id)
{

    unsigned int s1 = 0;
    unsigned int s2 = 0;
    asm volatile(
        "movl $0x01, %%eax; \n\t"
        "xorl %%edx, %%edx; \n\t"
        "cpuid; \n\t"
        "movl %%edx, %0; \n\t"
        "movl %%eax, %1; \n\t"
        : "=m"(s1), "=m"(s2));
    if (0 == s1 && 0 == s2){
        return -1;
    }
    char cpu[32] = {0};
    snprintf(cpu, sizeof(cpu), "%08X%08X", htonl(s2), htonl(s1));
    strcpy(cpu_id, cpu);
    return 0;
}

int get_mac_address(char* mac_address)
{
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0){
        return -1;
    }

    struct ifreq ifr = {0};
    strncpy(ifr.ifr_name, "enp0s31f6", sizeof(ifr.ifr_name) - 1);
    int ret = (ioctl(sock, SIOCGIFHWADDR, &ifr) >= 0);

    close(sock);

    const char hex[] =
        {
            '0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    char mac[16] = {0};
    for (int index = 0; index < 6; ++index)
    {
        size_t value = ifr.ifr_hwaddr.sa_data[index] & 0xFF;
        mac[2 * index + 0] = hex[value / 16];
        mac[2 * index + 1] = hex[value % 16];
    }
    // std::string(mac).swap(mac_address);
    strcpy(mac_address, mac);

    return (ret);
}

static int parse_board_serial(const char *file_name, const char *match_words, char* board_serial)
{
    FILE *fp = fopen(file_name, "r");
    if(fp == NULL){
        return -1;
    }
    char *line = NULL; // = {0};
    size_t len;
    ssize_t read;
    while ((read = getline(&line, &len, fp)) != -1){
        const char *board = strstr(line, match_words);
        if (NULL == board){
            return -2;
        }
        board += strlen(match_words);
        strcpy(board_serial, board);
        if (strcmp(board_serial,"None")==0){
            memset(board_serial, 0, strlen(board_serial));
            continue;
        }
        if(strlen(board_serial) >0){
            break;
        }
    }

    fclose(fp);
    return 0;
}

static int get_board_serial_by_system(char* board_serial)
{

    const char *dmidecode_result = ".dmidecode_result.txt";
    char command[512] = {0};
    snprintf(command, sizeof(command), "dmidecode -t 2 | grep Serial > %s", dmidecode_result);

    if (0 == system(command)){
        parse_board_serial(dmidecode_result, "Serial Number:", board_serial);
    }

    unlink(dmidecode_result);
    if(board_serial == 0){

    }

    if (strlen(board_serial) > 0){
        return 0;
    }else{
        return -1;
    }
}

int get_board_serial_number(char *board_serial)
{
    if (0 == getuid()){
        if (!get_board_serial_by_system(board_serial)){
            return 0;
        }
    }
    return -1;
}

另外一个版本的源代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h> /* Definition of AT_* constants */
#include <net/if.h>
#include <net/if_arp.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "darknet.h"
int del_space(char *src)
{
    char *pTmp = src;
    unsigned int iSpace = 0;
 
    while (*src != '\0') {
        if (*src != ' ' && *src != '\n' && *src != '\t') {
            *pTmp++ = *src;
        }else{
            iSpace++;
        }
        src++;
    }
 
    *pTmp = '\0';
    return iSpace;
}
int get_cpu_id(char *cpu_id)
{

    unsigned int s1 = 0;
    unsigned int s2 = 0;
    asm volatile(
        "movl $0x01, %%eax; \n\t"
        "xorl %%edx, %%edx; \n\t"
        "cpuid; \n\t"
        "movl %%edx, %0; \n\t"
        "movl %%eax, %1; \n\t"
        : "=m"(s1), "=m"(s2));
    if (0 == s1 && 0 == s2){
        return -1;
    }
    char cpu[32] = {0};
    snprintf(cpu, sizeof(cpu), "%08X%08X", htonl(s2), htonl(s1));
    strcpy(cpu_id, cpu);
    // del_space(cpu_id);
    return 0;
}

static int parse_serial(const char *file_name, const char *match_words, char *serial)
{
    FILE *fp = fopen(file_name, "r");
    if (fp == NULL)
    {
        return -1;
    }
    char *line = NULL; // = {0};
    size_t len;
    ssize_t read;
    while ((read = getline(&line, &len, fp)) != -1)
    {
        char temp[128];
        const char *board = strstr(line, match_words);
        if (NULL == board){
            return -2;
        }
        board += strlen(match_words);
        strcpy(temp, board);
        del_space(temp);
        if (strcmp(temp, "None") == 0)
        {
            memset(temp, 0, strlen(temp));
            continue;
        }
        strcat(serial, temp);
    }

    fclose(fp);
    return 0;
}

int get_mac_address2(char* mac_address)
{
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0){
        return -1;
    }

    struct ifreq ifr = {0};
    strncpy(ifr.ifr_name, "enp0s31f6", sizeof(ifr.ifr_name) - 1);
    int ret = (ioctl(sock, SIOCGIFHWADDR, &ifr) >= 0);

    close(sock);

    const char hex[] =
        {
            '0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    char mac[16] = {0};
    for (int index = 0; index < 6; ++index)
    {
        size_t value = ifr.ifr_hwaddr.sa_data[index] & 0xFF;
        mac[2 * index + 0] = hex[value / 16];
        mac[2 * index + 1] = hex[value % 16];
    }
    // std::string(mac).swap(mac_address);
    strcpy(mac_address, mac);
    del_space(mac_address);
    return !(ret);
}

int get_mac_address(char *mac_address)
{
    memset(mac_address,0, strlen(mac_address));
    const char *dmidecode_result = ".dmidecode_result.txt";
    char command[512] = {0};
    snprintf(command, sizeof(command), "ip addr| grep link/ether > %s", dmidecode_result);
    if (0 == system(command)){
        parse_serial(dmidecode_result, "link/ether", mac_address + strlen(mac_address));
    }
    unlink(dmidecode_result);

    if (strlen(mac_address) > 0){
        return 0;
    }else{
        return -1;
    }
}

static int get_board_serial_by_system(char* board_serial)
{
    const char *dmidecode_result = ".dmidecode_result.txt";
    char command[512] = {0};
    snprintf(command, sizeof(command), "dmidecode -t 2 | grep Serial > %s", dmidecode_result);

    if (0 == system(command)){
        parse_serial(dmidecode_result, "Serial Number:", board_serial);
    }

    unlink(dmidecode_result);
    if(board_serial == 0){

    }

    if (strlen(board_serial) > 0){
        return 0;
    }else{
        return -1;
    }
}

int get_board_serial_number(char *board_serial)
{
    if (0 == getuid()){
        if (!get_board_serial_by_system(board_serial)){
            return 0;
        }
    }
    return -1;
}

技拙,把这两行代码粘贴出来了。

感谢网友的分享,让我填坑。


参考:
-Linux C学习–getline()函数
-linux下获取CPUID,MAC地址,硬盘序列号,主板序列号

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值