Ejemplo n.º 1
0
    def handle(self):
        def md5_file(filename):
                if os.path.isfile(filename):
                        m = md5()
                        with open(filename, 'rb') as f:
                                m.update(f.read())
                        return m.hexdigest()
                else:
                        return None
        def receive_data(sock):
		return_data ='' 
                while True:
                        data = sock.recv(4096)
			#print '\033[36;1m%s\033[0m' %data
			if data == 'EndOfDataConfirmationMark':
				print 'file transfer done==='
				break
                        return_data += data
		#print '\033[34;1m=====\033[0m',return_data
                return return_data
        # self.request is the TCP socket connected to the client
	print "Got a connection from",self.client_address[0]
	RSA_signal =self.request.recv(1024).strip()
	random_num,dcrypted_data_from_server = str(random.random()),None
	#print random_num, dcrypted_data_from_server
	if RSA_signal == 'RSA_KEY_Virification':
		encrypted_data = key_gen.RSA_key.encrypt_RSA(key_gen.public_file,random_num)
		print encrypted_data
		self.request.send(encrypted_data)
		dcrypted_data_from_server = self.request.recv(1024)	
	if random_num != dcrypted_data_from_server:
		print random_num, dcrypted_data_from_server
		self.request.send('WrongRSAkey')
		self.finish()
	
		"""if self.client_address[0] != server_address:
		err_msg= "\033[31;1mIP %s not allowed to connect this server!\033[0m" % self.client_address[0]
		pickle_data = '', err_msg
		self.request.sendall( pickle.dumps(pickle_data)  )
		print err_msg 
		block_list.append(self.client_address[0])
		if block_list.count(self.client_address[0]) > 5:
			print "Alert::malicious attack from:" , self.client_address[0]
		"""
	else:
		self.request.send('CorrectRSAkey')
		print "RSA virification passed!"
		self.data = self.request.recv(1024).strip()
		if self.data.startswith('CMD_Excution|'):
			cmd= self.data.split('CMD_Excution|')[1]
			cmd_status,result = commands.getstatusoutput(cmd)	
			print 'host:%s \tcmd:%s \tresult:%s' %(self.client_address[0], cmd, cmd_status)	
			self.request.sendall(pickle.dumps( (cmd_status,result) ))
		if self.data.startswith('RUN_Script|'):
			recv_data= self.data
			filename = "%s%s" %(recv_dir,recv_data.split('|')[1].strip())
			print '+++++|receiving file from server:',filename
			md5_key_from_client = self.data.split('|')[2]
			file_content = receive_data(self.request)
			print 'write data into file....'
			with open(filename, 'wb') as f:
				f.write(file_content)
			md5_key= md5_file(filename )
			print '+++++|verfiying md5key---'
			if md5_key == md5_key_from_client:	
				print 'file transfer verification ok' 
				self.request.send('FileTransferComplete')	
				os.system('chmod +rx %s ' % filename)
				#os.chmod(filename,stat.S_IREAD+stat.S_IWOTH+stat.S_IXUSR+stat.S_IRWXO)
				cmd = "nohup  %s > %s.log &" % (filename, filename)
				print '\033[32;1mgoing to run script:\033[0m',cmd
				result = os.system(cmd )
				#print result,'||||+|||'
			else:
				print "file not complete"
				self.request.send('FileTransferNotComplete')	
		if self.data.startswith('SEND_FILE|'):
                        file_info = self.data.split('|')
			md5_key_from_client = file_info[1]
			compress_mark = file_info[4]
			print compress_mark
			source_file = file_info[2].split('/')[-1]
			if file_info[3].endswith('/'):
				dst_filename = '%s%s' %(file_info[3], source_file)
			else:
				dst_filename = '%s/%s' %(file_info[3], source_file)
			print dst_filename
                        print '+++++|receiving file from server:',dst_filename
			try:
                          with open(dst_filename, 'wb') as f:
				self.request.send("FileOk2Send")
                        	file_content = receive_data(self.request)
                        	print 'write data into file....'
                                f.write(file_content)
			except IOError:
				print dst_filename,'not exist'
				self.request.send("FileTransferError:cannot save file to %s" % dst_filename )
				return  "FileTransferError..."	
                        md5_key= md5_file(dst_filename )
                        print '+++++|verfiying md5key---'
                        if md5_key == md5_key_from_client:
                                print 'file transfer verification ok'
                                self.request.send('FileTransferComplete')
				if compress_mark == 'tarAndCompressed':
					temp_tar_file = 'temp/%s.tar' % source_file
					compress.decompress(dst_filename, temp_tar_file)
					compress.untar(temp_tar_file,file_info[3] )
					os.remove(temp_tar_file)
					os.remove(dst_filename)
				elif compress_mark == 'compressed':
					target_path = dst_filename.split('/%s' %source_file)[0]
					print target_path
					target_file = '%s/%s' %(target_path, source_file[:-2])
					compress.decompress(dst_filename, target_file) 	
					os.remove(dst_filename)
                        else:
                                print "file not complete"
                                self.request.send('FileTransferNotComplete')
		if self.data == 'getHardwareInfo':
			import Hardware_Collect_Script
			hardware_data = Hardware_Collect_Script.collectAsset() 
			self.request.sendall(hardware_data )
Ejemplo n.º 2
0
    def handle(self):
        def md5_file(filename):
                if os.path.isfile(filename):
                        m = md5()
                        with open(filename, 'rb') as f:
                                m.update(f.read())
                        return m.hexdigest()
                else:
                        return None
        def receive_data(sock):
            return_data ='' 
            while True:
                data = sock.recv(4096)
                #print '\033[36;1m%s\033[0m' %data
                if data == 'EndOfDataConfirmationMark':
                    print 'file transfer done==='
                    break
                return_data += data
            return return_data

        # self.request is the TCP socket connected to the client
        def RSA_verifyication(sock):
            raw_rsa_data = sock.recv(396)  #fixed key length
            #print '-------->',raw_rsa_data, len(raw_rsa_data)
            try:
                RSA_signal, encrypted_data,random_num_from_client = json.loads(  raw_rsa_data)
            except ValueError:
                print '\033[41;1mWrong data format from %s,close connection!\033[0m' %  self.client_address[0]
                return 0
            if RSA_signal == 'RSA_KEY_Virification':
                #encrypted_data = "iwTgqSzMcNOHauWdXXc+rgfbWt6IUXmdIXUqNUJ2U7FZKISc2WR2yAJrq7ldR3TxQEppWgIo/Ycj\nA5gl0fGDVvAEvV02CKZ3gZEI6fWpiMoy6ucpFFDyVAWUrpiXdUOVKxOsDXGgeOObgvd1jsEQCo4i\ncLCBTWDn0HfyQic+Btm1txXc7Nw9jknUCZx6Y8I+6JaIYjNRLwJ6kSMwpTsfP37lvrQfdUkWu3bX\npV9z3hHOQ6+A8rlK7fmL1zk75TXDCmnrLY88UIv6BL4zPXtim4BCD7PlOvDG296br0VIcvF5uhqr\ntj7zOcbA81P1JBFm1nMJqLv+SB5sit923v05XA==\n"
                try:
                    decrpted_data = key_gen.RSA_key.decrypt_RSA(key_gen.private_file,encrypted_data)
                    #print decrpted_data,'+++++++', random_num_from_client
                except ValueError:
                    decrpted_data = 'Incorrect decryption'
                if decrpted_data != random_num_from_client:
                    return 0
            else:
                return 0
        if RSA_verifyication(self.request) == 0: #didn't pass the RSA virification
            #if self.client_address[0] != server_address:
            err_msg= "\033[31;1mIP %s didn't pass the RSA virification,drop it!\033[0m" % self.client_address[0]
            pickle_data = '', err_msg
            self.request.sendall( pickle.dumps(pickle_data)  )
            print err_msg 
            block_list.append(self.client_address[0])
            if block_list.count(self.client_address[0]) > 5:
                print "Alert::malicious attack from:" , self.client_address[0]
        else:
            self.request.send('RSA_OK')
            #get connect client ip and port.
            client_ip,client_port=self.client_address
            #get client request data_type to do something
            self.data_type = self.request.recv(1024).strip()
            print "{} wrote:",self.client_address[0]
            if self.data_type.startswith('CMD_Excution|'):
                cmd= self.data_type.split('CMD_Excution|')[1]
                cmd_status,result = commands.getstatusoutput(cmd)
                print 'host:%s \tcmd:%s \tresult:%s' %(self.client_address[0], cmd, cmd_status)
                self.request.sendall(pickle.dumps( (cmd_status,result) ))
            elif self.data_type.startswith('RUN_Script|'):
                recv_data= self.data_type
                filename = "%s%s" %(recv_dir,recv_data.split('|')[1].strip())
                print '+++++|receiving file from server:',filename
                md5_key_from_client = self.data_type.split('|')[2]
                file_content = receive_data(self.request)
                print 'write data into file....'
                with open(filename, 'wb') as f:
                    f.write(file_content)
                md5_key= md5_file(filename )
                print '+++++|verfiying md5key---'
                if md5_key == md5_key_from_client:
                    print 'file transfer verification ok' 
                    self.request.send('FileTransferComplete')
                    os.system('chmod +rx %s ' % filename)
                    #os.chmod(filename,stat.S_IREAD+stat.S_IWOTH+stat.S_IXUSR+stat.S_IRWXO)
                    cmd = "nohup  %s > %s.log &" % (filename, filename)
                    print '\033[32;1mgoing to run script:\033[0m',cmd
                    result = os.system(cmd )
                    #print result,'||||+|||'
                else:
                    print "file not complete"
                    self.request.send('FileTransferNotComplete')
            elif self.data_type == "getMonitorStatusData":
                print "going to serialize Monitor_dic"
            elif self.data_type == 'getHardwareInfo':
                import Hardware_Collect_Script
                hardware_data = Hardware_Collect_Script.collectAsset() 
                self.request.sendall(hardware_data )
            elif self.data_type.startswith('MonitorDataRequest'):
                #第一次进行监控项信息的通信,还需要把主机名hostname传送过去?
                #self.request.send('ReadyToSendStatusData')
                
                if is_server:
                    
                    #server deal
                    proxy_flag=self.data_type.split("|")
                    if proxy_flag[1] == 'proxy':
                        #处理代理请求
                        monitor_data=get_monitor_dic_fromDB.get_proxy_monitor_list(ip=client_ip)
                    else:
                        
                        #得到该ip的trunk_servers_id,如果没有数据怎么办,返回monitor_data为0
                        server_ip='10.168.7.101'
                        monitor_data = get_config.get_config_for_host(ip=client_ip)    
                        #monitor_data=get_monitor_dic_fromDB.get_one_host_monitor_dir(client_ip,server_ip)
                        #monitor_data=''时,没有得到该ip的监控项
                        
                        if type(monitor_data) is not str:
                            print '---->',monitor_data
                            print len(json.dumps(monitor_data  ))    
                            self.request.send(  str(len(json.dumps(monitor_data))) )
                            time.sleep(0.5)
                            self.request.send( json.dumps(monitor_data)  )
                        else:
                            print '\033[43;1m------code goes here-------\033[0m', monitor_data
                            self.request.send( json.dumps(monitor_data)  )
                        """while 1:
                        if monitor_data:
                            print '---frist send monitor_data!---'
                            self.request.send(str(len(json.dumps(monitor_data))))
                            self.request.sendall(json.dumps(monitor_data))
                        else:
                            #没有该ip的监控项数据,返回0
                            #self.request.send("no_monitor")
                            self.request.send(json.dumps(monitor_data))
                            print 'ip has not monitor data......'
                        #接收客户端是否接收成功!注意这是阻塞等待接收???
                        self.data_status=self.request.recv(1024)
                        print self.data_status,'*+'*30
                        if self.data_status:
                            #接收成功。
                            break
                        else:
                            pass
                        """
                else:
                    #proxy MonitorDataRequest as S  
                    #如果不存在IP的监控项??client_ip,不能使用 global data_dic
                    while 1:
                        if proxy_monitor_dic.has_key(client_ip):
                            #发送监控项数据大小以及内容
                            monitor_data={}
                            monitor_data[client_ip] = proxy_monitor_dic[client_ip]
                            print monitor_data[client_ip]
                            self.request.send(str(len(json.dumps(monitor_data[client_ip]))))
                            self.request.sendall(json.dumps(monitor_data[client_ip]))
                        else:
                            #没有得到数据时,返回了一个'0'
                            print 'ip has not monitor status' 
                            self.request.send(str(0))
                        #接收客户端是否接收成功!,注意这是阻塞等待接收???
                        self.data_status=self.request.recv(1024)
                        print self.data_status
                        #接收成功。
                        if self.data_status:
                            print self.data_status
                            break
                        else:
                            pass
            #代理trunk_servers as local area network server optrate
            elif self.data_type.startswith('MonitorDataChange'):
                if is_server:
                    #监控数据改变通过tri_sender.py来主动发送,这里不实现
                    pass
                else:
                    #proxy MonitorDataChange as C->S发送连接状态,先以C接收,然后在以S分发
                    self.request.sendall('ReadyToReceiveData')
                    #get_proxy_monitor_dic
                    #data_size=self.request.recv(1024)
                    data_size=self.data_type.split("|")[1]
                    if data_size <=8096:
                        proxy_monitor_new_dic=json.loads(self.request.recv(8096))
                    else:
                        proxy_monitor_new_dic=json.loads(receive_data_by_size(self.request,int(data_size)))
                    #下发这些监控信息,得到监控数据中具有的ip
                    for k in proxy_monitor_new_dic.keys():
                        if k == 'ip':
                            client_monitor_dic=proxy_monitor_new_dic[k]
                            #向本地client端发送改变的监控信息
                            send_data(1,client_ip,9997,client_monitor_dic)
                            
            elif self.data_type.startswith('AssetsDataCollect'):
                self.request.send('ReadyToReceiveData')
                signal_flag = self.data_type.split("|")
                raw_data_from_client_size = int(signal_flag[1])
                if raw_data_from_client_size <= 8096:
                    raw_data_from_client = self.request.recv(8096)
                else:
                    raw_data_from_client = receive_data_by_size(self.request,raw_data_from_client_size)
                print '+++', raw_data_from_client
                assets_data = json.loads( raw_data_from_client )
                if is_server:
                    #处理收集到的资产信息,分为本地和代理发送过来的。
                    #if client_ip.startswith('10.') or client_ip.startswith('192.168.') or client_ip.startswith('172.[16,31].'):
                    assets_dic={}
                    if signal_flag[2] == 'proxy':
                        #代理的资产数据字典的处理,一个代理
                        for proxy_ip in assets_data.keys():
                            if proxy_ip == client_ip:
                                host_list=[]
                                for one_assets_data in assets_data[proxy_ip]:
                                    #首先要获取有几台主机信息
                                    client_hostname=one_assets_data['hostname']
                                    host_list.append(client_hostname)
                                for host in set(host_list):
                                    assets_dic[client_hostname]={}
                                
                                print assets_dic
                                
                                for one_assets_data in assets_data[proxy_ip]:
                                    #这里几条监控数据可能是同一台主机发送的,如何整理
                                    client_hostname=one_assets_data['hostname']
                                    #assets_dic.setdefault(client_hostname:{})
                                    #assets_dic={client_hostname:{}}
                                    for name,service_status in one_assets_data.items():
                                        if type(service_status) is dict:
                                            #说明name!=hostname的其他监控项,添加最后的监控时间
                                            service_status['last_check']=time.time()
                                            assets_dic[client_hostname][name]=service_status
                                            print assets_dic
                                    #如果hostname相同怎么吧???会重复覆盖,放到里面并??
                            print assets_dic
                            
                        #最后处理资产收集到的字典设置
                    else:
                        #本地的,只是一个资产数据,进行数据基本处理
                        #{u'cpu_info': {'cpu_basicinfo': [{'cpu_id': u'CPU0', 'NumberOfCores': 2}]}, 'hostname': u'5245','mem_info':{}}
                        #旧的:{'load': {'status': 1}, 'hostname': 'localhost'}
                        client_hostname = assets_data['hostname']
                        assets_dic={client_hostname:{}}
                        #tmp={}
                        for name,service_status in assets_data.items():
                            #if type(service_status) is dict:
                            #说明name!=hostname的其他监控项,添加最后的监控时间
                            if name !='hostname':                                
                                service_status['last_check']=time.time()
                                #tmp[name]=service_status
                                assets_dic[client_hostname][name]=service_status
                        #assets_dic[client_hostname] = tmp
                        print assets_dic
                        print "------get conn from %s------\n" %client_hostname
                    # push status data into JSON file
                    '''
                    if client_hostname == 'localhost':
                        #print redis_connector.r.keys()
                        redis_connector.r['TriAquae_monitor_status'] = json.dumps(assets_dic)
                        #redis_connector.r.save()
                        print 'status inserted into JSON file'
                    '''
                else:
                    #proxy deal 向服务端发送数据HOST=?,PORT=? is_client=0
                    #把接收client端的数据放到redis中
                    timestemp=time.time()
                    redis_connector.r[timestemp]=json.dumps(assets_data)
                    '''
                    S_HOST='10.168.7.40'
                    S_PORT=9998
                    #何时向服务器端发送资产信息??接到本地资产信息时发送,所有资产信息都收集到
                    #在之后更新的资产信息时,如何发送??
                    client_hostname = client_ip
                    proxy_assets_dic={}
                    assets_dic={}
                    assets_dic[client_hostname]=assets_data
                    proxy_assets_dic['HOST'].append(assets_dic)
                    if client_hostname == HOST:
                        send_data(0,S_HOST,S_PORT,proxy_assets_dic)
                    '''
                    '''
                    for name,service_status in assets_data.items():
                        #print name,service_status
                        if type(service_status) is dict:
                            service_status['last_check'] = time.time()
                        assets_dic[client_hostname][name] =  service_status
                    proxy_assets_dic[HOST].append(assets_dic)
                    '''

            elif self.data_type == 'CollectStatusIntoJsonFile':
                print 'CollectStatusIntoJsonFile'
            elif self.data_type == 'datasizetest':
                print 'test'
                with open('tri_server.py') as f:
                    #data=f.read()
                    data=f.readlines()
                print type(data)
                self.request.sendall('1024')
                #time.sleep(0.5)
                self.request.send('OK')
Ejemplo n.º 3
0
    def handle(self):
        def md5_file(filename):
            if os.path.isfile(filename):
                m = md5()
                with open(filename, 'rb') as f:
                    m.update(f.read())
                return m.hexdigest()
            else:
                return None

        def receive_data(sock):
            return_data = ''
            while True:
                data = sock.recv(4096)
                #print '\033[36;1m%s\033[0m' %data
                if data == 'EndOfDataConfirmationMark':
                    print 'file transfer done==='
                    break
                return_data += data
            return return_data

        # self.request is the TCP socket connected to the client
        def RSA_verifyication(sock):
            raw_rsa_data = sock.recv(396)  #fixed key length
            #print '-------->',raw_rsa_data, len(raw_rsa_data)
            try:
                RSA_signal, encrypted_data, random_num_from_client = json.loads(
                    raw_rsa_data)
            except ValueError:
                print '\033[41;1mWrong data format from %s,close connection!\033[0m' % self.client_address[
                    0]
                return 0
            if RSA_signal == 'RSA_KEY_Virification':
                #encrypted_data = "iwTgqSzMcNOHauWdXXc+rgfbWt6IUXmdIXUqNUJ2U7FZKISc2WR2yAJrq7ldR3TxQEppWgIo/Ycj\nA5gl0fGDVvAEvV02CKZ3gZEI6fWpiMoy6ucpFFDyVAWUrpiXdUOVKxOsDXGgeOObgvd1jsEQCo4i\ncLCBTWDn0HfyQic+Btm1txXc7Nw9jknUCZx6Y8I+6JaIYjNRLwJ6kSMwpTsfP37lvrQfdUkWu3bX\npV9z3hHOQ6+A8rlK7fmL1zk75TXDCmnrLY88UIv6BL4zPXtim4BCD7PlOvDG296br0VIcvF5uhqr\ntj7zOcbA81P1JBFm1nMJqLv+SB5sit923v05XA==\n"
                try:
                    decrpted_data = key_gen.RSA_key.decrypt_RSA(
                        key_gen.private_file, encrypted_data)
                    #print decrpted_data,'+++++++', random_num_from_client
                except ValueError:
                    decrpted_data = 'Incorrect decryption'
                if decrpted_data != random_num_from_client:
                    return 0
            else:
                return 0

        if RSA_verifyication(
                self.request) == 0:  #didn't pass the RSA virification
            #if self.client_address[0] != server_address:
            err_msg = "\033[31;1mIP %s didn't pass the RSA virification,drop it!\033[0m" % self.client_address[
                0]
            pickle_data = '', err_msg
            self.request.sendall(pickle.dumps(pickle_data))
            print err_msg
            block_list.append(self.client_address[0])
            if block_list.count(self.client_address[0]) > 5:
                print "Alert::malicious attack from:", self.client_address[0]
        else:
            self.request.send('RSA_OK')
            #get connect client ip and port.
            client_ip, client_port = self.client_address
            #get client request data_type to do something
            self.data_type = self.request.recv(1024).strip()
            print "{} wrote:", self.client_address[0]
            if self.data_type.startswith('CMD_Excution|'):
                cmd = self.data_type.split('CMD_Excution|')[1]
                cmd_status, result = commands.getstatusoutput(cmd)
                print 'host:%s \tcmd:%s \tresult:%s' % (self.client_address[0],
                                                        cmd, cmd_status)
                self.request.sendall(pickle.dumps((cmd_status, result)))
            elif self.data_type.startswith('RUN_Script|'):
                recv_data = self.data_type
                filename = "%s%s" % (recv_dir, recv_data.split('|')[1].strip())
                print '+++++|receiving file from server:', filename
                md5_key_from_client = self.data_type.split('|')[2]
                file_content = receive_data(self.request)
                print 'write data into file....'
                with open(filename, 'wb') as f:
                    f.write(file_content)
                md5_key = md5_file(filename)
                print '+++++|verfiying md5key---'
                if md5_key == md5_key_from_client:
                    print 'file transfer verification ok'
                    self.request.send('FileTransferComplete')
                    os.system('chmod +rx %s ' % filename)
                    #os.chmod(filename,stat.S_IREAD+stat.S_IWOTH+stat.S_IXUSR+stat.S_IRWXO)
                    cmd = "nohup  %s > %s.log &" % (filename, filename)
                    print '\033[32;1mgoing to run script:\033[0m', cmd
                    result = os.system(cmd)
                    #print result,'||||+|||'
                else:
                    print "file not complete"
                    self.request.send('FileTransferNotComplete')
            elif self.data_type == "getMonitorStatusData":
                print "going to serialize Monitor_dic"
            elif self.data_type == 'getHardwareInfo':
                import Hardware_Collect_Script
                hardware_data = Hardware_Collect_Script.collectAsset()
                self.request.sendall(hardware_data)
            elif self.data_type.startswith('MonitorDataRequest'):
                #第一次进行监控项信息的通信,还需要把主机名hostname传送过去?
                #self.request.send('ReadyToSendStatusData')
                if is_server:
                    #server deal
                    proxy_flag = self.data_type.split("|")
                    if proxy_flag[1] == 'proxy':
                        #处理代理请求
                        monitor_data = get_monitor_dic_fromDB.get_proxy_monitor_list(
                            ip=client_ip)
                    else:
                        #新版:server与proxy server同时使用此代码
                        #得到该ip的trunk_servers_id,如果没有数据怎么办,返回monitor_data为0
                        monitor_data = get_config.get_config_for_host(
                            ip=client_ip)
                        #server_ip='10.168.7.101'
                        #monitor_data=get_monitor_dic_fromDB.get_one_host_monitor_dir(client_ip,server_ip)
                    #monitor_data=''时,没有得到该ip的监控项
                    print '---->', monitor_data
                    print len(json.dumps(monitor_data))
                    self.request.send(str(len(json.dumps(monitor_data))))
                    if type(monitor_data) is not str:
                        time.sleep(0.5)
                        self.request.send(json.dumps(monitor_data))
                    else:
                        pass

                else:
                    #注意新版中proxy与server功能一致,下面没有用了。
                    #proxy MonitorDataRequest as S
                    #如果不存在IP的监控项??client_ip,不能使用 global data_dic
                    while 1:
                        if proxy_monitor_dic.has_key(client_ip):
                            #发送监控项数据大小以及内容
                            monitor_data = {}
                            monitor_data[client_ip] = proxy_monitor_dic[
                                client_ip]
                            print monitor_data[client_ip]
                            self.request.send(
                                str(len(json.dumps(monitor_data[client_ip]))))
                            self.request.sendall(
                                json.dumps(monitor_data[client_ip]))
                        else:
                            #没有得到数据时,返回了一个'0'
                            print 'ip has not monitor status'
                            self.request.send(str(0))
                        #接收客户端是否接收成功!,注意这是阻塞等待接收???
                        self.data_status = self.request.recv(1024)
                        print self.data_status
                        #接收成功。
                        if self.data_status:
                            print self.data_status
                            break
                        else:
                            pass
            #代理trunk_servers as local area network server optrate
            #新版也通过tri_sender来判断监控项是否改变
            elif self.data_type.startswith('MonitorDataChange'):
                if is_server:
                    #监控数据改变通过tri_sender.py来主动发送,这里不实现
                    pass
                else:
                    #proxy MonitorDataChange as C->S发送连接状态,先以C接收,然后在以S分发
                    self.request.sendall('ReadyToReceiveData')
                    #get_proxy_monitor_dic
                    #data_size=self.request.recv(1024)
                    data_size = self.data_type.split("|")[1]
                    if data_size <= 4096:
                        proxy_monitor_new_dic = json.loads(
                            self.request.recv(4096))
                    else:
                        proxy_monitor_new_dic = json.loads(
                            receive_data_by_size(self.request, int(data_size)))
                    #下发这些监控信息,得到监控数据中具有的ip
                    for k in proxy_monitor_new_dic.keys():
                        if k == 'ip':
                            client_monitor_dic = proxy_monitor_new_dic[k]
                            #向本地client端发送改变的监控信息
                            send_data(1, client_ip, 9997, client_monitor_dic)

            elif self.data_type.startswith('MonitorResultDataCollect'):
                self.request.send('ReadyToReceiveData')
                signal_flag = self.data_type.split("|")
                raw_data_from_client_size = int(signal_flag[1])
                print '\033[32;1m result data size is %s\033[0m' % signal_flag[
                    1]
                if raw_data_from_client_size <= 4096:
                    raw_data_from_client = self.request.recv(4096)
                else:
                    raw_data_from_client = receive_data_by_size(
                        self.request, raw_data_from_client_size)
                #print '+++', raw_data_from_client,'+++'
                monitor_result_data = json.loads(raw_data_from_client)
                if is_server:
                    #处理收集到的资产信息,分为本地和代理发送过来的。
                    #if client_ip.startswith('10.') or client_ip.startswith('192.168.') or client_ip.startswith('172.[16,31].'):
                    assets_dic = {}
                    if signal_flag[2] == 'proxy':
                        #代理的资产数据字典的处理,一个代理
                        for proxy_ip in monitor_result_data.keys():
                            if proxy_ip == client_ip:
                                host_list = []
                                for one_monitor_result_data in monitor_result_data[
                                        proxy_ip]:
                                    #首先要获取有几台主机信息
                                    client_hostname = one_monitor_result_data[
                                        'hostname']
                                    host_list.append(client_hostname)
                                for host in set(host_list):
                                    assets_dic[client_hostname] = {}

                                print assets_dic

                                for one_monitor_result_data in monitor_result_data[
                                        proxy_ip]:
                                    #这里几条监控数据可能是同一台主机发送的,如何整理
                                    client_hostname = one_monitor_result_data[
                                        'hostname']
                                    #assets_dic.setdefault(client_hostname:{})
                                    #assets_dic={client_hostname:{}}
                                    for name, service_status in one_monitor_result_data.items(
                                    ):
                                        if type(service_status) is dict:
                                            #说明name!=hostname的其他监控项,添加最后的监控时间
                                            service_status[
                                                'last_check'] = time.time()
                                            assets_dic[client_hostname][
                                                name] = service_status
                                            print assets_dic
                                    #如果hostname相同怎么吧???会重复覆盖,放到里面并??
                            print assets_dic
                        #最后处理资产收集到的字典设置
                    else:
                        #本地的,只是一个监控状态数据,进行数据基本处理,并将状态信息放到初始字典中

                        for k in monitor_result_data['result_values'].keys():
                            try:
                                monitor_result_data['result_values'][k][
                                    'last_check'] = time.time()
                                monitor_result_dic[monitor_result_data[
                                    'hostname']]['result_values'][
                                        k] = monitor_result_data[
                                            'result_values'][k]

                            except TypeError, err:
                                pprint(err, 'err')
                                continue
                        #monitor_result_dic[monitor_result_data['hostname']]=monitor_result_data
                        #print '...',monitor_result_data,'...'
                        print "\033[31;1m------get conn from %s------\033[0m\n" % monitor_result_data[
                            'hostname']
                    print '\033[34;1mmonitor data result is\033[0m'
                    for k, v in monitor_result_dic.items():
                        print '\033[34;1mone_result:\033[0m', k, v
                else:
                    #proxy deal 向服务端发送数据HOST=?,PORT=? is_client=0
                    #把接收client端的数据放到redis中
                    timestemp = time.time()
                    redis_connector.r[timestemp] = json.dumps(
                        monitor_result_data)

            elif self.data_type.startswith('StatusDataIntoRedis'):
                timestemp = time.time()
                redis_connector.r['monitor_status_data'] = json.dumps(
                    monitor_result_dic)
                print '\033[35;1mMonitor Result Data Push Into Redis....\033[0m'
                self.request.send("StatusDataIntoRedis_OK")
                print 'OK'
Ejemplo n.º 4
0
    def handle(self):
        def md5_file(filename):
                if os.path.isfile(filename):
                        m = md5()
                        with open(filename, 'rb') as f:
                                m.update(f.read())
                        return m.hexdigest()
                else:
                        return None
        def receive_data(sock):
		return_data ='' 
                while True:
                        data = sock.recv(4096)
			#print '\033[36;1m%s\033[0m' %data
			if data == 'EndOfDataConfirmationMark':
				print 'file transfer done==='
				break
                        return_data += data
		#print '\033[34;1m=====\033[0m',return_data
                return return_data
        # self.request is the TCP socket connected to the client
	def RSA_verifyication(sock):
		RSA_signal, encrypted_data,random_num_from_client = json.loads(sock.recv(1024))
		#print RSA_signal, encrypted_data,random_num_from_client
		if RSA_signal == 'RSA_KEY_Virification':
			#encrypted_data = "iwTgqSzMcNOHauWdXXc+rgfbWt6IUXmdIXUqNUJ2U7FZKISc2WR2yAJrq7ldR3TxQEppWgIo/Ycj\nA5gl0fGDVvAEvV02CKZ3gZEI6fWpiMoy6ucpFFDyVAWUrpiXdUOVKxOsDXGgeOObgvd1jsEQCo4i\ncLCBTWDn0HfyQic+Btm1txXc7Nw9jknUCZx6Y8I+6JaIYjNRLwJ6kSMwpTsfP37lvrQfdUkWu3bX\npV9z3hHOQ6+A8rlK7fmL1zk75TXDCmnrLY88UIv6BL4zPXtim4BCD7PlOvDG296br0VIcvF5uhqr\ntj7zOcbA81P1JBFm1nMJqLv+SB5sit923v05XA==\n"
			
			try:
				decrpted_data = key_gen.RSA_key.decrypt_RSA(key_gen.private_file,encrypted_data)
				#print decrpted_data,'+++++++', random_num_from_client
			except ValueError:
				decrpted_data = 'Incorrect decryption'
			if decrpted_data != random_num_from_client:
				return 0
		else:
			return 0
		#print RSA_singal, encrypted_data 	
	if RSA_verifyication(self.request) == 0: #didn't pass the RSA virification

		#if self.client_address[0] != server_address:
		err_msg= "\033[31;1mIP %s didn't pass the RSA virification,drop it!\033[0m" % self.client_address[0]
		pickle_data = '', err_msg
		self.request.sendall( pickle.dumps(pickle_data)  )
		print err_msg 
		block_list.append(self.client_address[0])
		if block_list.count(self.client_address[0]) > 5:
			print "Alert::malicious attack from:" , self.client_address[0]
	else:
		self.data = self.request.recv(1024).strip()
		print "{} wrote:",self.client_address[0]
		if self.data.startswith('CMD_Excution|'):
			cmd= self.data.split('CMD_Excution|')[1]
			cmd_status,result = commands.getstatusoutput(cmd)	
			print 'host:%s \tcmd:%s \tresult:%s' %(self.client_address[0], cmd, cmd_status)	
			self.request.sendall(pickle.dumps( (cmd_status,result) ))
		elif self.data.startswith('RUN_Script|'):
			recv_data= self.data
			filename = "%s%s" %(recv_dir,recv_data.split('|')[1].strip())
			print '+++++|receiving file from server:',filename
			md5_key_from_client = self.data.split('|')[2]
			file_content = receive_data(self.request)
			print 'write data into file....'
			with open(filename, 'wb') as f:
				f.write(file_content)
			md5_key= md5_file(filename )
			print '+++++|verfiying md5key---'
			if md5_key == md5_key_from_client:	
				print 'file transfer verification ok' 
				self.request.send('FileTransferComplete')	
				os.system('chmod +rx %s ' % filename)
				#os.chmod(filename,stat.S_IREAD+stat.S_IWOTH+stat.S_IXUSR+stat.S_IRWXO)
				cmd = "nohup  %s > %s.log &" % (filename, filename)
				print '\033[32;1mgoing to run script:\033[0m',cmd
				result = os.system(cmd )
				#print result,'||||+|||'
			else:
				print "file not complete"
				self.request.send('FileTransferNotComplete')	
		elif self.data == "getMonitorStatusData":
			print "going to serialize Monitor_dic"
			
		elif self.data == 'getHardwareInfo':
			import Hardware_Collect_Script
			hardware_data = Hardware_Collect_Script.collectAsset() 
			self.request.sendall(hardware_data )
		elif self.data == 'ReportMonitorStatus':
			self.request.send('ReadyToReceiveStatusData')	
			status_data = json.loads(self.request.recv(8096) )	
			client_hostname =  status_data['hostname']
			for name,service_status in status_data.items():
				#print name,service_status
				if type(service_status) is dict:service_status['last_check'] = time.time()
				monitor_dic[ client_hostname][name] =  service_status
			print "************get conn from %s------\n" %client_hostname
			# push status data into JSON file
			if client_hostname == 'localhost':
				redis_connector.r['TriAquae_monitor_status'] = json.dumps(monitor_dic)
				redis_connector.r.save()

				print 'status inserted into JSON file'
				
		elif self.data == 'CollectStatusIntoJsonFile':
			print 'CollectStatusIntoJsonFile'