linux
使用dmidecode
CPU ID
// 代码:
sudo dmidecode -t 4 | grep ID
// 主板序列号
代码:
sudo dmidecode -t 2 | grep Serial
// MAC地址
代码:
sudo lshw -c network | grep serial | head -n 1
这个需要使用root权限,同时对于jetson 板子,dmidecode没用
14、获取CPU序列号或者主板序列号
sudodmidecode –t 4 | grep ID
15、查看序列号:
sudodmidecode | grep Serial | more
16、查看cpu
sudodmidecode –t 4 | more
17、查看BIOS信息
sudodmidecode –t 0 | more
18、查看主板信息
sudodmidecode –t 2 | more
19、查看OEM
Sudodmidecode –t 11 | more
20、显示当前内存大小
free –m| grep “Mem” | awk ‘{print $2}’
21:显示系统运行时间
uptime
22:查看内核限制
23:查看Ubuntu版本信息
cat /etc/issue
或 cat/etc/lsb-release
或 lsb_release–dsc
24:查看系统是32位还是64位
#查看long的位数,返回32或64
getconf LONG_BIT
#查看文件信息,包含32-bit就是32位,包含64-bit就是64位
file /sbin/init
linux使用dmidecode代码获取主板序列号
/* ************************************************************************
* 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);
}
获取CPUID:
CPUID并不是所有的Intel CPU都支持的。如果支持,汇编调用为:eax置0000_0003,调用cpuid。
以下为实现代码(在我的CPU上,并没有得到):
#define cpuid(in,a,b,c,d) asm("cpuid": "=a" (a), "=b" (b), "=c" (c), "=d" (d) : "a" (in));
static int getcpuid (char *id, size_t max)
{
int i;
unsigned long li, maxi, maxei, ebx, ecx, edx, unused;
cpuid (0, maxi, unused, unused, unused);
maxi &= 0xffff;
if (maxi < 3)
{
return -1;
}
cpuid (3, eax, ebx, ecx, edx);
snprintf (id, max, "%08lx %08lx %08lx %08lx", eax, ebx, ecx, edx);
fprintf (stdout, "get cpu id: %s/n", id);
return 0;
}
或者
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <arpa/inet.h>
#include <string>
#include <fstream>
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地址:(可以考虑加入ifconfig -a的解析,因为lshw实在太慢了)
#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 };
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 <cctype>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <unistd.h>
#include <scsi/sg.h>
#include <sys/ioctl.h>
#include <linux/hdreg.h>
#include <string>
#include <fstream>
static bool get_disk_name(std::string & disk_name)
{
disk_name.c_str();
std::ifstream ifs("/etc/mtab", std::ios::binary);
if (!ifs.is_open())
{
return(false);
}
char line[4096] = { 0 };
while (!ifs.eof())
{
ifs.getline(line, sizeof(line));
if (!ifs.good())
{
break;
}
const char * disk = line;
while (isspace(disk[0]))
{
++disk;
}
const char * space = strchr(disk, ' ');
if (NULL == space)
{
continue;
}
const char * mount = space + 1;
while (isspace(mount[0]))
{
++mount;
}
if ('/' != mount[0] || ' ' != mount[1])
{
continue;
}
while (space > disk && isdigit(space[-1]))
{
--space;
}
if (space > disk)
{
std::string(disk, space).swap(disk_name);
break;
}
}
ifs.close();
return(!disk_name.empty());
}
static void trim_serial(const void * serial, size_t serial_len, std::string & serial_no)
{
const char * serial_s = static_cast<const char *>(serial);
const char * serial_e = serial_s + serial_len;
while (serial_s < serial_e)
{
if (isspace(serial_s[0]))
{
++serial_s;
}
else if ('\0' == serial_e[-1] || isspace(serial_e[-1]))
{
--serial_e;
}
else
{
break;
}
}
if (serial_s < serial_e)
{
std::string(serial_s, serial_e).swap(serial_no);
}
}
static bool get_disk_serial_by_way_1(const std::string & disk_name, std::string & serial_no)
{
serial_no.clear();
int fd = open(disk_name.c_str(), O_RDONLY);
if (-1 == fd)
{
return(false);
}
struct hd_driveid drive = { 0 };
if (0 == ioctl(fd, HDIO_GET_IDENTITY, &drive))
{
trim_serial(drive.serial_no, sizeof(drive.serial_no), serial_no);
}
close(fd);
return(!serial_no.empty());
}
static bool scsi_io(
int fd, unsigned char * cdb,
unsigned char cdb_size, int xfer_dir,
unsigned char * data, unsigned int data_size,
unsigned char * sense, unsigned int sense_len
)
{
sg_io_hdr_t io_hdr = { 0 };
io_hdr.interface_id = 'S';
io_hdr.cmdp = cdb;
io_hdr.cmd_len = cdb_size;
io_hdr.sbp = sense;
io_hdr.mx_sb_len = sense_len;
io_hdr.dxfer_direction = xfer_dir;
io_hdr.dxferp = data;
io_hdr.dxfer_len = data_size;
io_hdr.timeout = 5000;
if (ioctl(fd, SG_IO, &io_hdr) < 0)
{
return(false);
}
if (SG_INFO_OK != (io_hdr.info & SG_INFO_OK_MASK) && io_hdr.sb_len_wr > 0)
{
return(false);
}
if (io_hdr.masked_status || io_hdr.host_status || io_hdr.driver_status)
{
return(false);
}
return(true);
}
static bool get_disk_serial_by_way_2(const std::string & disk_name, std::string & serial_no)
{
serial_no.clear();
int fd = open(disk_name.c_str(), O_RDONLY);
if (-1 == fd)
{
return(false);
}
int version = 0;
if (ioctl(fd, SG_GET_VERSION_NUM, &version) < 0 || version < 30000)
{
close(fd);
return(false);
}
const unsigned int data_size = 0x00ff;
unsigned char data[data_size] = { 0 };
const unsigned int sense_len = 32;
unsigned char sense[sense_len] = { 0 };
unsigned char cdb[] = { 0x12, 0x01, 0x80, 0x00, 0x00, 0x00 };
cdb[3] = (data_size >> 8) & 0xff;
cdb[4] = (data_size & 0xff);
if (scsi_io(fd, cdb, sizeof(cdb), SG_DXFER_FROM_DEV, data, data_size, sense, sense_len))
{
int page_len = data[3];
trim_serial(data + 4, page_len, serial_no);
}
close(fd);
return(!serial_no.empty());
}
static bool parse_serial(const char * line, int line_size, const char * match_words, std::string & serial_no)
{
const char * serial_s = strstr(line, match_words);
if (NULL == serial_s)
{
return(false);
}
serial_s += strlen(match_words);
while (isspace(serial_s[0]))
{
++serial_s;
}
const char * serial_e = line + line_size;
const char * comma = strchr(serial_s, ',');
if (NULL != comma)
{
serial_e = comma;
}
while (serial_e > serial_s && isspace(serial_e[-1]))
{
--serial_e;
}
if (serial_e <= serial_s)
{
return(false);
}
std::string(serial_s, serial_e).swap(serial_no);
return(true);
}
static void get_serial(const char * file_name, const char * match_words, std::string & serial_no)
{
serial_no.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;
}
if (0 == ifs.gcount())
{
continue;
}
if (parse_serial(line, ifs.gcount() - 1, match_words, serial_no))
{
break;
}
}
ifs.close();
}
static bool get_disk_serial_by_way_3(const std::string & disk_name, std::string & serial_no)
{
serial_no.c_str();
const char * hdparm_result = ".hdparm_result.txt";
char command[512] = { 0 };
snprintf(command, sizeof(command), "hdparm -i %s | grep SerialNo > %s", disk_name.c_str(), hdparm_result);
if (0 == system(command))
{
get_serial(hdparm_result, "SerialNo=", serial_no);
}
unlink(hdparm_result);
return(!serial_no.empty());
}
static bool get_disk_serial_by_way_4(std::string & serial_no)
{
serial_no.c_str();
const char * lshw_result = ".lshw_result.txt";
char command[512] = { 0 };
snprintf(command, sizeof(command), "lshw -class disk | grep serial > %s", lshw_result);
if (0 == system(command))
{
get_serial(lshw_result, "serial:", serial_no);
}
unlink(lshw_result);
return(!serial_no.empty());
}
static bool get_disk_serial_number(std::string & serial_no)
{
if (0 != getuid())
{
return(false);
}
std::string disk_name;
if (get_disk_name(disk_name))
{
if (get_disk_serial_by_way_1(disk_name, serial_no))
{
return(true);
}
if (get_disk_serial_by_way_2(disk_name, serial_no))
{
return(true);
}
if (get_disk_serial_by_way_3(disk_name, serial_no))
{
return(true);
}
}
if (get_disk_serial_by_way_4(serial_no))
{
return(true);
}
return(false);
}
static void test_1()
{
std::string serial_no;
if (get_disk_serial_number(serial_no))
{
printf("serial_number: [%s]\n", serial_no.c_str());
}
else
{
printf("get serial number failed\n");
}
}
static void test_2()
{
std::string disk_name;
if (get_disk_name(disk_name))
{
printf("disk_name:[%s]\n", disk_name.c_str());
{
std::string serial_no;
get_disk_serial_by_way_1(disk_name, serial_no);
printf("get_serial_by_way_1:[%s]\n", serial_no.c_str());
}
{
std::string serial_no;
get_disk_serial_by_way_2(disk_name, serial_no);
printf("get_serial_by_way_2:[%s]\n", serial_no.c_str());
}
{
std::string serial_no;
get_disk_serial_by_way_3(disk_name, serial_no);
printf("get_serial_by_way_3:[%s]\n", serial_no.c_str());
}
}
{
std::string serial_no;
get_disk_serial_by_way_4(serial_no);
printf("get_serial_by_way_4:[%s]\n", serial_no.c_str());
}
}
int main(int argc, char * argv[])
{
printf("---------------\n");
test_1();
printf("---------------\n");
test_2();
printf("---------------\n");
return(0);
}
简化:
#include <stdio.h>
#include <sys/ioctl.h>
#include <linux/hdreg.h>
#include <sys/stat.h>
#include <fcntl.h>
static int getdiskid (char *hardc)
{
int fd;
struct hd_driveid hid;
fd = open ("/dev/sda", O_RDONLY);
if (fd < 0)
{
return -1;
}
if (ioctl (fd, HDIO_GET_IDENTITY, &hid) < 0)
{
return -1;
}
close (fd);
sprintf(hardc,"%s", hid.serial_no);
return 0;
}
int main(void)
{
char hardseri[50];
getdiskid(hardseri);
printf("%s",hardseri);
return 0;
}
获取主板序列号:(没有找到纯代码的实现方法)
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <fstream>
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 -t 2 | grep Serial > %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);
}
方便测试的Makefile:
build :
g++ -o get_cpu_id get_cpu_id.cpp
g++ -o get_mac_address get_mac_address.cpp
g++ -o get_disk_serial_number get_disk_serial_number.cpp
g++ -o get_board_serial_number get_board_serial_number.cpp
run :
@echo "--------------------"
@- ./get_cpu_id
@echo "--------------------"
@- ./get_mac_address
@echo "--------------------"
@- ./get_disk_serial_number
@echo "--------------------"
@- ./get_board_serial_number
@echo "--------------------"
clean :
-rm get_cpu_id
-rm get_mac_address
-rm get_disk_serial_number
-rm get_board_serial_number
rebuild : clean build
编译:make 或者 make build
运行:make run 或者 sudo make run (上面大多数信息都需要超级用户权限才能获取到结果)
清理:make clean (这个写得太死了,本来是想删除非cpp文件的,shell写不出来)
重编:make rebuild
windows:
命令行:
查看主机序列号:
C:\Users\Administrator>wmic bios get serialnumber
SerialNumber
WB12345678-------->>>>这是我笔记本的主机序列号,在不换主板的情况下,该序列号应该与机器机身上贴的序列号一致
C:\Users\Administrator>
查看磁盘序列号:
一、查看ECS实例中块存储的设备名
lsblk
二、使用以下命令获取块存储设备的序列号
udevadm info --query=all --name=磁盘设备名 | grep ID_SERIAL
例如
udevadm info --query=all --name=/dev/vda | grep ID_SERIAL
##获取CPU序列号
C:\Users\jesson>wmic CPU get ProcessorID
ProcessorId
BFEBFBFF000206A7
获取mac地址和ip信息
一台机器上可能不只有一个网卡,但每一个网卡只有一个MAC地址,而每一个网卡可能配置有多个IP地址;如平常的笔记本电脑中,就会有无线网卡和有线网卡(网线接口)两种;因此,如果要获得本机所有网卡的IP和MAC地址信息,则必须顺序获得每个网卡,再依次获取其信息等;在windows sdk中,用IP_ADAPTER_INFO结构体存储网卡信息,包括网卡名、网卡描述、网卡MAC地址、网卡IP等,该结构体的主要描述如下所示:
typedef struct _IP_ADAPTER_INFO {
struct _IP_ADAPTER_INFO* Next;//指向链表中下一个适配器信息的指针
DWORD ComboIndex;//预留值
char AdapterName[MAX_ADAPTER_NAME_LENGTH + 4];//使用ANSI字符串表示的适配器名称
char Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4];//使用ANSI字符串表示的适配器描述
UINT AddressLength;//适配器硬件地址以字节计算的长度
BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH];//硬件地址以BYTE数组所表示
DWORD Index;//适配器索引
UINT Type;//适配器类型,主要有以下几种:
/*
* MIB_IF_TYPE_OTHER 1
* MIB_IF_TYPE_ETHERNET 6
* MIB_IF_TYPE_TOKENRING 9
* MIB_IF_TYPE_FDDI 15
* MIB_IF_TYPE_PPP 23
* MIB_IF_TYPE_LOOPBACK 24
* MIB_IF_TYPE_SLIP 28
*/
UINT DhcpEnabled;//指定这个适配器是否开启DHCP
PIP_ADDR_STRING CurrentIpAddress;//预留值
IP_ADDR_STRING IpAddressList;//该适配器的IPv4地址链表
IP_ADDR_STRING GatewayList;//该适配器的网关IPv4地址链表
IP_ADDR_STRING DhcpServer;//该适配器的DHCP服务器的IPv4 地址链表
BOOL HaveWins;
IP_ADDR_STRING PrimaryWinsServer;
IP_ADDR_STRING SecondaryWinsServer;
time_t LeaseObtained;
time_t LeaseExpires;
} IP_ADAPTER_INFO,*PIP_ADAPTER_INFO;
由于可能有多个网卡,因此struct _IP_ADAPTER_INFO* Next字段为一个链表结构指针,由于一个网卡可能有多个IP,因此IP_ADDR_STRING字段应该也是一个链表结构,其信息如下所示:
typedef struct _IP_ADDR_STRING
{
struct _IP_ADDR_STRING* Next; //指向同类型节点,即下一个IP(如果有多IP的话)
IP_ADDRESS_STRING IpAddress; //IP地址信息
IP_MASK_STRING IpMask; //IP子网掩码
DWORD Context;// 网络表入口。这个值对应着AddIPAddredd和DeleteIPAddress函数中的NTEContext参数
} IP_ADDR_STRING;
由于可能有多个网卡,因此struct _IP_ADAPTER_INFO* Next字段为一个链表结构指针,由于一个网卡可能有多个IP,因此IP_ADDR_STRING字段应该也是一个链表结构,其信息如下所示:
typedef struct _IP_ADDR_STRING
{
struct _IP_ADDR_STRING* Next; //指向同类型节点,即下一个IP(如果有多IP的话)
IP_ADDRESS_STRING IpAddress; //IP地址信息
IP_MASK_STRING IpMask; //IP子网掩码
DWORD Context;// 网络表入口。这个值对应着AddIPAddredd和DeleteIPAddress函数中的NTEContext参数
} IP_ADDR_STRING;
在基本了解以上信息后,就可以调用GetAdaptersInfo函数来获取相关网卡信息了,其通用的代码如下所示:
#include <WinSock2.h>
#include <Iphlpapi.h>
#include <iostream>
using namespace std;
#pragma comment(lib,"Iphlpapi.lib") //需要添加Iphlpapi.lib库
int main(int argc, char* argv[])
{
//PIP_ADAPTER_INFO结构体指针存储本机网卡信息
PIP_ADAPTER_INFO pIpAdapterInfo = new IP_ADAPTER_INFO();
//得到结构体大小,用于GetAdaptersInfo参数
unsigned long stSize = sizeof(IP_ADAPTER_INFO);
//调用GetAdaptersInfo函数,填充pIpAdapterInfo指针变量;其中stSize参数既是一个输入量也是一个输出量
int nRel = GetAdaptersInfo(pIpAdapterInfo,&stSize);
//记录网卡数量
int netCardNum = 0;
//记录每张网卡上的IP地址数量
int IPnumPerNetCard = 0;
if (ERROR_BUFFER_OVERFLOW == nRel)
{
//如果函数返回的是ERROR_BUFFER_OVERFLOW
//则说明GetAdaptersInfo参数传递的内存空间不够,同时其传出stSize,表示需要的空间大小
//这也是说明为什么stSize既是一个输入量也是一个输出量
//释放原来的内存空间
delete pIpAdapterInfo;
//重新申请内存空间用来存储所有网卡信息
pIpAdapterInfo = (PIP_ADAPTER_INFO)new BYTE[stSize];
//再次调用GetAdaptersInfo函数,填充pIpAdapterInfo指针变量
nRel=GetAdaptersInfo(pIpAdapterInfo,&stSize);
}
if (ERROR_SUCCESS == nRel)
{
//输出网卡信息
//可能有多网卡,因此通过循环去判断
while (pIpAdapterInfo)
{
cout<<"网卡数量:"<<++netCardNum<<endl;
cout<<"网卡名称:"<<pIpAdapterInfo->AdapterName<<endl;
cout<<"网卡描述:"<<pIpAdapterInfo->Description<<endl;
switch(pIpAdapterInfo->Type)
{
case MIB_IF_TYPE_OTHER:
cout<<"网卡类型:"<<"OTHER"<<endl;
break;
case MIB_IF_TYPE_ETHERNET:
cout<<"网卡类型:"<<"ETHERNET"<<endl;
break;
case MIB_IF_TYPE_TOKENRING:
cout<<"网卡类型:"<<"TOKENRING"<<endl;
break;
case MIB_IF_TYPE_FDDI:
cout<<"网卡类型:"<<"FDDI"<<endl;
break;
case MIB_IF_TYPE_PPP:
printf("PP\n");
cout<<"网卡类型:"<<"PPP"<<endl;
break;
case MIB_IF_TYPE_LOOPBACK:
cout<<"网卡类型:"<<"LOOPBACK"<<endl;
break;
case MIB_IF_TYPE_SLIP:
cout<<"网卡类型:"<<"SLIP"<<endl;
break;
default:
break;
}
cout<<"网卡MAC地址:";
for (DWORD i = 0; i < pIpAdapterInfo->AddressLength; i++)
if (i < pIpAdapterInfo->AddressLength-1)
{
printf("%02X-", pIpAdapterInfo->Address[i]);
}
else
{
printf("%02X\n", pIpAdapterInfo->Address[i]);
}
cout<<"网卡IP地址如下:"<<endl;
//可能网卡有多IP,因此通过循环去判断
IP_ADDR_STRING *pIpAddrString =&(pIpAdapterInfo->IpAddressList);
do
{
cout<<"该网卡上的IP数量:"<<++IPnumPerNetCard<<endl;
cout<<"IP 地址:"<<pIpAddrString->IpAddress.String<<endl;
cout<<"子网地址:"<<pIpAddrString->IpMask.String<<endl;
cout<<"网关地址:"<<pIpAdapterInfo->GatewayList.IpAddress.String<<endl;
pIpAddrString=pIpAddrString->Next;
} while (pIpAddrString);
pIpAdapterInfo = pIpAdapterInfo->Next;
cout<<"--------------------------------------------------------------------"<<endl;
}
}
//释放内存空间
if (pIpAdapterInfo)
{
delete pIpAdapterInfo;
}
return 0;
}
执行结果:
通过WMI获取网卡MAC地址、硬盘序列号、主板序列号、CPU ID、BIOS序列号
头文件:WMI_DeviceQuery.h
/* ----------------------------------------------------------
文件名称:WMI_DeviceQuery.h
作者:秦建辉
MSN:splashcn@msn.com
版本历史:
V1.4 2010年05月17日
修正了硬盘序列号处理中的错误。现在和EVEREST Ultimate Edition 5.5一致。
V1.3 2010年05月11日
增加了对网卡原生MAC地址的查询。
V1.2 2010年05月05日
增加对硬盘序列号的进一步处理。
V1.1 2010年04月30日
修正微软MSDN例子错误,并增加对虚拟机网卡的判断。
V1.0 2010年04月27日
完成正式版本。
功能描述:
基于WMI获取设备属性:
0:网卡原生MAC地址
1:硬盘序列号
2:主板序列号
3:CPU ID
4:BIOS序列号
5:主板型号
6:网卡当前MAC地址
接口函数:
WMI_DeviceQuery
------------------------------------------------------------ */
#pragma once
#include <windows.h>
#ifndef MACRO_T_DEVICE_PROPERTY
#define MACRO_T_DEVICE_PROPERTY
#define PROPERTY_MAX_LEN 128 // 属性字段最大长度
typedef struct _T_DEVICE_PROPERTY
{
TCHAR szProperty[PROPERTY_MAX_LEN];
} T_DEVICE_PROPERTY;
#endif
#define WMI_QUERY_TYPENUM 7 // WMI查询支持的类型数
#ifdef __cplusplus
extern "C"
{
#endif
/*
功能:通过WMI获取设备属性
参数说明:
iQueryType:需要查询的设备属性
0:网卡原生MAC地址
1:硬盘序列号
2:主板序列号
3:CPU ID
4:BIOS序列号
5:主板型号
6:网卡当前MAC地址
properties:存储设备属性值
iSize:可存储的最大设备个数
返回值:
-1:不支持的设备属性值
-2:WMI连接失败
-3:不正确的WQL查询语句
>=0:获取的设备个数
*/
INT WMI_DeviceQuery( INT iQueryType, T_DEVICE_PROPERTY *properties, INT iSize );
#ifdef __cplusplus
}
#endif
实现文件:WMI_DeviceQuery.cpp
#include "WMI_DeviceQuery.h"
#include <comutil.h>
#include <Wbemidl.h>
#include <tchar.h>
#include <strsafe.h>
#include <algorithm>
#include <atlconv.h>
#include <ntddndis.h>
#pragma comment (lib, "comsuppw.lib")
#pragma comment (lib, "wbemuuid.lib")
typedef struct _T_WQL_QUERY
{
CHAR* szSelect; // SELECT语句
WCHAR* szProperty; // 属性字段
} T_WQL_QUERY;
// WQL查询语句
const T_WQL_QUERY szWQLQuery[] = {
// 网卡原生MAC地址
"SELECT * FROM Win32_NetworkAdapter WHERE (MACAddress IS NOT NULL) AND (NOT (PNPDeviceID LIKE 'ROOT%'))",
L"PNPDeviceID",
// 硬盘序列号
"SELECT * FROM Win32_DiskDrive WHERE (SerialNumber IS NOT NULL) AND (MediaType LIKE 'Fixed hard disk%')",
L"SerialNumber",
// 主板序列号
"SELECT * FROM Win32_BaseBoard WHERE (SerialNumber IS NOT NULL)",
L"SerialNumber",
// 处理器ID
"SELECT * FROM Win32_Processor WHERE (ProcessorId IS NOT NULL)",
L"ProcessorId",
// BIOS序列号
"SELECT * FROM Win32_BIOS WHERE (SerialNumber IS NOT NULL)",
L"SerialNumber",
// 主板型号
"SELECT * FROM Win32_BaseBoard WHERE (Product IS NOT NULL)",
L"Product",
// 网卡当前MAC地址
"SELECT * FROM Win32_NetworkAdapter WHERE (MACAddress IS NOT NULL) AND (NOT (PNPDeviceID LIKE 'ROOT%'))",
L"MACAddress",
};
// 通过“PNPDeviceID”获取网卡原生MAC地址
static BOOL WMI_DoWithPNPDeviceID( const TCHAR *PNPDeviceID, TCHAR *MacAddress, UINT uSize )
{
TCHAR DevicePath[MAX_PATH];
HANDLE hDeviceFile;
BOOL isOK = FALSE;
// 生成设备路径名
StringCchCopy( DevicePath, MAX_PATH, TEXT(".//") );
StringCchCat( DevicePath, MAX_PATH, PNPDeviceID );
StringCchCat( DevicePath, MAX_PATH, TEXT("#{ad498944-762f-11d0-8dcb-00c04fc3358c}") );
// 将“PNPDeviceID”中的“/”替换成“#”,以获得真正的设备路径名
std::replace( DevicePath + 4, DevicePath + 4 + _tcslen(PNPDeviceID), TEXT('//'), TEXT('#') );
// 获取设备句柄
hDeviceFile = CreateFile( DevicePath,
0,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if( hDeviceFile != INVALID_HANDLE_VALUE )
{
ULONG dwID;
BYTE ucData[8];
DWORD dwByteRet;
// 获取网卡原生MAC地址
dwID = OID_802_3_PERMANENT_ADDRESS;
isOK = DeviceIoControl( hDeviceFile, IOCTL_NDIS_QUERY_GLOBAL_STATS, &dwID, sizeof(dwID), ucData, sizeof(ucData), &dwByteRet, NULL );
if( isOK )
{ // 将字节数组转换成16进制字符串
for( DWORD i = 0; i < dwByteRet; i++ )
{
StringCchPrintf( MacAddress + (i << 1), uSize - (i << 1), TEXT("%02X"), ucData[i] );
}
MacAddress[dwByteRet << 1] = TEXT('/0'); // 写入字符串结束标记
}
CloseHandle( hDeviceFile );
}
return isOK;
}
static BOOL WMI_DoWithHarddiskSerialNumber( TCHAR *SerialNumber, UINT uSize )
{
UINT iLen;
UINT i;
iLen = _tcslen( SerialNumber );
if( iLen == 40 ) // InterfaceType = "IDE"
{ // 需要将16进制编码串转换为字符串
TCHAR ch, szBuf[32];
BYTE b;
for( i = 0; i < 20; i++ )
{ // 将16进制字符转换为高4位
ch = SerialNumber[i * 2];
if( (ch >= '0') && (ch <= '9') )
{
b = ch - '0';
}
else if( (ch >= 'A') && (ch <= 'F') )
{
b = ch - 'A' + 10;
}
else if( (ch >= 'a') && (ch <= 'f') )
{
b = ch - 'a' + 10;
}
else
{ // 非法字符
break;
}
b <<= 4;
// 将16进制字符转换为低4位
ch = SerialNumber[i * 2 + 1];
if( (ch >= '0') && (ch <= '9') )
{
b += ch - '0';
}
else if( (ch >= 'A') && (ch <= 'F') )
{
b += ch - 'A' + 10;
}
else if( (ch >= 'a') && (ch <= 'f') )
{
b += ch - 'a' + 10;
}
else
{ // 非法字符
break;
}
szBuf[i] = b;
}
if( i == 20 )
{ // 转换成功
szBuf[i] = L'/0';
StringCchCopy( SerialNumber, uSize, szBuf );
iLen = _tcslen( SerialNumber );
}
}
// 每2个字符互换位置
for( i = 0; i < iLen; i += 2 )
{
std::swap( SerialNumber[i], SerialNumber[i+1] );
}
// 去掉空格
std::remove( SerialNumber, SerialNumber + _tcslen(SerialNumber) + 1, L' ' );
return TRUE;
}
static BOOL WMI_DoWithProperty( INT iQueryType, TCHAR *szProperty, UINT uSize )
{
BOOL isOK = TRUE;
switch( iQueryType )
{
case 0: // 网卡原生MAC地址
isOK = WMI_DoWithPNPDeviceID( szProperty, szProperty, uSize );
break;
case 1: // 硬盘序列号
isOK = WMI_DoWithHarddiskSerialNumber( szProperty, uSize );
break;
case 6: // 网卡当前MAC地址
// 去掉冒号
std::remove( szProperty, szProperty + _tcslen(szProperty) + 1, L':' );
break;
default:
// 去掉空格
std::remove( szProperty, szProperty + _tcslen(szProperty) + 1, L' ' );
}
return isOK;
}
// 基于Windows Management Instrumentation(Windows管理规范)
INT WMI_DeviceQuery( INT iQueryType, T_DEVICE_PROPERTY *properties, INT iSize )
{
HRESULT hres;
INT iTotal = 0;
// 判断查询类型是否支持
if( (iQueryType < 0) || (iQueryType >= sizeof(szWQLQuery)/sizeof(T_WQL_QUERY)) )
{
return -1; // 查询类型不支持
}
// 初始化COM
hres = CoInitializeEx( NULL, COINIT_MULTITHREADED );
if( FAILED(hres) )
{
return -2;
}
// 设置COM的安全认证级别
hres = CoInitializeSecurity(
NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
NULL
);
if( FAILED(hres) )
{
CoUninitialize();
return -2;
}
// 获得WMI连接COM接口
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
NULL,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator,
reinterpret_cast<LPVOID*>(&pLoc)
);
if( FAILED(hres) )
{
CoUninitialize();
return -2;
}
// 通过连接接口连接WMI的内核对象名"ROOT//CIMV2"
IWbemServices *pSvc = NULL;
hres = pLoc->ConnectServer(
_bstr_t( L"ROOT//CIMV2" ),
NULL,
NULL,
NULL,
0,
NULL,
NULL,
&pSvc
);
if( FAILED(hres) )
{
pLoc->Release();
CoUninitialize();
return -2;
}
// 设置请求代理的安全级别
hres = CoSetProxyBlanket(
pSvc,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
NULL,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE
);
if( FAILED(hres) )
{
pSvc->Release();
pLoc->Release();
CoUninitialize();
return -2;
}
// 通过请求代理来向WMI发送请求
IEnumWbemClassObject *pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t( szWQLQuery[iQueryType].szSelect ),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator
);
if( FAILED(hres) )
{
pSvc->Release();
pLoc->Release();
CoUninitialize();
return -3;
}
// 循环枚举所有的结果对象
while( pEnumerator )
{
IWbemClassObject *pclsObj = NULL;
ULONG uReturn = 0;
if( (properties != NULL) && (iTotal >= iSize) )
{
break;
}
pEnumerator->Next(
WBEM_INFINITE,
1,
&pclsObj,
&uReturn
);
if( uReturn == 0 )
{
break;
}
if( properties != NULL )
{ // 获取属性值
VARIANT vtProperty;
VariantInit( &vtProperty );
pclsObj->Get( szWQLQuery[iQueryType].szProperty, 0, &vtProperty, NULL, NULL );
StringCchCopy( properties[iTotal].szProperty, PROPERTY_MAX_LEN, W2T(vtProperty.bstrVal) );
VariantClear( &vtProperty );
// 对属性值做进一步的处理
if( WMI_DoWithProperty( iQueryType, properties[iTotal].szProperty, PROPERTY_MAX_LEN ) )
{
iTotal++;
}
}
else
{
iTotal++;
}
pclsObj->Release();
} // End While
// 释放资源
pEnumerator->Release();
pSvc->Release();
pLoc->Release();
CoUninitialize();
return iTotal;
}