[其它] centos7 获取CPUID,MAC地址,硬盘序列号,主板序列号命令

[复制链接]
查看: 1214|回复: 0

8

主题

8

帖子

28

积分

新手上路

Rank: 1

积分
28
发表于 2020-1-17 08:47:56 | 显示全部楼层 |阅读模式
centos7 获取CPUID,MAC地址,硬盘序列号,主板序列号命令:

1.CPUID
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <arpa/inet.h>
  5. #include <string>
  6. #include <fstream>
  7. #include <unistd.h>

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

  11.     unsigned int s1 = 0;
  12.     unsigned int s2 = 0;
  13.     asm volatile
  14.     (
  15.         "movl $0x01, %%eax; \n\t"
  16.         "xorl %%edx, %%edx; \n\t"
  17.         "cpuid; \n\t"
  18.         "movl %%edx, %0; \n\t"
  19.         "movl %%eax, %1; \n\t"
  20.         : "=m"(s1), "=m"(s2)
  21.     );
  22.     if (0 == s1 && 0 == s2)
  23.     {
  24.         return(false);
  25.     }
  26.     char cpu[32] = { 0 };
  27.     snprintf(cpu, sizeof(cpu), "%08X%08X", htonl(s2), htonl(s1));
  28.     std::string(cpu).swap(cpu_id);
  29.     return(true);
  30. }
  31. static void parse_cpu_id(const char * file_name, const char * match_words, std::string & cpu_id)
  32. {
  33.     cpu_id.c_str();
  34.     std::ifstream ifs(file_name, std::ios::binary);
  35.     if (!ifs.is_open())
  36.     {
  37.         return;
  38.     }
  39.     char line[4096] = { 0 };
  40.     while (!ifs.eof())
  41.     {
  42.         ifs.getline(line, sizeof(line));
  43.         if (!ifs.good())
  44.         {
  45.             break;
  46.         }
  47.         const char * cpu = strstr(line, match_words);
  48.         if (NULL == cpu)
  49.         {
  50.             continue;
  51.         }
  52.         cpu += strlen(match_words);
  53.         while ('\0' != cpu[0])
  54.         {
  55.             if (' ' != cpu[0])
  56.             {
  57.                 cpu_id.push_back(cpu[0]);
  58.             }
  59.             ++cpu;
  60.         }
  61.         if (!cpu_id.empty())
  62.         {
  63.             break;
  64.         }
  65.     }
  66.     ifs.close();
  67. }
  68. static bool get_cpu_id_by_system(std::string & cpu_id)
  69. {
  70.     cpu_id.c_str();
  71.     const char * dmidecode_result = ".dmidecode_result.txt";
  72.     char command[512] = { 0 };
  73.     snprintf(command, sizeof(command), "dmidecode -t 4 | grep ID > %s", dmidecode_result);
  74.     if (0 == system(command))
  75.     {
  76.         parse_cpu_id(dmidecode_result, "ID:", cpu_id);
  77.     }
  78.     unlink(dmidecode_result);
  79.     return(!cpu_id.empty());
  80. }
  81. static bool get_cpu_id(std::string & cpu_id)
  82. {
  83.     if (get_cpu_id_by_asm(cpu_id))
  84.     {
  85.         return(true);
  86.     }
  87.     if (0 == getuid())
  88.     {
  89.         if (get_cpu_id_by_system(cpu_id))
  90.         {
  91.             return(true);
  92.         }
  93.     }
  94.     return(false);
  95. }
  96. static void test_1()
  97. {
  98.     std::string cpu_id;
  99.     if (get_cpu_id(cpu_id))
  100.     {
  101.         printf("cpu_id: [%s]\n", cpu_id.c_str());
  102.     }
  103.     else
  104.     {
  105.         printf("can not get cpu id\n");
  106.     }
  107. }
  108. static void test_2()
  109. {
  110.     {
  111.         std::string cpu_id;
  112.         if (get_cpu_id_by_asm(cpu_id))
  113.         {
  114.             printf("cpu_id_by_asm: [%s]\n", cpu_id.c_str());
  115.         }
  116.         else
  117.         {
  118.             printf("can not get cpu id\n");
  119.         }
  120.     }
  121.     {
  122.         std::string cpu_id;
  123.         if (get_cpu_id_by_system(cpu_id))
  124.         {
  125.             printf("cpu_id_by_sys: [%s]\n", cpu_id.c_str());
  126.         }
  127.         else
  128.         {
  129.             printf("can not get cpu id\n");
  130.         }
  131.     }
  132. }
复制代码
2.获取MAC地址: 修复原创 命令获取时 加入000000000000 的问题
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <unistd.h>
  5. #include <net/if.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <string>
  10. #include <fstream>

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

  14.     int sock = socket(AF_INET, SOCK_STREAM, 0);
  15.     if (sock < 0)
  16.     {
  17.         return(false);
  18.     }

  19.     struct ifreq ifr = { 0 };
  20.     strncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name) - 1);
  21.     bool ret = (ioctl(sock, SIOCGIFHWADDR, &ifr) >= 0);

  22.     close(sock);

  23.     const char hex[] =
  24.     {
  25.         '0', '1', '2', '3', '4', '5', '6', '7',
  26.         '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
  27.     };
  28.     char mac[16] = { 0 };
  29.     for (int index = 0; index < 6; ++index)
  30.     {
  31.          size_t value = ifr.ifr_hwaddr.sa_data[index] & 0xFF;
  32.          mac[2 * index + 0] = hex[value / 16];
  33.          mac[2 * index + 1] = hex[value % 16];
  34.     }
  35.     std::string(mac).swap(mac_address);

  36.     return(ret);
  37. }

  38. static void parse_mac_address(const char * file_name, const char * match_words, std::string & mac_address)
  39. {
  40.     mac_address.c_str();

  41.     std::ifstream ifs(file_name, std::ios::binary);
  42.     if (!ifs.is_open())
  43.     {
  44.         return;
  45.     }

  46.     char line[4096] = { 0 };
  47.     while (!ifs.eof())
  48.     {
  49.         ifs.getline(line, sizeof(line));
  50.         if (!ifs.good())
  51.         {
  52.             break;
  53.         }

  54.         const char * mac = strstr(line, match_words);
  55.         if (NULL == mac)
  56.         {
  57.             continue;
  58.         }
  59.         mac += strlen(match_words);

  60.         while ('\0' != mac[0])
  61.         {
  62.             if (' ' != mac[0] && ':' != mac[0])
  63.             {
  64.                 mac_address.push_back(mac[0]);
  65.             }
  66.             ++mac;
  67.         }

  68.         if (!mac_address.empty())
  69.         {

  70.            mac_address= mac_address.substr(mac_address.length() - 12, mac_address.length());
  71.             break;
  72.         }
  73.     }

  74.     ifs.close();
  75. }

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

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

  82.     if (0 == system(command))
  83.     {
  84.         parse_mac_address(lshw_result, "serial:", mac_address);
  85.     }

  86.     unlink(lshw_result);

  87.     return(!mac_address.empty());
  88. }

  89. static bool get_mac_address(std::string & mac_address)
  90. {
  91.     if (get_mac_address_by_ioctl(mac_address))
  92.     {
  93.         return(true);
  94.     }
  95.     if (get_mac_address_by_system(mac_address))
  96.     {
  97.         return(true);
  98.     }
  99.     return(false);
  100. }

  101. static void test_1()
  102. {
  103.     std::string mac_address;
  104.     if (get_mac_address(mac_address))
  105.     {
  106.         printf("mac_address: [%s]\n", mac_address.c_str());
  107.     }
  108.     else
  109.     {
  110.         printf("can not get mac address\n");
  111.     }
  112. }

  113. static void test_2()
  114. {
  115.     {
  116.         std::string mac_address;
  117.         if (get_mac_address_by_ioctl(mac_address))
  118.         {
  119.             printf("mac_address: [%s]\n", mac_address.c_str());
  120.         }
  121.         else
  122.         {
  123.             printf("can not get mac address\n");
  124.         }
  125.     }
  126.     {
  127.         std::string mac_address;
  128.         if (get_mac_address_by_system(mac_address))
  129.         {
  130.             printf("mac_address: [%s]\n", mac_address.c_str());
  131.         }
  132.         else
  133.         {
  134.             printf("can not get mac address\n");
  135.         }
  136.     }
  137. }
复制代码
获取硬盘序列号:
  1. #include <cctype>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <scsi/sg.h>
  7. #include <sys/ioctl.h>
  8. #include <linux/hdreg.h>
  9. #include <string>
  10. #include <fstream>

  11. static bool get_disk_name(std::string & disk_name)
  12. {
  13.     disk_name.c_str();

  14.     std::ifstream ifs("/etc/mtab", std::ios::binary);
  15.     if (!ifs.is_open())
  16.     {
  17.         return(false);
  18.     }

  19.     char line[4096] = { 0 };
  20.     while (!ifs.eof())
  21.     {
  22.         ifs.getline(line, sizeof(line));
  23.         if (!ifs.good())
  24.         {
  25.             break;
  26.         }

  27.         const char * disk = line;
  28.         while (isspace(disk[0]))
  29.         {
  30.             ++disk;
  31.         }

  32.         const char * space = strchr(disk, ' ');
  33.         if (NULL == space)
  34.         {
  35.             continue;
  36.         }

  37.         const char * mount = space + 1;
  38.         while (isspace(mount[0]))
  39.         {
  40.             ++mount;
  41.         }
  42.         if ('/' != mount[0] || ' ' != mount[1])
  43.         {
  44.             continue;
  45.         }

  46.         while (space > disk && isdigit(space[-1]))
  47.         {
  48.             --space;
  49.         }

  50.         if (space > disk)
  51.         {
  52.             std::string(disk, space).swap(disk_name);
  53.             break;
  54.         }
  55.     }

  56.     ifs.close();

  57.     return(!disk_name.empty());
  58. }

  59. static void trim_serial(const void * serial, size_t serial_len, std::string & serial_no)
  60. {
  61.     const char * serial_s = static_cast<const char *>(serial);
  62.     const char * serial_e = serial_s + serial_len;
  63.     while (serial_s < serial_e)
  64.     {
  65.         if (isspace(serial_s[0]))
  66.         {
  67.             ++serial_s;
  68.         }
  69.         else if ('\0' == serial_e[-1] || isspace(serial_e[-1]))
  70.         {
  71.             --serial_e;
  72.         }
  73.         else
  74.         {
  75.             break;
  76.         }
  77.     }

  78.     if (serial_s < serial_e)
  79.     {
  80.         std::string(serial_s, serial_e).swap(serial_no);
  81.     }
  82. }

  83. static bool get_disk_serial_by_way_1(const std::string & disk_name, std::string & serial_no)
  84. {
  85.     serial_no.clear();

  86.     int fd = open(disk_name.c_str(), O_RDONLY);
  87.     if (-1 == fd)
  88.     {
  89.         return(false);
  90.     }

  91.     struct hd_driveid drive = { 0 };
  92.     if (0 == ioctl(fd, HDIO_GET_IDENTITY, &drive))
  93.     {
  94.         trim_serial(drive.serial_no, sizeof(drive.serial_no), serial_no);
  95.     }

  96.     close(fd);

  97.     return(!serial_no.empty());
  98. }

  99. static bool scsi_io(
  100.                 int fd, unsigned char * cdb,
  101.                 unsigned char cdb_size, int xfer_dir,
  102.                 unsigned char * data, unsigned int data_size,
  103.                 unsigned char * sense, unsigned int sense_len
  104.             )
  105. {
  106.     sg_io_hdr_t io_hdr = { 0 };
  107.     io_hdr.interface_id = 'S';
  108.     io_hdr.cmdp = cdb;
  109.     io_hdr.cmd_len = cdb_size;
  110.     io_hdr.sbp = sense;
  111.     io_hdr.mx_sb_len = sense_len;
  112.     io_hdr.dxfer_direction = xfer_dir;
  113.     io_hdr.dxferp = data;
  114.     io_hdr.dxfer_len = data_size;
  115.     io_hdr.timeout = 5000;

  116.     if (ioctl(fd, SG_IO, &io_hdr) < 0)
  117.     {
  118.         return(false);
  119.     }

  120.     if (SG_INFO_OK != (io_hdr.info & SG_INFO_OK_MASK) && io_hdr.sb_len_wr > 0)
  121.     {
  122.         return(false);
  123.     }

  124.     if (io_hdr.masked_status || io_hdr.host_status || io_hdr.driver_status)
  125.     {
  126.         return(false);
  127.     }

  128.     return(true);
  129. }

  130. static bool get_disk_serial_by_way_2(const std::string & disk_name, std::string & serial_no)
  131. {
  132.     serial_no.clear();

  133.     int fd = open(disk_name.c_str(), O_RDONLY);
  134.     if (-1 == fd)
  135.     {
  136.         return(false);
  137.     }

  138.     int version = 0;
  139.     if (ioctl(fd, SG_GET_VERSION_NUM, &version) < 0 || version < 30000)
  140.     {
  141.         close(fd);
  142.         return(false);
  143.     }

  144.     const unsigned int data_size = 0x00ff;
  145.     unsigned char data[data_size] = { 0 };
  146.     const unsigned int sense_len = 32;
  147.     unsigned char sense[sense_len] = { 0 };
  148.     unsigned char cdb[] = { 0x12, 0x01, 0x80, 0x00, 0x00, 0x00 };
  149.     cdb[3] = (data_size >> 8) & 0xff;
  150.     cdb[4] = (data_size & 0xff);

  151.     if (scsi_io(fd, cdb, sizeof(cdb), SG_DXFER_FROM_DEV, data, data_size, sense, sense_len))
  152.     {
  153.         int page_len = data[3];
  154.         trim_serial(data + 4, page_len, serial_no);
  155.     }

  156.     close(fd);

  157.     return(!serial_no.empty());
  158. }

  159. static bool parse_serial(const char * line, int line_size, const char * match_words, std::string & serial_no)
  160. {
  161.     const char * serial_s = strstr(line, match_words);
  162.     if (NULL == serial_s)
  163.     {
  164.         return(false);
  165.     }
  166.     serial_s += strlen(match_words);
  167.     while (isspace(serial_s[0]))
  168.     {
  169.         ++serial_s;
  170.     }

  171.     const char * serial_e = line + line_size;
  172.     const char * comma = strchr(serial_s, ',');
  173.     if (NULL != comma)
  174.     {
  175.         serial_e = comma;
  176.     }

  177.     while (serial_e > serial_s && isspace(serial_e[-1]))
  178.     {
  179.         --serial_e;
  180.     }

  181.     if (serial_e <= serial_s)
  182.     {
  183.         return(false);
  184.     }

  185.     std::string(serial_s, serial_e).swap(serial_no);

  186.     return(true);
  187. }

  188. static void get_serial(const char * file_name, const char * match_words, std::string & serial_no)
  189. {
  190.     serial_no.c_str();

  191.     std::ifstream ifs(file_name, std::ios::binary);
  192.     if (!ifs.is_open())
  193.     {
  194.         return;
  195.     }

  196.     char line[4096] = { 0 };
  197.     while (!ifs.eof())
  198.     {
  199.         ifs.getline(line, sizeof(line));
  200.         if (!ifs.good())
  201.         {
  202.             break;
  203.         }

  204.         if (0 == ifs.gcount())
  205.         {
  206.             continue;
  207.         }

  208.         if (parse_serial(line, ifs.gcount() - 1, match_words, serial_no))
  209.         {
  210.             break;
  211.         }
  212.     }

  213.     ifs.close();
  214. }

  215. static bool get_disk_serial_by_way_3(const std::string & disk_name, std::string & serial_no)
  216. {
  217.     serial_no.c_str();

  218.     const char * hdparm_result = ".hdparm_result.txt";
  219.     char command[512] = { 0 };
  220.     snprintf(command, sizeof(command), "hdparm -i %s | grep SerialNo > %s", disk_name.c_str(), hdparm_result);

  221.     if (0 == system(command))
  222.     {
  223.         get_serial(hdparm_result, "SerialNo=", serial_no);
  224.     }

  225.     unlink(hdparm_result);

  226.     return(!serial_no.empty());
  227. }

  228. static bool get_disk_serial_by_way_4(std::string & serial_no)
  229. {
  230.     serial_no.c_str();

  231.     const char * lshw_result = ".lshw_result.txt";
  232.     char command[512] = { 0 };
  233.     snprintf(command, sizeof(command), "lshw -class disk | grep serial > %s", lshw_result);

  234.     if (0 == system(command))
  235.     {
  236.         get_serial(lshw_result, "serial:", serial_no);
  237.     }

  238.     unlink(lshw_result);

  239.     return(!serial_no.empty());
  240. }

  241. static bool get_disk_serial_number(std::string & serial_no)
  242. {
  243.     if (0 != getuid())
  244.     {
  245.         return(false);
  246.     }

  247.     std::string disk_name;
  248.     if (get_disk_name(disk_name))
  249.     {
  250.         if (get_disk_serial_by_way_1(disk_name, serial_no))
  251.         {
  252.             return(true);
  253.         }
  254.         if (get_disk_serial_by_way_2(disk_name, serial_no))
  255.         {
  256.             return(true);
  257.         }
  258.         if (get_disk_serial_by_way_3(disk_name, serial_no))
  259.         {
  260.             return(true);
  261.         }
  262.     }
  263.     if (get_disk_serial_by_way_4(serial_no))
  264.     {
  265.         return(true);
  266.     }
  267.     return(false);
  268. }

  269. static void test_1()
  270. {
  271.     std::string serial_no;
  272.     if (get_disk_serial_number(serial_no))
  273.     {
  274.         printf("serial_number: [%s]\n", serial_no.c_str());
  275.     }
  276.     else
  277.     {
  278.         printf("get serial number failed\n");
  279.     }
  280. }

  281. static void test_2()
  282. {
  283.     std::string disk_name;
  284.     if (get_disk_name(disk_name))
  285.     {
  286.         printf("disk_name:[%s]\n", disk_name.c_str());
  287.         {
  288.             std::string serial_no;
  289.             get_disk_serial_by_way_1(disk_name, serial_no);
  290.             printf("get_serial_by_way_1:[%s]\n", serial_no.c_str());
  291.         }
  292.         {
  293.             std::string serial_no;
  294.             get_disk_serial_by_way_2(disk_name, serial_no);
  295.             printf("get_serial_by_way_2:[%s]\n", serial_no.c_str());
  296.         }
  297.         {
  298.             std::string serial_no;
  299.             get_disk_serial_by_way_3(disk_name, serial_no);
  300.             printf("get_serial_by_way_3:[%s]\n", serial_no.c_str());
  301.         }
  302.     }
  303.     {
  304.         std::string serial_no;
  305.         get_disk_serial_by_way_4(serial_no);
  306.         printf("get_serial_by_way_4:[%s]\n", serial_no.c_str());
  307.     }
  308. }
复制代码
获取主板序列号:
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <string>
  5. #include <fstream>

  6. static void parse_board_serial(const char * file_name, const char * match_words, std::string & board_serial)
  7. {
  8.     board_serial.c_str();

  9.     std::ifstream ifs(file_name, std::ios::binary);
  10.     if (!ifs.is_open())
  11.     {
  12.         return;
  13.     }

  14.     char line[4096] = { 0 };
  15.     while (!ifs.eof())
  16.     {
  17.         ifs.getline(line, sizeof(line));
  18.         if (!ifs.good())
  19.         {
  20.             break;
  21.         }

  22.         const char * board = strstr(line, match_words);
  23.         if (NULL == board)
  24.         {
  25.             continue;
  26.         }
  27.         board += strlen(match_words);

  28.         while ('\0' != board[0])
  29.         {
  30.             if (' ' != board[0])
  31.             {
  32.                 board_serial.push_back(board[0]);
  33.             }
  34.             ++board;
  35.         }

  36.         if ("None" == board_serial)
  37.         {
  38.             board_serial.clear();
  39.             continue;
  40.         }

  41.         if (!board_serial.empty())
  42.         {
  43.             break;
  44.         }
  45.     }

  46.     ifs.close();
  47. }

  48. static bool get_board_serial_by_system(std::string & board_serial)
  49. {
  50.     board_serial.c_str();

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

  54.     if (0 == system(command))
  55.     {
  56.         parse_board_serial(dmidecode_result, "Serial Number:", board_serial);
  57.     }

  58.     unlink(dmidecode_result);

  59.     return(!board_serial.empty());
  60. }

  61. static bool get_board_serial_number(std::string & board_serial)
  62. {
  63.     if (0 == getuid())
  64.     {
  65.         if (get_board_serial_by_system(board_serial))
  66.         {
  67.             return(true);
  68.         }
  69.     }
  70.     return(false);
  71. }

  72. static void test()
  73. {
  74.     std::string board_serial;
  75.     if (get_board_serial_number(board_serial))
  76.     {
  77.         printf("board_serial: [%s]\n", board_serial.c_str());
  78.     }
  79.     else
  80.     {
  81.         printf("can not get board id\n");
  82.     }
  83. }
复制代码

腾讯云
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

精彩图文



在线客服(工作时间:9:00-22:00)
400-600-6565

内容导航

微信客服

Copyright   ©2015-2019  云服务器社区  Powered by©Discuz!  技术支持:尊托网络     ( 湘ICP备15009499号-1 )