def copyLog(self, path, fileNames, isfull = False): #根据通配符得到相应文件列表,如果直接用通配符无法在文件名中+上IP chkPath(r'.\temp') for fileName in fileNames: dirResult = os.popen(r'dir "%s%s"' % (path, fileName)).readlines() if os.path.split(fileName)[0]: temppath = '%s%s\\'%(path, os.path.split(fileName)[0]) else: temppath = path for line in dirResult[5:-2]: dirline = re.split(' +', line[:-1], 3) if len(dirline) > 3 and not dirline[3] in ['.','..']: logFileName = r'%s%s'%(temppath, dirline[3]) logFileInfo = os.popen('dir /-C "%s"'%logFileName).readlines()[5:-2][0] logFileSize = int(re.split(' +', logFileInfo[:-1], 3)[2]) if self.login: fileInfo = '%s_%s'%(USER, self.returnIP) else: fileInfo = self.returnIP if logFileSize < CUTLOG_SIZE or isfull: newFileName = r'.\temp\%s_%s'%(fileInfo, dirline[3]) self.copyLogNames.append('%s_%s'%(fileInfo, dirline[3])) os.popen(r'start /B copy /y "%s%s" ".\temp\%s_%s"'%(temppath, dirline[3], fileInfo, dirline[3])) else: newFileName = r'.\temp\%s_cut_%s'%(fileInfo, dirline[3]) self.copyLogNames.append('%s_cut_%s'%(fileInfo, dirline[3])) logFile = open(logFileName, 'r') logFile.seek(logFileSize - CUTLOG_SIZE) newFile = open(newFileName, 'w') newFile.write(logFile.read()) logFile.close() newFile.close()
def listen_message(self): filesize = 0 downfile = None global PATH for len, cmd, data in self.r_pack: if cmd == 0x0003: if downfile: downfile.write(data[14:]) else: chkPath(r'.\temp') downfile = open(r'.\temp\down.rar', 'wb') downfile.write(data[14:]) filesize += len + 2 elif cmd == 0x0001: self.data = self.readString(data[14:]) if 'open' in self.data.keys() or 'down' in self.data.keys() or 'up' in self.data.keys() or 'delete' in self.data.keys(): if self.data['localpath'] in PATH.keys() and os.path.exists('%s%s'%(PATH[self.data['localpath']], self.data['localpath'])): self.localpath = PATH[self.data['localpath']] os.environ['TEMPPATH'] = self.localpath self.stepList = re.split('\|', self.data['step']) else: self.result.extend([self.returnIP, ' : path is none!\n']) self.search = self.data['localpath'] return else: if not self.data['localpath'] in PATH.keys(): PATH[self.data['localpath']] = '' self.stepList = re.split('\|', self.data['step']) if downfile: downfile.close() log.LOG.info('receive filesize is %s' % filesize) self.operation()
def listen_message(self): global PATH filesize = 0 total = 0 for len, type, data in self.r_pack: if type == 0x0002: #对指令的结果反馈 log.LOG.info(data[14:]) log.LOG.info('='*30) total += 1 elif type == 0x0004: filename_len = unpack('<H',data[14 : 16])[0] #log文件名长度 filename = data[16 : filename_len + 16] if filename in self.file.keys(): self.file[filename].write(data[filename_len + 16 : ]) #补充文件 else: if 'decompression' in OTHER: chkPath(r'.\temp') self.file[filename] = open(r'.\temp\%s.rar'%filename,'wb') self.file[filename].write(data[filename_len + 16 : ]) else: chkPath(PATH['logpath']) self.file[filename] = open(r'%s%s.rar'%(PATH['logpath'], filename),'wb') self.file[filename].write(data[filename_len + 16 : ]) filesize += data[filename_len + 16 : ].__len__() log.LOG.info('receive filesize is %s'%filesize) log.LOG.info('response client total %s'%total) for filename in self.file.keys(): self.file[filename].close() if 'decompression' in OTHER: chkPath(PATH['logpath']) log.LOG.debug(mylib.rar.decompression(filename, PATH['logpath'], False)) #解压log,此时socket已经断开不需要settimeout log.LOG.info('解压%s到%s\n'%(filename, PATH['logpath'])) else: log.LOG.info('%s下载到%s\n'%(filename, PATH['logpath']))
def operation(self): try: while len(self.stepList): #================上传Log================= if self.stepList[0] == 'up': if self.data[self.stepList[0]][0] == '!': notNullWildcard = [uploadFileName for uploadFileName in re.split('\|', self.data[self.stepList[0]][1:]) if uploadFileName] print notNullWildcard self.copyLog(self.localpath, notNullWildcard) else: notNullWildcard = [uploadFileName for uploadFileName in re.split('\|', self.data[self.stepList[0]]) if uploadFileName] self.copyLog(self.localpath, notNullWildcard, isfull = True) if self.copyLogNames: self.stepList[0] = 'chkCopyLog' else: del(self.stepList[0]) continue #================上传Log================= #================下载文件================= elif self.stepList[0] == 'down': chkPath('%shistory' % self.localpath) for flag in re.split('\|', self.data[self.stepList[0]]): tcmd = '%shistory\\%s' % (self.localpath, flag) runCMD('del /q /f "%s(5)"' % tcmd) runCMD('ren "%s(4)" "%s(5)"' % (tcmd, flag)) runCMD('ren "%s(3)" "%s(4)"' % (tcmd, flag)) runCMD('ren "%s(2)" "%s(3)"' % (tcmd, flag)) runCMD('ren "%s(1)" "%s(2)"' % (tcmd, flag)) runCMD('copy /y "%s%s" "%s(1)"' % (self.localpath, flag, tcmd)) runCMD('del /q /f "%s%s"' % (self.localpath, flag)) log.LOG.info('成功下载文件%s' % flag) self.result.append('成功下载文件%s\n'%flag) log.LOG.debug(mylib.rar.decompression('down', self.localpath)) #异步解压文件 self.stepList[0] = 'chkDecompression' continue elif self.stepList[0] == 'chkCopyLog': for n in self.copyLogNames: try: tempfile = open(r'.\temp\%s'%n, 'r') tempfile.close() except: return os.popen('start msg %username% "log已收集,请继续测试!"') log.LOG.debug(mylib.rar.compression('up', '.\\temp\\', self.copyLogNames)) self.stepList[0] = 'chkCompression' continue elif self.stepList[0] == 'chkCompression': if 'Rar.exe' in os.popen('tasklist /FI "IMAGENAME eq rar.exe"').read(): return else: try: tempfile = open(r'.\temp\up.rar', 'r') tempfile.close() if self.login: package = mylib.package.pack4('%s_%s'%(USER, self.returnIP)) else: package = mylib.package.pack4(self.returnIP) self.result.append('成功压缩文件') self.s_pack.append(package) except: self.result.append('压缩失败!') log.LOG.error('压缩失败') del(self.stepList[0]) continue elif self.stepList[0] == 'chkDecompression': if 'Rar.exe' in os.popen('tasklist /FI "IMAGENAME eq rar.exe"').read(): return else: for fileName in re.split('\|', self.data['down']): if os.path.exists('%s%s'%(self.localpath, fileName)): log.LOG.info('%s成功解压到%s' % (fileName, self.localpath)) self.result.append('%s成功解压到%s\n' % (fileName, self.localpath)) else: self.result.append('%s文件下载失败\n'%fileName) log.LOG.info('%s文件下载失败\n'%fileName) del(self.stepList[0]) continue #================下载文件================= for flag in re.split('\|', self.data[self.stepList[0]]): #================杀进程================= if self.stepList[0] == 'kill': self.result.append(killProcess(flag)) #================杀进程================= #================开进程================= elif self.stepList[0] == 'open': runCMD('start "" /d "%s" "%s"' % (self.localpath, flag)) log.LOG.info('运行文件%s'%flag) self.result.append('运行文件%s'%flag) #================开进程================= #================CMD命令================= elif self.stepList[0] == 'cmd': self.result.append('%s\n'%runCMD(flag)) #================CMD命令================= #================修改config文件================= elif self.stepList[0] == 'conf': para = re.split('-', self.data[self.stepList[0]]) CONF.save(para[0], para[1], para[2]) fileRestart = open('restart.bat', 'w') cmds = r''' taskkill /f /im client.exe taskkill /f /im gmclient_watcher.exe start.vbs del /q /f restart.bat exit ''' fileRestart.write(cmds) fileRestart.close() self.socket.close() os.system('restart.bat') #================修改config文件================= #================自我更新================= elif self.stepList[0] == 'update': chkPath(r'.\update') os.popen(r'del /q /f .\update\*.*') log.LOG.debug(mylib.rar.decompression('down', '.\\update\\', False)) #解压update文件 fileUpdate = open('update.bat', 'w') cmds = r''' taskkill /f /im client.exe taskkill /f /im gmclient_watcher.exe ping -n 2 127.0.0.1 copy /y ".\update\*.*" ".\" start.vbs del /q /f update.bat exit ''' fileUpdate.write(cmds) fileUpdate.close() self.socket.close() os.system('update.bat') #================自我更新================= #================删除文件================= elif self.stepList[0] == 'delete': delReturn = runCMD('del /q /f "%s%s"' % (self.localpath, flag)) if delReturn: self.result.append('%s%s'%(flag, delReturn)) else: self.result.append('成功删除%s\n'%flag) #================删除文件================= del(self.stepList[0]) except : self.stepList = [] self.result = [self.returnIP, ' error : ', format_exc(), '\n', self.localpath, '\n'] + self.result log.LOG.error(''.join(self.result))