9-09 11 views
版本
Python:3.x
Windows
安装依赖库
如果觉得pip的下载速度慢,可以使用国内的安装源: mirrors.aliyun.com
用例:
1 |
python.exe -m pip install pywin32 --index-url=http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com |
1 2 |
python.exe -m pip install pywin32 python.exe -m pip install wmi |
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # @Author : Eric Winn # @Email : eng.eric.winn@gmail.com # @Time : 2018-5-17 17:16 # @Version : 1.0 # @File : get_os_info_win # @Software : PyCharm import wmi w = wmi.WMI() # 获取硬盘信息 def get_disk_total(): disk_total = 0 for point in w.Win32_DiskDrive(): disk_total += int(point.Size) return "%.0fG" % (disk_total * 1000 / 1000 / 1000 / 1000 / 1000) # 获取网上信息 class get_netcard(object): def __init__(self): self.obj = w.Win32_NetworkAdapterConfiguration(IPEnabled=1) def get_ip_address(self): return self.obj[0].IPAddress[0] def get_mac_address(self): return self.obj[0].MacAddress # 获取系统的一些详细信息 class get_os(): def __init__(self): self.obj = w.Win32_OperatingSystem()[0] def get_hostname(self): return self.obj.CSName def get_sn(self): return self.obj.SerialNumber def get_os_version(self): return self.obj.Caption def get_architecture(self): return self.obj.OSArchitecture # 获取主版上的一些详细信息 class get_bios(): def __init__(self): self.obj = w.Win32_ComputerSystem()[0] def get_memory_total(self): return int(self.obj.TotalPhysicalMemory) / 1024 / 1024 def get_vendor(self): return self.obj.Manufacturer def get_model(self): return self.obj.Model def get_cpu_processors(self): return self.obj.NumberOfProcessors # 获取CPU的详细信息 class get_cpu_info(): def __init__(self): self.obj = w.Win32_Processor() self.processor=[] def get_cpu_process(self): for cpu in self.obj: self.processor.append(cpu.DeviceID.split('CPU')[1]) self.processor.append(cpu.name) return self.processor def get_cpu_cores(self): return self.obj[0].NumberOfCores # 实例化 netcard_info = get_netcard() os_info = get_os() bios_info = get_bios() cpu_info = get_cpu_info() data = { 'ipv4_addresses': netcard_info.get_ip_address(), 'mac': netcard_info.get_mac_address(), 'system_status': 2, "hostname": os_info.get_hostname(), "distribution": os_info.get_os_version(), "distribution_version": None, "processor_count": bios_info.get_cpu_processors(), "memtotal_mb": bios_info.get_memory_total(), "devices": { "sda": { "removable": "0", "size": get_disk_total(), } }, "processor": cpu_info.get_cpu_process(), "processor_cores": cpu_info.get_cpu_cores(), "architecture": os_info.get_architecture(), "product_name": bios_info.get_model(), "product_serial": os_info.get_sn(), "system_vendor": bios_info.get_vendor(), } |
Linux
安装依赖库
1 2 3 |
python3 -m pip install psutil python3 -m pip install platform python3 -m pip install collections |
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # @Author : Eric Winn # @Email : eng.eric.winn@gmail.com # @Time : 2018-5-17 17:06 # @Version : 1.0 # @File : get_os_info_linux # @Software : PyCharm import sys import platform import psutil from collections import OrderedDict # 获取CPU相关信息 class CPUinfo(object): def __init__(self): self.CPUinfo = OrderedDict() self.processor_cores = 0 self.processor = [] self.processor_count_list = [] procinfo = OrderedDict() nprocs = 0 with open('/proc/cpuinfo') as f: for line in f: if not line.strip(): self.CPUinfo['proc%s' % nprocs] = procinfo nprocs = nprocs + 1 # Reset procinfo = OrderedDict() else: if len(line.split(':')) == 2: procinfo[line.split(':')[0].strip()] = line.split(':')[1].strip() else: procinfo[line.split(':')[0].strip()] = '' def get_cpu_info(self): for k, v in self.CPUinfo.items(): if not self.processor_cores: self.processor_cores = int(v.get('cpu cores')) self.processor.append(v.get('processor')) self.processor.append(v.get('model name')) if v.get('processor') not in self.processor_count_list: self.processor_count_list.append(v.get('core id')) def get_cpu_processor(self): return self.processor def get_cpu_processor_cores(self): return self.processor_cores def get_cpu_processor_count(self): return len(self.processor_count_list) # 获取硬盘 def get_disk_total(): disk_total = 0 for point in psutil.disk_partitions(): disk_total += int(psutil.disk_usage(point.mountpoint).total) disk_total += int(psutil.swap_memory().total) return "%.0fG" % (disk_total * 1000 / 1000 / 1000 / 1000 / 1000) # 获取内存 def get_memory_total(): return psutil.virtual_memory().total / 1024 / 1024 # 获取ip、mac class get_netcard(object): def __init__(self): self.netcard_info = [] self.info = psutil.net_if_addrs() for k, v in self.info.items(): if len(v) >= 2 and k != 'lo': for item in v: if item[0] == 2: dev = k ip = item[1] if item[0] == 17: mac = item[1] self.netcard_info.append((dev, ip, mac)) def get_ip_address(self): return self.netcard_info[0][1] def get_mac_address(self): return self.netcard_info[0][2] # 利用系统中的dmidecode命令获取厂商、序列号等信息 def get_system_info(): import subprocess sys_dict = {} sys = subprocess.Popen(['dmidecode', '-t', 'system'], stdout=subprocess.PIPE) sys_list = sys.stdout.readlines() for line in sys_list: if "Serial Number" in line.decode(): sys_dict["product_serial"] = line.decode().split(':')[1].strip() if "Product Name" in line.decode(): sys_dict["product_name"] = line.decode().split(':')[1].strip() if "Manufacturer" in line.decode(): sys_dict["system_vendor"] = line.decode().split(':')[1].strip() return sys_dict # 获取系统版本 os_distribution = platform.linux_distribution() # 实例化 system_info = get_system_info() netcard_info = get_netcard() cpu_info = CPUinfo() cpu_info.get_cpu_info() # 获取详细信息 data = { 'ipv4_addresses': netcard_info.get_ip_address(), 'mac': netcard_info.get_mac_address(), 'system_status': 2, "hostname": platform.node(), "distribution": os_distribution[0], "distribution_version": os_distribution[1], "processor_count": cpu_info.get_cpu_processor_count(), "memtotal_mb": get_memory_total(), "devices": { "sda": { "removable": "0", "size": get_disk_total(), } }, "processor": cpu_info.processor, "processor_cores": cpu_info.processor_cores, "architecture": platform.processor(), "product_name": system_info.get('product_name'), "product_serial": system_info.get('product_serial'), "system_vendor": system_info.get('system_vendor'), } |
如果想赏钱,可以用微信扫描下面的二维码,一来能刺激我写博客的欲望,二来好维护云主机的费用; 另外再次标注博客原地址 itnotebooks.com 感谢!
