自己造了个轮子,用python+wmic去获取windows的硬件信息,其实是很狭隘的版本。
之前写过一个版本,用requests+json去网上获取,但是那个有个坑,联想的序列号分不清带不带I,如x230和x230i序列号可能是一样的,还得选。
所以只好自己写了。
# coding=GBK import os,re # windows计算机硬件检查的函数 def wmic_get(command, return_type = 's', location = 1): # 这个函数的目的是解析windows的强大的wmic命令行,解析出返回值 # 参数带q,表示返回的是一个队列,参数带s,表示返回的是要取的值,默认是s return_value = [i for i in os.popen(command).readlines() if i != ' '] return_queue = [] for i in return_value: return_queue.append(i.strip()) return_one = return_queue[location] if return_type == 'q': return return_queue elif return_type == 's': return return_one def get_manufacturer(): # 检查厂商 return wmic_get('wmic bios get manufacturer') def get_cpu_info(): # 检查CPU型号 cpu_info = os.popen('wmic cpu list brief') cpu_info = cpu_info.readlines() f = [i for i in cpu_info if i != ' '][1].split(' ') for i in f: if re.search('.*@.*', i): return i def get_hdd_info(): # 检查硬盘名称 return wmic_get("wmic diskdrive get model") def get_hdd_size(): # 检查硬盘容量 hdd_size = int(wmic_get('wmic diskdrive get size')) / 1000000000 return str(int(hdd_size)) + ' GB' def get_free_space(): # 检查硬盘剩余容量 free_space = wmic_get('wmic volume get freespace', 'q') summary = [] for i in free_space: s = re.match('d+', i) if s != None: summary.append(int(s.group(0))) return str(int(sum(summary) / 1073741824)) + ' GB' def get_mem_size(): # 检查内存大小 result = wmic_get('wmic memorychip get capacity') result = int(result) / 1024 ** 3 return str(result) + ' GB' def get_mem_speed(): # 检查内存速率 return wmic_get('wmic memorychip get speed') def get_type_number(): # 检查TYPE号(如果有) return wmic_get('wmic baseboard get product') def get_sn(): # 检查序列号 return wmic_get('wmic bios get serialnumber') def get_os_name(): # 检查操作系统 os = wmic_get('wmic os get name') os_name = os.split('|') return os_name[0] def get_os_path(): # 检查操作系统SP补丁号: return wmic_get('wmic os get csdversion') def get_domain(): # 检查域 return wmic_get('wmic computersystem get domain') def get_install_date(): # 检查安装日期 i = wmic_get('wmic os get installdate')[:14] i = i[:4] + '\' + i[4:6] + '\' + i[6:8] + ' ' + i[8:10] + ':' + i[10:12] + ':' + i[12:14] return i def get_window_dir(): # 检查C盘所在位置 return wmic_get('wmic os get windowsdirectory') def get_pc_name(): # 检查计算机名 return wmic_get('wmic computersystem get caption') def get_pc_model(): # 检查计算机型号 return wmic_get('wmic computersystem get model') def get_user_name(): # 检查当前登录用户 return wmic_get('wmic computersystem get username') # test function print '厂商: ', get_manufacturer() print '型号: ', get_pc_model() print 'CPU: ', get_cpu_info() print '内存大小: ', get_mem_size() print '内存速度: ', get_mem_speed() print '硬盘: ', get_hdd_info() print '硬盘大小: ', get_hdd_size() print '剩余空间: ', get_free_space() print 'TYPE: ', get_type_number() print 'SN: ', get_sn() print '操作系统: ', get_os_name() print '补丁: ', get_os_path() print '域: ', get_domain() print '安装时间: ', get_install_date() print '计算机名: ', get_pc_name() print 'windows目录: ', get_window_dir() print '登录用户: ', get_user_name() a=input() # APPLE计算机硬件检查的函数 # 检查计算机是什么类型的