首页 > 其他分享 >电脑信息登记代码

电脑信息登记代码

时间:2022-10-13 11:33:35浏览次数:41  
标签:登记 name nic 代码 电脑 item text data self

import wmi
import os
import sys
import requests
import json
from PyQt5.QtCore import QCoreApplication
from PyQt5.QtWidgets import (QWidget, QToolTip,
QPushButton, QApplication, QLabel, QLineEdit, QComboBox, QHBoxLayout, QVBoxLayout,
QMessageBox, QDesktopWidget)
from PyQt5.QtGui import QFont


class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):

QMessageBox.information(self,"使用前须知","本脚的作用是用于上传电脑信息,极限深圳市办公室的电脑(私人电脑,苹果电脑,win7系统的电脑可以忽略掉)")


QToolTip.setFont(QFont('SansSerif', 10))
self.setToolTip('希望大家多多配合哈')

self.lbl = QLabel(self)
self.qle = QLineEdit(self)
self.lbl.setText("请输入您的中文姓名")
self.lbl.adjustSize()

hbox = QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(self.lbl)
hbox.addWidget(self.qle)

# qle.textChanged[str].connect(self.onChanged)

self.combo = QComboBox(self)
self.combo.addItem("是")
self.combo.addItem("否")
self.lbl1 = QLabel(self)
self.lbl1.setText("运行的的这台电脑是否为笔记本")
self.lbl1.adjustSize()
# combo.activated[str].connect(self.onActivated)
hbox1 = QHBoxLayout()
hbox1.addStretch(1)
hbox1.addWidget(self.lbl1)
hbox1.addWidget(self.combo)

btn = QPushButton('确认', self)
btn.setToolTip('输入完成之后按确定')
btn.resize(btn.sizeHint())
btn.clicked.connect(self.buttonClicked)
hbox2 = QHBoxLayout()
hbox2.addStretch(1)
hbox2.addWidget(btn)

vbox = QVBoxLayout()
vbox.addStretch(1)
vbox.addLayout(hbox)
vbox.addLayout(hbox1)
vbox.addLayout(hbox2)
self.setLayout(vbox)
self.resize(200,100)
self.center()
self.setWindowTitle('公司电脑信息登记')
self.show()

def buttonClicked(self):
print(self.qle.text())
print(self.combo.currentText())
s = systeminfo()
s.getnetworkinfo()
if not self.qle.text():
QMessageBox.warning(self, "shaoke", "请输入您的姓名")
else:
response = s.run(self.qle.text(), self.combo.currentText())
if "id" in response:
QMessageBox.information(self, "shaoke", "上传成功~~~~,可以删除脚本了~~~")
QCoreApplication.instance().quit()
else:
QMessageBox.warning(self, "shaoke", "出问题了呢,企业微信上@网管小哥吧~~~")

# sender = self.sender()
# self.statusBar().showMessage(sender.text() + ' was pressed')

def center(self):

qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())


class systeminfo():
def getnetworkinfo(self):
# count = 0
# for nic in w.Win32_NetworkAdapterConfiguration():
# if nic.MACAddress is not None:
# count += 1
# item_data = {}
# item_data['macaddress'] = nic.MACAddress
# item_data['model'] = nic.Caption
# # item_data['name'] = nic.Index
# #
# # if nic.IPAddress is not None:
# # item_data['ipaddress'] = nic.IPAddress[0]
# # item_data['netmask'] = nic.IPSubnet
# # else:
# # item_data['ipaddress'] = ""
# # item_data['netmask'] = ""
# data["nic%s" % count] = item_data
data_internet, data_address=None,None
from psutil import net_if_addrs
data = {}
for k, v in net_if_addrs().items():
for item in v:
address = item[1]
if '-' in address and len(address) == 17:
if k == "WLAN":
data_address = address
elif k == "以太网":
data_internet = address

if data_address is None and data_internet is None:
data={"WLAN": data_address, "以太网": self.getnetinfo()}
else:
data = {"WLAN": data_address, "以太网": data_internet}
return data

def getnetinfo(self):
w = wmi.WMI()
data = {}
count = 0
for nic in w.Win32_NetworkAdapterConfiguration():
if nic.MACAddress is not None:
count += 1
item_data = {}
item_data['macaddress'] = nic.MACAddress
item_data['model'] = nic.Caption
item_data['name'] = nic.Index

if nic.IPAddress is not None:
item_data['ipaddress'] = nic.IPAddress[0]
item_data['netmask'] = nic.IPSubnet
else:
item_data['ipaddress'] = ""
item_data['netmask'] = ""
data["nic%s" % count] = item_data

return str(data)


def run(self, name, isNotbook):

w = wmi.WMI()
hostname= w.Win32_OperatingSystem()[0].CSName
headers = {
"Content-Type": "application/json; charset=UTF-8"
}
url = "http://172.16.8.150:5678/webhook/9c12e2d9-2e97-4ac2-9a53-535683dfb6f9"
#url="http://172.16.8.150:5678/webhook-test/9c12e2d9-2e97-4ac2-9a53-535683dfb6f9"
body = {
"使用者姓名": name,
"主机名": hostname,
"有线网卡MAC": self.getnetworkinfo()["以太网"],
"无线网卡MAC": self.getnetworkinfo()["WLAN"],
"是否笔记本电脑": isNotbook,
"电脑型号": w.Win32_ComputerSystem()[0].Manufacturer + "-" + w.Win32_ComputerSystem()[0].model,
"CPU": w.Win32_Processor()[0].Name,
"内存": format(int(w.Win32_ComputerSystem()[0].TotalPhysicalMemory) / (1024 * 1024 * 1024),'.2f')+"G",
"操作系统": w.Win32_OperatingSystem()[0].Caption
}
rename=self.getname(name, isNotbook)

print(rename)
print(body)
response = requests.post(url, data=json.dumps(body), headers=headers).text
print(response)
command="WMIC computersystem where caption=" + "'" + hostname+ "'" " rename " + "'" + rename + "'"
print(command)
os.system(command)

return response
def getname(self,name,isNotbook):

if isNotbook== "是":
return "LAPTOP-"+self.getpinyin(name).upper()
else:
return "PC-"+self.getpinyin(name).upper()
def getpinyin(self,name):
resp=requests.get("http://www.521php.com/api/pinyin.php?str="+name+"&type=1")
print(resp.text)
return resp.text
# def onChanged(self, text):
# self.lbl.setText(text)
# self.lbl.adjustSize()
#
# def onActivated(self, text):
# self.lbl.setText(text)
# self.lbl.adjustSize()


if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

标签:登记,name,nic,代码,电脑,item,text,data,self
From: https://www.cnblogs.com/huluxia-fun/p/16787616.html

相关文章