1. 程式人生 > >centos 7 下獲取CPUID,MAC地址,主機板序列號

centos 7 下獲取CPUID,MAC地址,主機板序列號

獲取CPUID:

#include <cstdio>  
#include <cstring>  
#include <cstdlib>  
#include <arpa/inet.h>  
#include <string>  
#include <fstream>  
#include <unistd.h>
#include <sys/types.h>

static bool get_cpu_id_by_asm(std::string & cpu_id)  
{  
    cpu_id.clear();  

    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
(false); } char cpu[32] = { 0 }; snprintf(cpu, sizeof(cpu), "%08X%08X", htonl(s2), htonl(s1)); std::string(cpu).swap(cpu_id); return(true); } static void parse_cpu_id(const char * file_name, const char * match_words, std::string & cpu_id) { cpu_id.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 * cpu = strstr(line, match_words); if (NULL == cpu) { continue; } cpu += strlen(match_words); while ('\0' != cpu[0]) { if (' ' != cpu[0]) { cpu_id.push_back(cpu[0]); } ++cpu; } if (!cpu_id.empty()) { break; } } ifs.close(); } static bool get_cpu_id_by_system(std::string & cpu_id) { cpu_id.c_str(); const char * dmidecode_result = ".dmidecode_result.txt"; char command[512] = { 0 }; snprintf(command, sizeof(command), "dmidecode -t 4 | grep ID > %s", dmidecode_result); if (0 == system(command)) { parse_cpu_id(dmidecode_result, "ID:", cpu_id); } unlink(dmidecode_result); return(!cpu_id.empty()); } static bool get_cpu_id(std::string & cpu_id) { if (get_cpu_id_by_asm(cpu_id)) { return(true); } if (0 == getuid()) { if (get_cpu_id_by_system(cpu_id)) { return(true); } } return(false); } static void test_1() { std::string cpu_id; if (get_cpu_id(cpu_id)) { printf("cpu_id: [%s]\n", cpu_id.c_str()); } else { printf("can not get cpu id\n"); } } static void test_2() { { std::string cpu_id; if (get_cpu_id_by_asm(cpu_id)) { printf("cpu_id_by_asm: [%s]\n", cpu_id.c_str()); } else { printf("can not get cpu id\n"); } } { std::string cpu_id; if (get_cpu_id_by_system(cpu_id)) { printf("cpu_id_by_sys: [%s]\n", cpu_id.c_str()); } else { printf("can not get cpu id\n"); } } } int main(int argc, char* argv[]) { test_1(); test_2(); return(0); }

獲取MAC地址:
注意網絡卡名稱,需使用本機實際的名稱

#include <cstdio>  
#include <cstring>  
#include <cstdlib>  
#include <unistd.h>  
#include <net/if.h>  
#include <sys/ioctl.h>  
#include <sys/types.h>  
#include <sys/socket.h>  
#include <string>  
#include <fstream>  

bool get_mac_address_by_ioctl(std::string & mac_address)  
{  
    mac_address.clear();  

    int sock = socket(AF_INET, SOCK_STREAM, 0);  
    if (sock < 0)  
    {  
        return(false);  
    }  

    struct ifreq ifr = { 0 };  
    //注意此處 eth0為網絡卡名稱,需根據實際名稱調整
    strncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name) - 1);  
    bool 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);  

    return(ret);  
}  

static void parse_mac_address(const char * file_name, const char * match_words, std::string & mac_address)  
{  
    mac_address.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 * mac = strstr(line, match_words);  
        if (NULL == mac)  
        {  
            continue;  
        }  
        mac += strlen(match_words);  

        while ('\0' != mac[0])  
        {  
            if (' ' != mac[0] && ':' != mac[0])  
            {  
                mac_address.push_back(mac[0]);  
            }  
            ++mac;  
        }  

        if (!mac_address.empty())  
        {  
            break;  
        }  
    }  

    ifs.close();  
}  

static bool get_mac_address_by_system(std::string & mac_address)  
{  
    mac_address.c_str();  

    const char * lshw_result = ".lshw_result.txt";  
    char command[512] = { 0 };  
    snprintf(command, sizeof(command), "lshw -c network | grep serial | head -n 1 > %s", lshw_result);  

    if (0 == system(command))  
    {  
        parse_mac_address(lshw_result, "serial:", mac_address);  
    }  

    unlink(lshw_result);  

    return(!mac_address.empty());  
}  

static bool get_mac_address(std::string & mac_address)  
{  
    if (get_mac_address_by_ioctl(mac_address))  
    {  
        return(true);  
    }  
    if (get_mac_address_by_system(mac_address))  
    {  
        return(true);  
    }  
    return(false);  
}  

static void test_1()  
{  
    std::string mac_address;  
    if (get_mac_address(mac_address))  
    {  
        printf("mac_address: [%s]\n", mac_address.c_str());  
    }  
    else  
    {  
        printf("can not get mac address\n");  
    }  
}  

static void test_2()  
{  
    {  
        std::string mac_address;  
        if (get_mac_address_by_ioctl(mac_address))  
        {  
            printf("mac_address: [%s]\n", mac_address.c_str());  
        }  
        else  
        {  
            printf("can not get mac address\n");  
        }  
    }  
    {  
        std::string mac_address;  
        if (get_mac_address_by_system(mac_address))  
        {  
            printf("mac_address: [%s]\n", mac_address.c_str());  
        }  
        else  
        {  
            printf("can not get mac address\n");  
        }  
    }  
}  

int main(int argc, char * argv[])  
{  
    test_1();  
    test_2();  
    return(0);  
}  

獲取主機板序列號:

#include <cstdio>  
#include <cstring>  
#include <cstdlib>  
#include <string>  
#include <fstream>  
#include <unistd.h>
#include <sys/types.h>

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), " dmidecode | grep 'Serial Number'|grep - > %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);  
}  

static void test()  
{  
    std::string board_serial;  
    if (get_board_serial_number(board_serial))  
    {  
        printf("board_serial: %s\n", board_serial.c_str());  
    }  
    else  
    {  
        printf("can not get board id\n");  
    }  
}  

int main(int argc, char* argv[])  
{  
    test();  
    return(0);  
}  

編譯:

g++ xxx.cpp -o xxx
//xxx為程式碼檔名稱

執行:

./xxx