标签:hostname getname logs turn drs 电脑 blog bsp split
最近领导想统计一下用户电脑配置信息。好几百人难道让我一个一个的去弄吗?
想想还是写个程序接收一下吧。
客户端
# -*- 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文档。和命令提示符里显示的一样
标签:hostname getname logs turn drs 电脑 blog bsp split
原文地址:http://www.cnblogs.com/guoyabin/p/7930206.html