Linux-硬件信息收集
使用lshw 来做收集。
import copy import json import os 硬件详细报表_dict={} def 命令执行(ip:str,cmd:str,port:int=22)->str: ''' 对指定的IP执行命令,使用ssh方式,bash解释器 应该确保对方主机信任本机。 返回命令执行的结果(读取的是stdout) ''' cmd_str = "ssh -p {} {} '{}'".format(port,ip,cmd) result=os.popen(cmd_str) result_str = result.read() return result_str; def get_dict_value(object,key,notValue=''): ''' 获取值,假若不存在key,则返回notValue ''' if(key in object.keys()): return object[key] else: return notValue; def 命令结果转换为json对象(txt:str)->dict: ret = json.loads(txt) return ret; def 更改dict的key(dict_object:dict,old_key:any,new_key:any)->dict: if(old_key in dict_object.keys()): dict_object[new_key] = copy.deepcopy(dict_object[old_key]) del dict_object[old_key] return dict_object; def format_size(size:int,units:str): ''' units:单位,按照不通的单位来友好显示 ''' if(units == 'bytes'): if size < 1024: return f"{size}B" elif size < 1024 * 1024: return f"{size / 1024:.2f}KB" elif size < 1024 * 1024 * 1024: return f"{size / (1024 * 1024):.2f}MB" elif size < 1024 * 1024 * 1024 * 1024: return f"{size / (1024 * 1024 * 1024):.2f}GB" else: return f"{size / (1024 * 1024 * 1024 * 1024):.2f}TB" elif(units == 'bit/s'): if size < 1024: return f"{size}B/s" elif size < 1024 * 1024: return f"{size / 1024:.2f}KB/s" elif size < 1024 * 1024 * 1024: return f"{size / (1024 * 1024):.2f}MB/s" elif size < 1024 * 1024 * 1024 * 1024: return f"{size / (1024 * 1024 * 1024):.2f}GB/s" else: return f"{size / (1024 * 1024 * 1024 * 1024 ):.2f}PB/s" elif(units == 'Hz'): if size < 1000: return f"{size}Hz" elif size < 1000 * 1000: return f"{int(size/1000)}KHz" elif size < 1000 * 1000* 1000: return f"{int(size/1000/1000)}MHz" elif size < 1000 * 1000* 1000*1000: return f"{int(size/1000/1000/1000)}GHz" else: return f"{int(size/1000/1000/1000/1000)}THz" elif(units == 'mWh'): if size < 1000: return f"{size}mWh" else: return f"{int(size/1000)}Wh" else: #没有单位的,按照1000来换算。 if size < 1000: return f"{size}" elif size < 1000 * 1000: return f"{int(size/1000)}" elif size < 1000 * 1000* 1000: return f"{int(size/1000/1000)}" elif size < 1000 * 1000* 1000*1000: return f"{int(size/1000/1000/1000)}" else: return f"{int(size/1000/1000/1000/1000)}"; def 基本信息处理(json_object): ''' 处理一些基本的信息字段,避免重复编码 ''' temp=copy.deepcopy(json_object) temp = 更改dict的key(temp,'claimed','声明状态') temp = 更改dict的key(temp,'handle','句柄') temp = 更改dict的key(temp,'description','描述') temp = 更改dict的key(temp,'product','产品型号') temp = 更改dict的key(temp,'vendor','制造商') temp = 更改dict的key(temp,'physid','物理标识符') temp = 更改dict的key(temp,'businfo','总线信息') temp = 更改dict的key(temp,'version','版本') temp = 更改dict的key(temp,'width','位宽') temp = 更改dict的key(temp,'clock','时钟频率') if('时钟频率' in temp.keys()): temp['时钟频率'] = format_size(temp['时钟频率'],'Hz') temp = 更改dict的key(temp,'logicalname','逻辑名称') temp = 更改dict的key(temp,'serial','序列号') temp = 更改dict的key(temp,'units','单位') if('size' in temp.keys()): temp['size'] = format_size(temp['size'],get_dict_value(temp,'单位')) temp = 更改dict的key(temp,'size','大小/速度/尺寸') temp = 更改dict的key(temp,'capacity','容量') if('容量' in temp.keys()): temp['容量'] = format_size(temp['容量'],get_dict_value(temp,'单位')) temp = 更改dict的key(temp,'dev','设备号') temp = 更改dict的key(temp,'vendor_id','制造商ID') temp = 更改dict的key(temp,'slot','插槽') temp = 更改dict的key(temp,'disabled','禁用') return temp; def processor_识别(processor_object:dict): global 硬件详细报表_dict temp=基本信息处理(processor_object) if('configuration' in temp.keys()): configuration = temp['configuration'] configuration = 更改dict的key(configuration,'microcode','微码,标识获取部分信息失败') configuration = 更改dict的key(configuration,'cores','核心数') configuration = 更改dict的key(configuration,'enabledcores','启用的核心数') configuration = 更改dict的key(configuration,'threads','线程数') temp = 更改dict的key(temp,'configuration','配置信息') if('capabilities' in temp.keys()): #CPU的能力我将key视为指令集处理,不单独列出 capabilities = temp['capabilities'] temp['capabilities'] = ",".join(capabilities.keys()) temp = 更改dict的key(temp,'capabilities','设备能力(指令集)') key = 预定义的class[temp['class']] if(key not in 硬件详细报表_dict.keys()): 硬件详细报表_dict[key]=[temp] else: 句柄列表 = [] for info in 硬件详细报表_dict[key]: 句柄=get_dict_value(info,'句柄') if(0!=len(句柄)): 句柄列表.append(句柄) if(get_dict_value(temp,'句柄') not in 句柄列表): 硬件详细报表_dict[key].append(temp); def memory_识别(memory_object:dict): global 硬件详细报表_dict capabilities_dict={"isa": "ISA bus(支持ISA总线。)","pci": "PCI bus(支持PCI总线。)","pnp": "Plug-and-Play(支持即插即用设备。)","upgrade": "BIOS EEPROM can be upgraded(BIOS EEPROM可以升级。)","shadowing": "BIOS shadowing(支持BIOS阴影。)","escd": "ESCD(支持扩展系统配置数据。)","cdboot": "Booting from CD-ROM/DVD(支持从CD-ROM/DVD启动。)","bootselect": "Selectable boot path(可选择的启动路径。)","socketedrom": "BIOS ROM is socketed(BIOS ROM是插座式的。)","edd": "Enhanced Disk Drive extensions(增强磁盘驱动器扩展。)","int13floppy360": "5.25’ 360KB floppy(支持5.25英寸360KB软盘。)","int13floppy1200": "5.25’ 1.2MB floppy(支持5.25英寸1.2MB软盘。)","int13floppy720": "3.5’ 720KB floppy(支持3.5英寸720KB软盘。)","int13floppy2880": "3.5’ 2.88MB floppy(支持3.5英寸2.88MB软盘。)","int5printscreen": "Print Screen key(支持打印屏幕键。)","int9keyboard": "i8042 keyboard controller(支持i8042键盘控制器。)","int14serial": "INT14 serial line control(支持INT14串行线控制。)","int17printer": "INT17 printer control(支持INT17打印机控制。)","int10video": "INT10 CGA/Mono video(支持INT10 CGA/单色视频。)","agp": "AGP(支持AGP。)","ls120boot": "Booting from LS-120(支持从LS-120启动。)","zipboot": "Booting from ATAPI ZIP(支持从ATAPI ZIP启动。)","biosbootspecification": "BIOS boot specification(支持BIOS引导规范。)",} temp=基本信息处理(memory_object) if('configuration' in temp.keys()): configuration = temp['configuration'] configuration = 更改dict的key(configuration,'driver','驱动程序') configuration = 更改dict的key(configuration,'latency','延迟') temp = 更改dict的key(temp,'configuration','配置信息') if('capabilities' in temp.keys()): capabilities = temp['capabilities'] for key in capabilities: capabilities[key] = get_dict_value(capabilities_dict,key,capabilities[key]) temp = 更改dict的key(temp,'capabilities','设备能力') key = 预定义的class[temp['class']] if(key not in 硬件详细报表_dict.keys()): 硬件详细报表_dict[key]=[temp] else: 句柄列表 = [] for info in 硬件详细报表_dict[key]: 句柄=get_dict_value(info,'句柄') if(0!=len(句柄)): 句柄列表.append(句柄) if(get_dict_value(temp,'句柄') not in 句柄列表): 硬件详细报表_dict[key].append(temp); def storage_识别(storage_object:dict): global 硬件详细报表_dict capabilities_dict={'ide':"支持IDE(集成驱动器电子学)接口","isa_compat_mode":"ISA compatibility mode(ISA兼容模式)","bus_master":"bus mastering”(总线控制功能)","emulated":"Emulated device(虚拟设备)","pnp": "Plug and Play(即插即用)"} temp=基本信息处理(storage_object) if('configuration' in temp.keys()): configuration = temp['configuration'] configuration = 更改dict的key(configuration,'driver','驱动程序') configuration = 更改dict的key(configuration,'latency','延迟') temp = 更改dict的key(temp,'configuration','配置信息') if('capabilities' in temp.keys()): capabilities = temp['capabilities'] for key in capabilities: capabilities[key] = get_dict_value(capabilities_dict,key,capabilities[key]) temp = 更改dict的key(temp,'capabilities','设备能力') key = 预定义的class[temp['class']] if(key not in 硬件详细报表_dict.keys()): 硬件详细报表_dict[key]=[temp] else: 句柄列表 = [] for info in 硬件详细报表_dict[key]: 句柄=get_dict_value(info,'句柄') if(0!=len(句柄)): 句柄列表.append(句柄) if(get_dict_value(temp,'句柄') not in 句柄列表): 硬件详细报表_dict[key].append(temp); def volume_识别(volume_object:dict): global 硬件详细报表_dict capabilities_dict={'primary':"Primary partition(主分区)接口","bootable":"Bootable partition (active)(可引导分区)","multi":"Multi-volumes(支持多卷)","lvm2":"支持LVM2(Logical Volume Manager 2)",} temp=基本信息处理(volume_object) if('configuration' in temp.keys()): configuration = temp['configuration'] configuration = 更改dict的key(configuration,'mount.fstype','文件系统类型') configuration = 更改dict的key(configuration,'mount.options','挂载选项') configuration = 更改dict的key(configuration,'state','挂载状态') temp = 更改dict的key(temp,'configuration','配置信息') if('capabilities' in temp.keys()): capabilities = temp['capabilities'] for key in capabilities: capabilities[key] = get_dict_value(capabilities_dict,key,capabilities[key]) temp = 更改dict的key(temp,'capabilities','设备能力') key = 预定义的class[temp['class']] if(key not in 硬件详细报表_dict.keys()): 硬件详细报表_dict[key]=[temp] else: 句柄列表 = [] for info in 硬件详细报表_dict[key]: 句柄=get_dict_value(info,'句柄') if(0!=len(句柄)): 句柄列表.append(句柄) if(get_dict_value(temp,'句柄') not in 句柄列表): 硬件详细报表_dict[key].append(temp); def network_识别(network_object:dict): global 硬件详细报表_dict capabilities_dict={'ethernet':"以太网功能","physical":"Physical interface(物理接口)"} temp=基本信息处理(network_object) if('configuration' in temp.keys()): configuration = temp['configuration'] configuration = 更改dict的key(configuration,'autonegotiation','自动协商功能') configuration = 更改dict的key(configuration,'broadcast','广播通信') configuration = 更改dict的key(configuration,'driver','驱动程序') configuration = 更改dict的key(configuration,'duplex','双工模式') configuration = 更改dict的key(configuration,'firmware','固件版本') configuration = 更改dict的key(configuration,'ip','IP地址') configuration = 更改dict的key(configuration,'link','连接状态') configuration = 更改dict的key(configuration,'multicast','多播通信') configuration = 更改dict的key(configuration,'speed','最大传输速率') temp = 更改dict的key(temp,'configuration','配置信息') if('capabilities' in temp.keys()): capabilities = temp['capabilities'] for key in capabilities: capabilities[key] = get_dict_value(capabilities_dict,key,capabilities[key]) temp = 更改dict的key(temp,'capabilities','设备能力') key = 预定义的class[temp['class']] if(key not in 硬件详细报表_dict.keys()): 硬件详细报表_dict[key]=[temp] else: 句柄列表 = [] for info in 硬件详细报表_dict[key]: 句柄=get_dict_value(info,'句柄') if(0!=len(句柄)): 句柄列表.append(句柄) if(get_dict_value(temp,'句柄') not in 句柄列表): 硬件详细报表_dict[key].append(temp); def communication_识别(communication_object:dict): global 硬件详细报表_dict capabilities_dict={"pnp": "Plug and Play(即插即用)"} temp=基本信息处理(communication_object) if('configuration' in temp.keys()): configuration = temp['configuration'] configuration = 更改dict的key(configuration,'driver','驱动程序') temp = 更改dict的key(temp,'configuration','配置信息') if('capabilities' in temp.keys()): capabilities = temp['capabilities'] for key in capabilities: capabilities[key] = get_dict_value(capabilities_dict,key,capabilities[key]) temp = 更改dict的key(temp,'capabilities','设备能力') key = 预定义的class[temp['class']] if(key not in 硬件详细报表_dict.keys()): 硬件详细报表_dict[key]=[temp] else: 句柄列表 = [] for info in 硬件详细报表_dict[key]: 句柄=get_dict_value(info,'句柄') if(0!=len(句柄)): 句柄列表.append(句柄) if(get_dict_value(temp,'句柄') not in 句柄列表): 硬件详细报表_dict[key].append(temp); def bridge_识别(bridge_object:dict): global 硬件详细报表_dict capabilities_dict={"isa": "ISA compatibility mode(ISA兼容模式)","bus_master": "bus mastering(总线控制)","bridge":"桥接功能"} temp=基本信息处理(bridge_object) if('configuration' in temp.keys()): configuration = temp['configuration'] configuration = 更改dict的key(configuration,'latency','延迟') temp = 更改dict的key(temp,'configuration','配置信息') if('capabilities' in temp.keys()): capabilities = temp['capabilities'] for key in capabilities: capabilities[key] = get_dict_value(capabilities_dict,key,capabilities[key]) temp = 更改dict的key(temp,'capabilities','设备能力') key = 预定义的class[temp['class']] if(key not in 硬件详细报表_dict.keys()): 硬件详细报表_dict[key]=[temp] else: 句柄列表 = [] for info in 硬件详细报表_dict[key]: 句柄=get_dict_value(info,'句柄') if(0!=len(句柄)): 句柄列表.append(句柄) if(get_dict_value(temp,'句柄') not in 句柄列表): 硬件详细报表_dict[key].append(temp); def bus_识别(bus_object:dict): global 硬件详细报表_dict capabilities_dict={"isa": "ISA compatibility mode(ISA兼容模式)","bus_master": "bus mastering(总线控制)","bridge":"桥接功能"} temp=基本信息处理(bus_object) if('configuration' in temp.keys()): configuration = temp['configuration'] configuration = 更改dict的key(configuration,'latency','延迟') temp = 更改dict的key(temp,'configuration','配置信息') if('capabilities' in temp.keys()): capabilities = temp['capabilities'] for key in capabilities: capabilities[key] = get_dict_value(capabilities_dict,key,capabilities[key]) temp = 更改dict的key(temp,'capabilities','设备能力') key = 预定义的class[temp['class']] if(key not in 硬件详细报表_dict.keys()): 硬件详细报表_dict[key]=[temp] else: 句柄列表 = [] for info in 硬件详细报表_dict[key]: 句柄=get_dict_value(info,'句柄') if(0!=len(句柄)): 句柄列表.append(句柄) if(get_dict_value(temp,'句柄') not in 句柄列表): 硬件详细报表_dict[key].append(temp); def disk_识别(disk_object:dict): global 硬件详细报表_dict capabilities_dict={"removable": "support is removable(支架可移动)","audio": "Audio CD playback(支持音频CD播放)","dvd":"DVD playback(支持音频DVD播放)","partitioned":"Partitioned disk(分区磁盘)","partitioned:dos":"MS-DOS partition table(MS-DOS分区表)"} temp=基本信息处理(disk_object) if('configuration' in temp.keys()): configuration = temp['configuration'] configuration = 更改dict的key(configuration,'ansiversion','ANSI版本') configuration = 更改dict的key(configuration,'logicalsectorsize','逻辑扇区大小') configuration = 更改dict的key(configuration,'sectorsize','扇区大小') configuration = 更改dict的key(configuration,'signature','签名') configuration = 更改dict的key(configuration,'status','状态(nodisc:无光盘插入)') temp = 更改dict的key(temp,'configuration','配置信息') if('capabilities' in temp.keys()): capabilities = temp['capabilities'] for key in capabilities: capabilities[key] = get_dict_value(capabilities_dict,key,capabilities[key]) temp = 更改dict的key(temp,'capabilities','设备能力') key = 预定义的class[temp['class']] if(key not in 硬件详细报表_dict.keys()): 硬件详细报表_dict[key]=[temp] else: 句柄列表 = [] for info in 硬件详细报表_dict[key]: 句柄=get_dict_value(info,'句柄') if(0!=len(句柄)): 句柄列表.append(句柄) if(get_dict_value(temp,'句柄') not in 句柄列表): 硬件详细报表_dict[key].append(temp); def system_识别(system_object:dict): global 硬件详细报表_dict capabilities_dict={"pnp": "Plug and Play(即插即用)","smbios-2.3": "SMBIOS(System Management BIOS) version 2.3","dmi-2.3": "DMI(Desktop Management Interface) version 2.3","smp": "Symmetric Multi-Processing(对称多处理,同时执行多个线程或进程)","vsyscall32": "32-bit processes(设备支持32位进程,vsyscall是Linux内核提供的一种系统调用优化技术,用于提高系统调用的性能)"} temp=基本信息处理(system_object) if('configuration' in temp.keys()): configuration = temp['configuration'] configuration = 更改dict的key(configuration,'driver','驱动程序') configuration = 更改dict的key(configuration,'boot','启动方式') configuration = 更改dict的key(configuration,'chassis','机箱类型') configuration = 更改dict的key(configuration,'uuid','通用唯一识别码') temp = 更改dict的key(temp,'configuration','配置信息') if('capabilities' in temp.keys()): capabilities = temp['capabilities'] for key in capabilities: capabilities[key] = get_dict_value(capabilities_dict,key,capabilities[key]) temp = 更改dict的key(temp,'capabilities','设备能力') key = 预定义的class[temp['class']] if(key not in 硬件详细报表_dict.keys()): 硬件详细报表_dict[key]=[temp] else: 句柄列表 = [] for info in 硬件详细报表_dict[key]: 句柄=get_dict_value(info,'句柄') if(0!=len(句柄)): 句柄列表.append(句柄) if(get_dict_value(temp,'句柄') not in 句柄列表): 硬件详细报表_dict[key].append(temp); def input_识别(input_object:dict): global 硬件详细报表_dict capabilities_dict={"pnp": "Plug and Play(即插即用)"} temp=基本信息处理(input_object) if('configuration' in temp.keys()): configuration = temp['configuration'] configuration = 更改dict的key(configuration,'driver','驱动程序') temp = 更改dict的key(temp,'configuration','配置信息') if('capabilities' in temp.keys()): capabilities = temp['capabilities'] for key in capabilities: capabilities[key] = get_dict_value(capabilities_dict,key,capabilities[key]) temp = 更改dict的key(temp,'capabilities','设备能力') key = 预定义的class[temp['class']] if(key not in 硬件详细报表_dict.keys()): 硬件详细报表_dict[key]=[temp] else: 句柄列表 = [] for info in 硬件详细报表_dict[key]: 句柄=get_dict_value(info,'句柄') if(0!=len(句柄)): 句柄列表.append(句柄) if(get_dict_value(temp,'句柄') not in 句柄列表): 硬件详细报表_dict[key].append(temp); def display_识别(display_object:dict): global 硬件详细报表_dict capabilities_dict={"vga_controller": "支持VGA控制器","bus_master":"bus mastering(总线主控)","rom":"extension ROM(扩展ROM)"} temp=基本信息处理(display_object) if('configuration' in temp.keys()): configuration = temp['configuration'] configuration = 更改dict的key(configuration,'driver','驱动程序') configuration = 更改dict的key(configuration,'latency','延迟') temp = 更改dict的key(temp,'configuration','配置信息') if('capabilities' in temp.keys()): capabilities = temp['capabilities'] for key in capabilities: capabilities[key] = get_dict_value(capabilities_dict,key,capabilities[key]) temp = 更改dict的key(temp,'capabilities','设备能力') key = 预定义的class[temp['class']] if(key not in 硬件详细报表_dict.keys()): 硬件详细报表_dict[key]=[temp] else: 句柄列表 = [] for info in 硬件详细报表_dict[key]: 句柄=get_dict_value(info,'句柄') if(0!=len(句柄)): 句柄列表.append(句柄) if(get_dict_value(temp,'句柄') not in 句柄列表): 硬件详细报表_dict[key].append(temp); def power_识别(power_object:dict): global 硬件详细报表_dict capabilities_dict={} temp=基本信息处理(power_object) if('configuration' in temp.keys()): # configuration = temp['configuration'] # configuration = 更改dict的key(configuration,'driver','驱动程序') # configuration = 更改dict的key(configuration,'latency','延迟') temp = 更改dict的key(temp,'configuration','配置信息') if('capabilities' in temp.keys()): capabilities = temp['capabilities'] for key in capabilities: capabilities[key] = get_dict_value(capabilities_dict,key,capabilities[key]) temp = 更改dict的key(temp,'capabilities','设备能力') key = 预定义的class[temp['class']] if(key not in 硬件详细报表_dict.keys()): 硬件详细报表_dict[key]=[temp] else: 句柄列表 = [] for info in 硬件详细报表_dict[key]: 句柄=get_dict_value(info,'句柄') if(0!=len(句柄)): 句柄列表.append(句柄) if(get_dict_value(temp,'句柄') not in 句柄列表): 硬件详细报表_dict[key].append(temp); def generic_识别(generic_object:dict): global 硬件详细报表_dict capabilities_dict={} temp=基本信息处理(generic_object) if('configuration' in temp.keys()): # configuration = temp['configuration'] # configuration = 更改dict的key(configuration,'driver','驱动程序') # configuration = 更改dict的key(configuration,'latency','延迟') temp = 更改dict的key(temp,'configuration','配置信息') if('capabilities' in temp.keys()): capabilities = temp['capabilities'] for key in capabilities: capabilities[key] = get_dict_value(capabilities_dict,key,capabilities[key]) temp = 更改dict的key(temp,'capabilities','设备能力') key = 预定义的class[temp['class']] if(key not in 硬件详细报表_dict.keys()): 硬件详细报表_dict[key]=[temp] else: 句柄列表 = [] for info in 硬件详细报表_dict[key]: 句柄=get_dict_value(info,'句柄') if(0!=len(句柄)): 句柄列表.append(句柄) if(get_dict_value(temp,'句柄') not in 句柄列表): 硬件详细报表_dict[key].append(temp); def 组件识别(): ''' 函数作用是识别各个组件,然后调用对应的函数去处理 ''' global 硬件信息列表_list for info in 硬件信息列表_list: function_name = '{}_识别'.format(info['class']) if function_name in globals(): func_object = globals()[function_name] #使用反射 func_object(info) else: print(f"函数 {function_name} 没有被定义。") 硬件信息列表_list=[] #里面是dict,但是不包含children def 遍历元素(datas_dict:dict): ''' 遍历整个json,从中获取基本信息和configuration、capabilities 这里主要解决的就是TM的层次问题,lshw的输出结果的层次简直不当人子 ''' global 硬件信息列表_list temp=copy.deepcopy(datas_dict) if('children' in datas_dict.keys()): del temp['children'] 硬件信息列表_list.append(temp) for children in datas_dict['children']: 遍历元素(children) else: 硬件信息列表_list.append(temp); 预定义的class={'generic':'通用设备','display':'显示设备','network':'网卡设备','storage':'存储设备','communication':'通讯设备','bridge':'桥接设备','memory':'内存设备','volume':'卷设备','system':'系统信息','input':'输入设备','bus':'总线','processor':'处理器','disk':'磁盘设备','power':"电源设备"} #{class:描述},} #,'multimedia':'多媒体设备':暂未遇到 def 统计class(): ''' 统计出有那些class,检查是否存在不存在于【预定义的class】变量的情况 ''' global 硬件信息列表_list class_set=set() for info in 硬件信息列表_list: class_set.add(info['class']) 预定义的class_set = set(预定义的class.keys()) ret_set = class_set.difference(预定义的class_set) #求差集 if(0 == len(ret_set)): return True else: print("以下class未被定义:") print(ret_set) return False; def 汇聚概览信息(ip:str,port:int=22): ''' 提取主要的信息进行显示: 电脑型号/处理器/主板/显卡/内存/主硬盘/显示器/声卡/网卡 ''' global 硬件详细报表_dict 系统信息 = None for info in 硬件详细报表_dict['系统信息']: 描述= get_dict_value(info,'描述') # if('Rack Mount Chassis' == 描述 or "Desktop Computer" == 描述): if(''!=描述): 系统信息=info break if(None == 系统信息): print("系统信息 没有找到所需要的json。") print(硬件详细报表_dict['系统信息']) exit(-1) 电脑型号 = "{} {} ({})".format(系统信息['产品型号'],系统信息['描述'],系统信息['id']) 处理器_json = None for info in 硬件详细报表_dict['处理器']: if('设备能力(指令集)' in info.keys()): 处理器_json=info break if(None == 处理器_json): print("设备能力(指令集) 没有找到所需要的json。") exit(-1) 禁用数量=0 for info in 硬件详细报表_dict['处理器']: if('禁用' in info.keys()): 禁用数量+=1 处理器 = "{} (X{})".format(处理器_json['产品型号'],len(硬件详细报表_dict['处理器'])-禁用数量) 主板_json = None for info in 硬件详细报表_dict['总线']: 描述= get_dict_value(info,'描述') if('Motherboard' == 描述): 主板_json=info break if(None == 主板_json): print("主板信息 没有找到所需要的json。") exit(-1) 主板 = "{} {}".format(主板_json['产品型号'],主板_json['制造商']) 显卡_json = 硬件详细报表_dict['显示设备'] if(1 != len(显卡_json)): print("提取显卡信息时,遇到多个显示设备") print(显卡_json) exit(-1) 显卡_json=显卡_json[0] 显卡 = "{} {}".format(显卡_json['产品型号'],主板_json['制造商']) 内存_json = None bank=None for info in 硬件详细报表_dict['内存设备']: 描述= get_dict_value(info,'描述') if('System Memory' == 描述): 内存_json=info break if(None == 内存_json): print("内存 没有找到所需要的json。") exit(-1) 总容量 = 内存_json['大小/速度/尺寸'] for info in 硬件详细报表_dict['内存设备']: if('bank' in info['id'] and 'empty' not in info['描述']): bank=info break 内存="{}({})".format(总容量,get_dict_value(bank,'描述')) #固定取sda 磁盘_json = None for info in 硬件详细报表_dict['磁盘设备']: 逻辑名称= get_dict_value(info,'逻辑名称') if('/dev/sda' == 逻辑名称): 磁盘_json=info break if(None == 磁盘_json): print("磁盘_json 没有找到所需要的json。") exit(-1) 硬盘 = "{} {} ({})".format(磁盘_json['产品型号'],磁盘_json['制造商'],磁盘_json['大小/速度/尺寸']) #显示器就略过。 #声卡,暂时没发现 网卡_json = None 虚拟网卡列表 = 命令执行(ip,"ls /sys/devices/virtual/net/ | sed \"s/ \+/ /g\"",port).split('\n') for info in 硬件详细报表_dict['网卡设备']: if(info['逻辑名称'] not in 虚拟网卡列表): 网卡_json=info break if(None == 网卡_json): print("网卡_json 没有找到所需要的json。") exit(-1) 网卡 = "{} {}".format(网卡_json['产品型号'],网卡_json['制造商']) 硬件详细报表_dict['概述']=[{"电脑型号":电脑型号,},{"处理器":处理器,},{"主板":主板,},{"显卡":显卡,},{"内存":内存,},{"硬盘":硬盘,},{"网卡":网卡,},] def main(): # 解析lshw命令结果(命令结果转换为json对象(命令执行('127.0.0.1','lshw -json',22))) # print(globals()) global 硬件信息列表_list,硬件详细报表_dict # ips=["22.62.96.36","22.62.96.37","22.62.96.38","22.62.96.39","22.62.96.40","22.62.96.41","22.62.96.42","22.62.96.43","22.62.96.44","22.62.96.45","22.62.96.46","22.62.96.47","22.62.96.48","22.62.96.49","22.62.96.50","22.62.96.51","22.62.96.52","22.62.96.53","22.62.96.54",] hostname_ip_dict={"harbor-001":"22.62.96.36","file-001":"22.62.96.37","database-001":"22.62.96.38","database-002":"22.62.96.39","database-003":"22.62.96.40","clickhouse-001":"22.62.96.41","clickhouse-002":"22.62.96.42","clickhouse-003":"22.62.96.43","clickhouse-004":"22.62.96.44","clickhouse-005":"22.62.96.45","clickhouse-006":"22.62.96.46","k8s-master-001":"22.62.96.47","k8s-master-002":"22.62.96.48","k8s-master-003":"22.62.96.49","k8s-node-001":"22.62.96.50","k8s-node-002":"22.62.96.51","emqx-001":"22.62.96.52","emqx-002":"22.62.96.53","emqx-003":"22.62.96.54",} # ips=["22.62.96.36"] for hostname,ip in hostname_ip_dict.items(): with open(f'/home/aduser/temp/harwareInfo/{hostname}-hardwareInfo.txt','w',encoding='utf-8')as out: datas_str = 命令执行(ip,'sudo lshw -json',10022) datas= 命令结果转换为json对象(datas_str) 遍历元素(datas) # for info in 硬件信息列表_list: # if(info['class'] == 'display'): # print(info) 统计class() 组件识别() 汇聚概览信息(ip,10022) print('概述',file=out) for info in 硬件详细报表_dict['概述']: print(info,file=out) for key,infos in 硬件详细报表_dict.items(): if(key == '概述'): continue print(key,file=out) for info in infos: print(info,file=out) print(f"{hostname} 已收集完成。") 硬件详细报表_dict.clear() 硬件信息列表_list.clear() if __name__ == '__main__': main()
输出到文件中去,大概长这个样子: