python - 获取win系统参数,发送/保存配置

 

 

import wmi
import json

c = wmi.WMI ()
msg = {}

# 系统信息:系统版本,主机名,系统安装时间,系统位数,串口ID,总内存大小
system = ['Caption','CSName','InstallDate','OSArchitecture','SerialNumber','TotalVisibleMemorySize']
system_msg = []
for sys in c.Win32_OperatingSystem():
    for i in system:
        if i == 'InstallDate':
            system_msg.append(getattr(sys,i).split('.')[0])
        elif i == 'TotalVisibleMemorySize':
            system_msg.append("%sGB"% round(int(getattr(sys, i))/(1024*1024)))
        else:
            system_msg.append(getattr(sys, i))
msg["sys_msg"] = dict(zip(system,system_msg))

# # CPU
cpu = ['Name']
cpu_msg = []
for i in c.Win32_Processor():
    for i2 in cpu:
        cpu_msg.append(getattr(i, i2))
msg['cpu']= dict(zip(cpu,cpu_msg))

# # 内存参数:
m0 = ['Capacity','Caption','Manufacturer','PartNumber','SerialNumber','Speed','Tag',]
m2 = {}
for i in enumerate(c.Win32_PhysicalMemory()):
    m1 = []
    for i2 in m0:
        m1.append(getattr(i[1], i2))
    m2["内存位置%s"%i[0]] = dict(zip(m0,m1))
msg.update(m2)

# # {盘符num:{磁盘型号,接口类型,磁盘容量}}
disk = ['Caption','InterfaceType','Size']
disk_count = {}
for i in enumerate(c.Win32_DiskDrive()):
    disk_msg = []
    for i2 in disk:
        if i2 == 'Size':
            disk_msg.append("%s GB"% round(int(getattr(i[1],i2))/(1024*1024*1024)))
        else:
            disk_msg.append(getattr(i[1],i2))
    disk_count["盘符%s"%i[0]] = dict(zip(disk,disk_msg))
msg.update(disk_count)

# #网卡信息 : 网关,网卡卡槽,是否开启DHCP,DNS,IPadd,MASk,MAC
n = ['DefaultIPGateway','Description','DHCPEnabled','DNSServerSearchOrder','IPAddress','IPSubnet','MACAddress',]
n2 = {}
for i in enumerate(c.Win32_NetworkAdapterConfiguration(IPEnabled=1)):
    n1 = []
    for i2 in n:
        n1.append(getattr(i[1],i2))
    n2["网卡%s" % i[0]] = dict(zip(n,n1))
msg.update(n2)

# # BIOS 信息: BIOS版本,BIOS厂商
bios = ['BIOSVersion','Manufacturer',]
b = wmi.WMI().Win32_BIOS()
b1 = []
for i in b:
    for i2 in bios:
        b1.append(getattr(i, i2))
msg['BIOS'] = dict(zip(bios,b1))

data = {"data":json.dumps(msg)}
import requests
url = 'http://172.16.1.2/index/'
response = requests.post(url,data=data)

 

posted @ 2018-11-22 14:55  Anec  阅读(624)  评论(0编辑  收藏  举报