[Python] 纯文本查看 复制代码
#code:UTF-8
import wmi
import os
import pyodbc
pc = wmi.WMI()
def get_network():
dict01={}
for interface in pc.Win32_NetworkAdapterConfiguration(IPEnabled=1):
if not "Virtual" in interface.Caption:
dict01["IP地址"]=f'{interface.IPAddress[0]}'
dict01["子网掩码"]=f'{interface.IPSubnet[0]}'
dict01["网关"]=f'{interface.DefaultIPGateway[0]}'
dict01["首选DNS服务器"]=f'{interface.DNSServerSearchOrder[0]}'
try:
dict01["备选DNS服务器"]=f'{interface.DNSServerSearchOrder[1]}'
except:
dict01["备选DNS服务器"]='没有备选DNS服务器'
dict01["MAC地址"]=f'{interface.MACAddress}'
return dict01
def System_spec():
os_info=pc.Win32_OperatingSystem()[0]
processor=pc.Win32_Processor()[0]
#Gpu=pc.Win32_VideoController()[0]
dict02={}
os_name=os_info.Name.encode('utf-8').split(b'|')[0].decode('utf-8')#操作系统
ram=float(os_info.TotalVisibleMemorySize)/1048576 #内存
#print(f'操作系统:{os_name}')
#print(f'CPU:{processor.Name}')
dict02["操作系统"]=f'{os_name}'
dict02["CPU"]=f'{processor.Name}'
if len(pc.Win32_VideoController())>1:
for i in range(0, len(pc.Win32_VideoController())):
for j in pc.Win32_VideoController(): #显卡
dict02["显卡"+str(i)]=' '.join(j.Caption.split())
else:
dict02["显卡0"] = ' '.join(pc.Win32_VideoController()[0].Caption.split())
dict02["显卡1"]="没有其他显卡"
#print(f'内存:{round(ram)}GB')
dict02["内存"]=f'{round(ram)}GB'
#print(f'显卡{Gpu.Name}')
disk=pc.Win32_DiskDrive()
disk_len=len(disk)
if disk_len>1:
for i in range(0, disk_len): # 硬盘
dict02["硬盘" + str(i) ]=' '.join(disk[i].Caption.split()) + "容量:" + str(round(int(disk[i].Size) / 1000 / 1000 / 1000)) + "GB"
else:
dict02["硬盘" + str(i)] = ' '.join(disk[0].Caption.split()) + "容量:" + str(round(int(disk[0].Size) / 1000 / 1000 / 1000)) + "GB"
dict02["硬盘1"]="没有第二块硬盘"
dict02["计算机名"] = f'{os.environ.get("COMPUTERNAME")}'
dict02["用户名"] = f'{os.getlogin()}'
return dict02
def sqlconn(text):
conn = pyodbc.connect('driver={sql server};server=192.168.1.180,1433;database=driveinfo;uid=sa;pwd=123456')
#conn = pyodbc.connect('driver={sql server};server=192.168.6.4,1433;database=driveinfo;uid=sa;pwd=123456')
cursor = conn.cursor() # 获取游标
dict01=get_network()
dict02=System_spec()
network_sql = "insert into networkinfo(MACID,IP,MASK,GATEWAY,FRIST_DNS,SECOND_DNS,MAC) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}')".format(dict01["MAC地址"].replace(":",""),dict01["IP地址"], dict01["子网掩码"], dict01["网关"], dict01["首选DNS服务器"], dict01["备选DNS服务器"],dict01["MAC地址"])
System_sql = "insert into configinfo(MACID,name,username,computername,systemname,CPU,RAM,Graphics0,Graphics1,Harddisk0,Harddisk1) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}')".format(dict01["MAC地址"].replace(":",""),text, dict02["用户名"], dict02["计算机名"],dict02["操作系统"],dict02["CPU"], dict02["内存"],dict02["显卡0"],dict02["显卡1"],dict02["硬盘0"],dict02["硬盘1"])
#sql = "insert into networkinfo(MACID,IP,MASK,GATEWAY,FRIST_DNS,SECOND_DNS,MAC) values('8C164591B40A','192.168.6.2','255.255.254.0','192.168.6.1','192.168.6.213','114.114.114.114','8C:16:45:91:B4:0A')"
cursor.execute(network_sql)
conn.commit()
select_sql = "Select * from networkinfo"
cursor.execute(select_sql)
result = cursor.fetchall() # fetchall()返回多行记录
cursor.execute(System_sql)
conn.commit()
conn.close()
if __name__ == "__main__":
dict001=System_spec()
dict002=get_network()
print(dict001)
print(dict002)