好友
阅读权限 10
听众
最后登录 1970-1-1
幻幻乐
发表于 2019-10-30 09:28
本帖最后由 幻幻乐 于 2019-10-30 09:31 编辑
各位DX好
最近打算使用python 里面的wmi来获取远程计算机的一些硬件信息,如果是在程序直接使用wmi.WMI(computer,user,password)这个是没有问题,代码及效果图如下:(由于刚接触python且刚开始写,所以代码肯定有不足的地方,或表达不清楚的地方,请各位大佬见谅)
1-1,(在这个效果图上面的第一个按钮是创建对应文件名的txt,下一个确认按钮是连接到远程电脑并记录相关信息)
from tkinter import *
import wmi
import socket
import uuid
def submit_user():
f= open("//192.168.98.204/temp/tongji/" + u2.get() + ".txt",'a')
print("部门:",u1.get()," ","姓名:",u2.get(),file=f)
def submit_remote():
c= wmi.WMI(computer=u3.get(),user=u4.get(),password=u5.get())
f= open("//192.168.98.204/temp/tongji/" + u2.get() + ".txt",'a')
for sys in c.Win32_OperatingSystem():
# 操作系统简要信息
print("---------------操作系统简要信息---------------",file=f)
print("操作系统:%s" % sys.Caption,"版本:%s" % sys.BuildNumber,sys.OSArchitecture,file=f)
print("计算机名:%s" %sys.CSName,"\n",file=f)
# 主板信息
print("---------------主板信息---------------",file=f)
for board in c.Win32_BaseBoard():
# print("main board id:", board.SerialNumber.strip())
print("制造厂商:",board.Manufacturer,file=f)
print("型号:",board.Product,"\n",file=f)
# CPU信息
print("---------------CPU信息---------------",file=f)
for processor in c.Win32_Processor():
print ("CPU: %s" % processor.Name.strip(),"\n",file=f)
# 内存信息
print("---------------内存信息---------------",file=f)
totalMemSize=0
for memModule in c.Win32_PhysicalMemory():
totalMemSize+=int(memModule.Capacity)
print ("内存: %.2fMB"%(totalMemSize/1048576),"\n",file=f)
# 硬盘信息
print("---------------硬盘信息---------------",file=f)
for disk_size in c.Win32_LogicalDisk():
print(disk_size.Caption, "磁盘大小:%0.1fGB"% ((int(disk_size.size) / 1048576) / 1024),"\n",file=f)
# 显卡信息
print("---------------显卡信息---------------",file=f)
for video in c.Win32_VideoController():
print("显卡名称:",video.Description,"\n",file=f)
# 网络信息
print("---------------网络信息---------------",file=f)
for ipmac in c.Win32_NetworkAdapterConfiguration():
print("网卡名称:",ipmac.Caption,"\n",file=f)
print("IP地址:%s",ipmac.IPAddress,"MAC地址:%s",ipmac.MACAddress,"\n",file=f)
# 显示器信息
print("---------------显示器信息---------------",file=f)
formonitors in c.Win32_DesktopMonitor():
print("显示器名称:", monitors.name, file=f)
root = Tk()
root.title("等待信息录入")
frame = Frame(root)
frame.pack(padx=8, pady=8, ipadx=4)
# #绑定对象到Entry
lab3 = Label(frame, text="计算机名:")
lab3.grid(row=3, column=0, padx=5, pady=5,sticky=W)
u3= StringVar()
ent3 = Entry(frame, textvariable=u3)
ent3.grid(row=3, column=1, sticky='ew',columnspan=2)
lab4 = Label(frame, text="账号:")
lab4.grid(row=4, column=0, padx=5, pady=5,sticky=W)
u4 = StringVar()
ent4 = Entry(frame, textvariable=u4)
ent4.grid(row=4, column=1, sticky='ew',columnspan=2)
lab5 = Label(frame, text="密码:")
lab5.grid(row=5, column=0, padx=5, pady=5,sticky=W)
u5 = StringVar()
ent5 = Entry(frame,textvariable=u5,show="*")
ent5.grid(row=5, column=1, sticky='ew',columnspan=2)
button = Button(frame, text="确定", command=submit_remote, default='active')
button.grid(row=6, column=1)
# #绑定对象到Entry
lab1 = Label(frame, text="部门:")
lab1.grid(row=0, column=0, padx=5, pady=5,sticky=W)
u1 = StringVar()
ent1 = Entry(frame, textvariable=u1)
ent1.grid(row=0, column=1, sticky='ew',columnspan=2)
lab2 = Label(frame, text="姓名:")
lab2.grid(row=1, column=0, padx=5, pady=5,sticky=W)
u2 = StringVar()
ent2 = Entry(frame, textvariable=u2)
ent2.grid(row=1, column=1, sticky='ew',columnspan=2)
button = Button(frame, text="确定", command=submit_user, default='active')
button.grid(row=2, column=1)
button2 = Button(frame, text="退出", command=quit)
button2.grid(row=6, column=2, padx=5,pady=5)
# #以下代码居中显示窗口
root.update_idletasks()
x = (root.winfo_screenwidth() -root.winfo_reqwidth()) / 2
y = (root.winfo_screenheight() -root.winfo_reqheight()) / 2
root.geometry("+%d+%d" % (x, y))
root.mainloop()
----------------------------------------------------------------------------------------------
上面第一版是没有判断远程主机的账号和密码是否正常的,下面这个第二版想加入判断账号和密码是否正确?就是输入账号密码后点测试按钮,判断是否正确,如果正确就可以继续往下输入,否则提示错误,已知账号密码错误或者远程电脑断网的情况会报错,是否能使用try来捕捉?如果捕捉到错误就提示错误,否则继续往下执行?
1-2 (添加测试按钮判断账号密码是否正确)
from tkinter import *
import wmi
import socket
import uuid
# c = wmi.WMI()
class Reg(Frame):
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.lab1 = Label(frame, text="计算机名:")
self.lab1.grid(row=0, column=0, sticky=W)
self.ent1 = Entry(frame)
self.ent1.grid(row=0, column=1, sticky=W)
self.lab2 = Label(frame, text="账户:")
self.lab2.grid(row=1, column=0, sticky=W)
self.ent2 = Entry(frame)
self.ent2.grid(row=1, column=1, sticky=W)
self.lab3 = Label(frame,text="密码:")
self.lab3.grid(row=2, column=0, sticky=W)
self.ent3 = Entry(frame, show="*")
self.ent3.grid(row=2, column=1, sticky=W)
self.lab4 = Label(frame, text="部门:")
self.lab4.grid(row=3, column=0, sticky=W)
self.ent4 = Entry(frame)
self.ent4.grid(row=3, column=1, sticky=W)
self.lab5 = Label(frame, text="使用者:")
self.lab5.grid(row=4, column=0, sticky=W)
self.ent5 = Entry(frame)
self.ent5.grid(row=4, column=1, sticky=W)
self.button = Button(frame, text="测试",command=self.Submit_connect)
self.button.grid(row=2, column=3, sticky=E)
self.button = Button(frame, text="登录",command=self.Submit_define)
self.button.grid(row=3, column=3, sticky=E)
self.lab6 = Label(frame, text="")
self.lab6.grid(row=6, column=1, sticky=W)
self.button2 = Button(frame, text="退出",command=frame.quit)
self.button2.grid(row=4, column=3, sticky=E)
def Submit_connect(self):
c = wmi.WMI(computer=self.ent1.get(), user=self.ent2.get(),password=self.ent3.get())
def Submit_define(self):
computername = self.ent1.get()
username = self.ent2.get()
psw = self.ent3.get()
department = self.ent4.get()
user1 = self.ent5.get()
if (username == 'administrator' and psw == 'simsme310') or (username =='administrator' and psw == 'gsdupl') or (username == 'mis' and psw =='simsme310'):
# if username == 'mis' and psw == 'simsme310':
c = wmi.WMI(computer=computername, user=username, password=psw)
f = open("//192.168.98.204/temp/tongji/" + user1 +".txt", 'a')
print("部门:", department, " ", "姓名:",user1, file=f)
for sys in c.Win32_OperatingSystem():
# 操作系统简要信息
print("---------------操作系统简要信息---------------", file=f)
print("操作系统: %s" % sys.Caption, "版本:%s"% sys.BuildNumber, sys.OSArchitecture, file=f)
print("计算机名: %s" % sys.CSName, "\n", file=f)
# 主板信息
print("---------------主板信息---------------", file=f)
for board inc.Win32_BaseBoard():
# print("main boardid:", board.SerialNumber.strip())
print("制造厂商:", board.Manufacturer, file=f)
print("型号:", board.Product, "\n", file=f)
# CPU信息
print("---------------CPU信息---------------", file=f)
for processor inc.Win32_Processor():
print("CPU: %s" %processor.Name.strip(), "\n", file=f)
# 内存信息
print("---------------内存信息---------------", file=f)
totalMemSize = 0
for memModule inc.Win32_PhysicalMemory():
totalMemSize +=int(memModule.Capacity)
print("内存: %.2fMB" % (totalMemSize / 1048576), "\n", file=f)
# 硬盘信息
print("---------------硬盘信息---------------", file=f)
for disk_size inc.Win32_LogicalDisk():
print(disk_size.Caption,"磁盘大小:%0.1fGB" % ((int(disk_size.size) /1048576) / 1024), "\n", file=f)
# 显卡信息
print("---------------显卡信息---------------", file=f)
for video inc.Win32_VideoController():
print("显卡名称:", video.Description, "\n", file=f)
# 网络信息
print("---------------网络信息---------------", file=f)
for ipmac inc.Win32_NetworkAdapterConfiguration():
print("网卡名称:", ipmac.Caption, "\n", file=f)
print("IP地址:%s", ipmac.IPAddress, "MAC地址:%s",ipmac.MACAddress, "\n", file=f)
# 显示器信息
print("---------------显示器信息---------------", file=f)
for monitors inc.Win32_DesktopMonitor():
print("显示器名称:", monitors.name, file=f)
self.lab6["text"] ="成功获取硬件信息"
elif username == '' and psw == '':
c = wmi.WMI()
f = open("//192.168.98.204/temp/tongji/" + user1 +".txt", 'a')
print("部门:", department, " ", "姓名:",user1, file=f)
for sys in c.Win32_OperatingSystem():
# 操作系统简要信息
print("---------------操作系统简要信息---------------", file=f)
# for sys in c.Win32_OperatingSystem():
# 创建文档文件
# f = open("//192.168.98.204/temp/tongji/"+ str(sys.CSName) + ".txt", 'a')
print("操作系统: %s" % sys.Caption, "版本:%s"% sys.BuildNumber, sys.OSArchitecture, file=f)
print("计算机名: %s" % sys.CSName, "\n", file=f)
# 主板信息
print("---------------主板信息---------------", file=f)
for board inc.Win32_BaseBoard():
# print("main boardid:", board.SerialNumber.strip())
print("制造厂商:", board.Manufacturer, file=f)
print("型号:", board.Product, "\n", file=f)
# CPU信息
print("---------------CPU信息---------------", file=f)
for processor inc.Win32_Processor():
print("CPU: %s" %processor.Name.strip(), "\n", file=f)
# 内存信息
print("---------------内存信息---------------", file=f)
totalMemSize = 0
for memModule inc.Win32_PhysicalMemory():
totalMemSize += int(memModule.Capacity)
print("内存: %.2fMB" % (totalMemSize / 1048576), "\n", file=f)
# 硬盘信息
print("---------------硬盘信息---------------", file=f)
for disk_size inc.Win32_LogicalDisk():
print(disk_size.Caption,"磁盘大小:%0.1fGB" % ((int(disk_size.size) /1048576) / 1024), "\n", file=f)
# 显卡信息
print("---------------显卡信息---------------", file=f)
for video inc.Win32_VideoController():
print("显卡名称:", video.Description, "\n", file=f)
# 网络信息
print("---------------网络信息---------------", file=f)
for ipmac in c.Win32_NetworkAdapterConfiguration():
print("网卡名称:", ipmac.Caption, "\n", file=f)
print("IP地址:%s", ipmac.IPAddress, "MAC地址:%s",ipmac.MACAddress, "\n", file=f)
# 显示器信息
print("---------------显示器信息---------------", file=f)
for monitors inc.Win32_DesktopMonitor():
print("显示器名称:", monitors.name, file=f)
self.lab6["text"] ="成功获取硬件信息"
else:
self.lab6["text"] = "用户名或密码错误!"
# self.ent1.delete(0, len(computername))
# self.ent2.delete(0, len(username))
# self.ent3.delete(0, len(psw))
# self.ent4.delete(0, len(department))
# self.ent5.delete(0, len(user1))
root = Tk()
root.title("用户登录")
app = Reg(root)
root.mainloop()
效果图1
效果图2