1. 程式人生 > >python3批量統計用戶電腦配置

python3批量統計用戶電腦配置

nbsp 服務 self mod not in 數據 fig grep 獲取

最近領導想統計一下用戶電腦配置信息。好幾百人難道讓我一個一個的去弄嗎?

想想還是寫個程序接收一下吧。

客戶端

# -*- coding: utf-8 -*-
#author:Guoyabin
import socket,psutil,os,sys

class wininfo():
	def ip_info(self):
		hostname=socket.gethostname() #獲取主機名
		ipconfig=psutil.net_if_addrs().items() #所有網卡信息
		try:
			s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
			s.connect((‘114.114.114.114‘, 80))
			addre = s.getsockname()[0] #獲取ip地址
		finally:
			s.close() #關閉socket連接
		for k,v in ipconfig:
			try:
				if addre in v[1].address:
					mac=v[0].address #獲取mac地址
					‘‘‘
					gateway=addre.split(‘.‘)[0]+‘.‘+addre.split(‘.‘)[1]+‘.‘+addre.split(‘.‘)[2]+‘.1‘ #獲取網關
					if ‘10.8.70‘ in addre: #特殊網關
						gateway=‘10.8.70.2‘
					os.system("netsh interface ip set address name=%s source=static addr=%s mask=255.255.255.0 gateway=%s gwmetric=1" % (k,addre,gateway)) #設置網關
					‘‘‘
					os.system("netsh interface ip set dns name=%s source=static addr=114.114.114.114" % k) #設置dns
			except:
				pass
		ip=str(hostname)+‘ ‘+addre+‘ ‘+mac
		return(ip)

	def mem_info(self):
		mem_all=psutil.virtual_memory().total/1024/1024/1024 #獲取內存大小
		mem=round(mem_all) #取最接近整數的值
		return(mem)

	def cpu_info(self):
		try:
			name=os.popen(‘wmic cpu get name‘).readlines()
			cpuname1=name[-4].replace(‘\n‘,‘‘)
			cpuname=cpuname1.replace(‘ ‘,‘‘) #獲取Windows CPU型號
		except:
			cpuname=‘ ‘
		cpucount=psutil.cpu_count() #獲取CPU核心
		cpu=str(cpuname)+‘ ‘+str(cpucount)
		return(cpu)

	def disk_info(self):
		sum=0
		for i in psutil.disk_partitions():
			try:
				sum=sum+psutil.disk_usage(i.mountpoint).total #獲取硬盤大小
			except:
				pass
		disk=round(sum/1024/1024/1024) #取最接近整數的值
		return(disk)

	def getname(self):
		name=input(‘請輸入您的名字:‘)
		return(name)

class linuxinfo(wininfo):
	def getname(self):
		name=‘name‘
		return(name)

	def cpu_info(self):
		cpuname1=os.popen("grep -m1 ‘model name‘ /proc/cpuinfo |awk -F ‘:‘ ‘{print $2}‘").readlines() 
		cpuname2=cpuname1[-1].replace(‘ ‘,‘‘)
		cpuname=cpuname2.replace(‘\n‘,‘‘) #獲取Linux CPU型號
		cpucount=psutil.cpu_count() #獲取CPU核心
		cpu=str(cpuname)+‘ ‘+str(cpucount)
		return(cpu)

	def ip_info(self):
		hostname=socket.gethostname() #獲取主機名
		try:
			s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
			s.connect((‘114.114.114.114‘, 80))
			addre = s.getsockname()[0] #獲取ip地址
		finally:
			s.close() #關閉socket連接
		ipconfig=psutil.net_if_addrs().items() #所有網卡信息
		for k,v in ipconfig:
			if addre in v[0].address:
				mac=v[2].address #獲取mac地址
		ip=str(hostname)+‘ ‘+addre+‘ ‘+mac
		return(ip)

if __name__==‘__main__‘:
	serverip = ‘192.168.8.50‘
	port = 9999
	client=socket.socket()
	client.connect((serverip,port))
	if sys.platform==‘win32‘:
		send=wininfo()
	else:
		send=linuxinfo()
	data = str(send.getname())+‘ ‘+str(send.ip_info())+‘ ‘+str(send.cpu_info())+‘ ‘+str(send.mem_info())+‘ ‘+str(send.disk_info())
	#print(data)
	client.sendall(bytes(data,encoding=‘utf-8‘))
	client.close()

  

服務端,我直接把‘密碼接收器‘程序修改了一下

# -*- coding:utf-8 -*-
#author:Guoyabin
import socketserver
import time

class recvpasswd(socketserver.BaseRequestHandler):
	def handle(self):
		conn=self.request #封裝所有對象請求
		while True:
			date=conn.recv(1024).decode()
			if date==‘exit‘:
				break
			if len(date)==0: #空數據則斷開
				break
			#if ‘=>‘ not in date: #防止惡意數據寫入
			#	break
			print(date)
			self.savepasswd(date)
			
	def savepasswd(self,passwd):
		now=time.strftime("%Y-%m-%d %H:%M:%S")
		errfile=open(‘passwd.txt‘,‘a‘)
		errfile.write(now+‘ ‘+str(passwd)+‘\n‘)
		errfile.close()
		
if __name__==‘__main__‘:
	server=socketserver.ThreadingTCPServer((‘0.0.0.0‘,9999),recvpasswd)
	server.serve_forever()

  

最終服務端看到的結果就是:

技術分享圖片

而且會在服務端同級目錄生成password.txt文檔。和命令提示符裏顯示的一樣

python3批量統計用戶電腦配置