python获取硬件信息-老罗rogabet-xls表格

#! -*- coding:UTF-8 -*-
import xlwt,os,sys,re
import wmi,json,base64
from datetime import datetime
import time
import wmi
c = wmi.WMI ()
hello='''Hello Dear!
The tool will collect hostname,current username,OS name,SerialNumber,IP and MAC address of the PC.
Please do not close any black dialog during the collection time. After exporting a table file pc-info.xls,the program will close automatically.
'''
print(hello)

for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1):
    print (interface.MACAddress)
    macaddr = interface.MACAddress
    for ip_address in interface.IPAddress:
        print(ip_address)


mysncmd="wmic bios get serialnumber > sn.txt"
myhost="hostname  > host.txt"
myusername="echo %username% > username.txt"
osname='systeminfo | find /i "OS 名称" > osname.txt'
pctype='systeminfo | find /i "系统型号" > pctype.txt'
memall='systeminfo | find /i "物理内存总" > memall.txt'
domain='systeminfo | find /i "域:" > domain.txt'
# other
    # 主板序列号
def get_mainboard_info():
    mainboard = []
    for board_id in c.Win32_BaseBoard():
        mainboard.append(board_id.SerialNumber.strip().strip('.'))
    return mainboard

#-----------------------------------------------------------------sn-
with open("sn.bat","wt") as outfile:
    outfile.write(mysncmd)
os.startfile('sn.bat')
time.sleep(1)

with open('sn.txt','rb') as snfile:
    sn=snfile.read().decode('GBK','ignore')
kill="S"
be=""
kill2="e"
be2=""
kill3="r"
be3=""
kill4="i"
be4=""
kill5="a"
be5=""
kill6="l"
be6=""
kill7="N"
be7=""
kill8="u"
be8=""
kill9="m"
be9=""
kill10="b"
be10=""
kill11="e"
be11=""
kill12="r"
be12=""
kill13='\t\t\t\t'
be13=""
sn2=sn.replace(kill,be)
sn3=sn2.replace(kill2,be2)
sn4=sn3.replace(kill3,be3)
sn5=sn4.replace(kill4,be4)
sn6=sn5.replace(kill5,be5)
sn7=sn6.replace(kill6,be6)
sn8=sn7.replace(kill7,be7)
sn9=sn8.replace(kill8,be8)
sn10=sn9.replace(kill9,be9)
sn11=sn10.replace(kill10,be10)
sn12=sn11.replace(kill11,be11)
sn13=sn12.replace(kill2,be12)
sn14=sn13.replace(kill3,be13)
sn15=sn14.strip()

with open("sn2.txt","wt") as sn2file:
    sn2file.write(sn15)
with open('sn2.txt','rb') as sn3file:
    snget0=sn3file.read().decode('GBK','ignore')

snget=snget0


#-------------------------------------------------------------hostname
with open("host.bat","wt") as hostfile:
    hostfile.write(myhost)
os.startfile('host.bat')
time.sleep(2)
with open('host.txt','rb') as host2file:
    host2=host2file.read().decode('GBK','ignore')
hostget=host2

#------------------------------------------------------------username
with open("username.bat","wt") as usernfile:
    usernfile.write(myusername)
os.startfile('username.bat')
time.sleep(3)
with open('username.txt','rb') as user2file:
    user2=user2file.read().decode('GBK','ignore')
userget=user2

#--------------------------------------------------------------osname
with open("osname.bat","wt") as osnfile:
    osnfile.write(osname)
os.startfile('osname.bat')
time.sleep(8)
with open('osname.txt','rb') as osn2file:
    osn2=osn2file.read().decode('GBK','ignore')
kill14='OS 名称:     '
be14=""
osn3=osn2.replace(kill14,be14)
osn4=osn3.strip()
osnget=osn4

#--------------------------------------------------------------pctype
with open("pctype.bat","wt") as pctypefile:
    pctypefile.write(pctype)
os.startfile('pctype.bat')
time.sleep(8)
with open('pctype.txt','rb') as pct2file:
    pct2=pct2file.read().decode('GBK','ignore')
kill15='系统型号:     '
be15=""
pct3=pct2.replace(kill15,be15)
pct4=pct3.strip()
pctget=pct4

#-------------------------------------------------------------snget2
bios=wmi.WMI().win32_BIOS()[0]
snget2=bios.SerialNumber
#--------------------------------------------------------------memall
with open("memall.bat","wt") as memafile:
    memafile.write(memall)
os.startfile('memall.bat')
time.sleep(8)
with open('memall.txt','rb') as mem2file:
    mem2=mem2file.read().decode('GBK','ignore')
kill16='物理内存总量:  '
be16=""
mem3=mem2.replace(kill16,be16)
mem4=mem3.strip()
memget=mem4
#------------------------------------------------------domain
with open("domain.bat","wt") as domfile:
    domfile.write(domain)
os.startfile('domain.bat')
time.sleep(8)
with open('domain.txt','rb') as dom2file:
    dom2=dom2file.read().decode('GBK','ignore')
kill17='域:  '
be17=""
dom3=dom2.replace(kill17,be17)
dom4=dom3.strip()
domget=dom4

#---------------------------------------------------------xls
style0 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on',num_format_str='#,##0.00')
style1 = xlwt.easyxf(num_format_str='D-MMM-YY')
 
wb = xlwt.Workbook()
ws = wb.add_sheet('pc_info_%s'%userget)
 
ws.write(0, 0, datetime.now(), style1)

ws.write(1, 0, 'hostname:')
ws.write(1, 1, hostget)
ws.write(2, 0, 'username:')
ws.write(2, 1, userget)
ws.write(3, 0, 'serialnumber::')
ws.write(3, 1, snget2)
ws.write(4, 0, 'PC type:')
ws.write(4, 1, pctget)
ws.write(5, 0, 'OS name:')
ws.write(5, 1, osnget)
ws.write(6, 0, 'mainboard:')
ws.write(6, 1, get_mainboard_info())
ws.write(7, 0, 'macaddr:')
ws.write(7, 1, macaddr)
ws.write(8, 0, 'ipaddr:')
ws.write(8, 1, interface.IPAddress[0])
ws.write(9, 0, 'Mem All:')
ws.write(9, 1, memget)
ws.write(10, 0, 'domain:')
ws.write(10, 1, domget)

ws.write(0, 1, snget,style0)

wb.save('pcinfo_%s.xls'%snget2)

outfile.close()
snfile.close()
sn2file.close()
sn3file.close()
hostfile.close()
host2file.close()
usernfile.close()
user2file.close()
osnfile.close()
osn2file.close()
pctypefile.close()
pct2file.close()
domfile.close()
dom2file.close()
memafile.close()
mem2file.close()

os.remove('host.bat')
os.remove('host.txt')
os.remove('osname.bat')
os.remove('osname.txt')
os.remove('pctype.bat')
os.remove('pctype.txt')
os.remove('sn.bat')
os.remove('sn.txt')
os.remove('sn2.txt')
os.remove('username.bat')
os.remove('username.txt')
os.remove('domain.bat')
os.remove('domain.txt')

os.remove('memall.bat')
os.remove('memall.txt')
bye='''
The tool will export the info about hostname,current username,OS name,SerialNumber,IP and MAC address of the PC.
Please do not close any black dialogs during the collection time. After exporting a table and txt files ,the program will close automatically.
Made by Luo jiabei 2019 Apr
'''
print(bye)

#----------------------------------------------------------more
global list
list=[]
def info():
    list.append("电脑信息")
    for BIOSs in c.Win32_ComputerSystem():
        list.append("电脑名称: %s" %BIOSs.Caption)
        list.append("使 用 者: %s" %BIOSs.UserName)
    for address in c.Win32_NetworkAdapterConfiguration(ServiceName = "e1dexpress"):
        list.append("IP地址: %s" % address.IPAddress[0])
        list.append("MAC地址: %s" % address.MACAddress)
    for BIOS in c.Win32_BIOS():
        list.append("Bios描述: %s" %BIOS.Description)
        list.append("序列号: %s" %BIOS.SerialNumber)
    for processor in c.Win32_Processor():
        list.append("CPU型号: %s" % processor.Name.strip())
    for memModule in c.Win32_PhysicalMemory():
        totalMemSize=int(memModule.Capacity)
        list.append("内存厂商: %s" %memModule.Manufacturer)
        list.append("内存型号: %s" %memModule.PartNumber)
        list.append("内存大小: %.2fGB" %(totalMemSize/1024**3))
    for disk in c.Win32_DiskDrive(InterfaceType = "IDE"):
        diskSize=int(disk.size)
        list.append("磁盘名称: %s" %disk.Caption)
        list.append("磁盘大小: %.2fGB" %(diskSize/1024**3))

    for xk in c.Win32_VideoController():
        list.append("显卡名称: %s" %xk.name)
    for disk in c.Win32_DiskDrive(InterfaceType = "SCSI"):
        diskSize=int(disk.size)
        list.append("磁盘名称: %s" %disk.Caption)
        list.append("磁盘大小: %.2fGB" %(diskSize/1024**3))
    for disk in c.Win32_DiskDrive(InterfaceType = "HDC"):
        diskSize=int(disk.size)
        list.append("磁盘名称: %s" %disk.Caption)
        list.append("磁盘大小: %.2fGB" %(diskSize/1024**3))
    for disk in c.Win32_DiskDrive(InterfaceType = "1394"):
        diskSize=int(disk.size)
        list.append("磁盘名称: %s" %disk.Caption)
        list.append("磁盘大小: %.2fGB" %(diskSize/1024**3))
#https://docs.microsoft.com/zh-cn/windows/desktop/CIMWin32Prov/win32-diskdrive
def main():
    global path
    path= os.getcwd()
    for BIOSs in c.Win32_ComputerSystem():
        UserNames=BIOSs.Caption
    fileName='pcinfo_%s.txt'%snget2
    info()
   #判断文件夹(路径)是否存在
    if not os.path.exists(path):
        print("不存在")
        #创建文件夹(文件路径)
        os.makedirs(path)
        #写入文件信息
        with open(fileName,'w+') as f:
            for li in list:
                print(li)
                l=li+"\n"
                f.write(l)
    else:
        print("存在")
        with open(fileName,'w+') as f:
            for li in list:
                print(li)
                l=li+"\n"
                f.write(l)

main()
time.sleep(8)
-----------------------------------------------------------------rest
#https://blog.csdn.net/fengmm521/article/details/79468677#
#https://blog.csdn.net/showy_ho/article/details/38293375
#https://blog.csdn.net/oh5W6HinUg43JvRhhB/article/details/79861047
def printMain_board():
    boards = []
    # print len(c.Win32_BaseBoard()):
    for board_id in c.Win32_BaseBoard():
        tmpmsg = {}
        tmpmsg['UUID'] = board_id.qualifiers['UUID'][1:-1]   #主板UUID,有的主板这部分信息取到为空值,ffffff-ffffff这样的
        tmpmsg['SerialNumber'] = board_id.SerialNumber                #主板序列号
        tmpmsg['Manufacturer'] = board_id.Manufacturer       #主板生产品牌厂家
        tmpmsg['Product'] = board_id.Product                 #主板型号

        boards.append(tmpmsg)
    print (boards)
    return boards
printMain_board()

def printCPU():
    tmpdict = {}
    tmpdict["CpuCores"] = 0
    for cpu in c.Win32_Processor():
        tmpdict["cpuid"] = cpu.ProcessorId.strip()
        tmpdict["CpuType"] = cpu.Name
        tmpdict['systemName'] = cpu.SystemName
        try:
            tmpdict["CpuCores"] = cpu.NumberOfCores
        except:
            tmpdict["CpuCores"] += 1
        tmpdict["CpuClock"] = cpu.MaxClockSpeed
        tmpdict['DataWidth'] = cpu.DataWidth
    print (tmpdict)
    return  tmpdict
printCPU()

猜你喜欢

转载自blog.csdn.net/rogabet/article/details/89710485