Python3 Batch Statistics User PC configuration

Source: Internet
Author: User

Recent leaders want to count the user's computer configuration information. Good hundreds of do you want me to get one?

Think about it or write a program to receive it.

Client

#-*-Coding:utf-8-*-#author: Guoyabinimport socket,psutil,os,sysclass wininfo ():d ef 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 ', ()) 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=sta Tic addr=%s mask=255.255.255.0 gateway=%s gwmetric=1 "% (k,addre,gateway)) #设置网关 ' Os.system (" netsh interface ip set DNS n ame=%s source=static addr=114.114.114.114 "% k) #设置dnsexcept:p ASSIP=STR (hostname) + ' +addre+ ' +macreturn (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 model except: Cpuname= ' Cpucount=psutil.cpu_count () #获取CPU核心cpu =str (cpuname) + ' +str (cpucount) return (CPU) def disk_info (self): Sum=0for i in Psutil.disk_partitions (): Try:sum=sum+psutil.disk_usage (i.mountpoint). Total #获取硬盘大小except:p assdisk= Round (sum/1024/1024/1024) #取最接近整数的值return (disk) def getname (self): name=input (' Please enter your name: ') return (name) class Linuxinfo (wininfo):d ef getname (self): name= ' name ' return (name) def cpu_info (self): Cpuname1=os.popen ("GREP-M1" model Name '/proc/cpuinfo |awk-f ': ' {print $} '). ReadLines () cpuname2=cpuname1[-1].replace (",") cpuname= Cpuname2.replace (' \ n ', ') #获取Linux CPU Model 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 ', ()) Addre = S.getsockname () [0] #获取ip地址finally: s.close () # Close the socket connection 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+ ' +macreturn (IP) if __name__== ' __main__ ': ServerIP = ' 192.168.8.50 ' port = 9999client=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 ()

  

Server, I directly to the ' Password receiver ' program modified a bit

#-*-Coding:utf-8-*-#author: Guoyabinimport socketserverimport timeclass recvpasswd (socketserver. Baserequesthandler):d EF handle (self): conn=self.request #封装所有对象请求while true:date=conn.recv (1024x768). Decode () if date== ' Exit ': Breakif len (date) ==0: #空数据则断开break # if ' + ' in Date: #防止恶意数据写入 #breakprint (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 ()

  

The result that the end-of-service sees is:

The Password.txt document is also generated on the server-side sibling directory. As shown in the command prompt.

Python3 Batch Statistics User PC configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.