def operation(self, cmdline): global OTHER cmds = cmdline.split(' ') #分割参数 dtldata = CONF.getList(PATH, cmds[0]) step = dtldata['step'].split('|') if 'who' in step: self.s_pack.append(self.w_pack) elif 'shutdown' in step: self.s_pack.append(mylib.package.packSrvEnd()) else: cmdpack = self.makeCMDString(dtldata) cltlist_string = self.makeCLTString() self.s_pack.append(mylib.package.pack1(cltlist_string, cmdpack)) #先发0x0001包是让server知道该转发给那些玩家 if 'up' in step: dtldata['logpath'] = ''.join([dtldata['logpath'], TIME, '_', cmds[1], '\\']) if dtldata['up'][0] == '!': OTHER.append('decompression') OTHER.append('up') if 'down' in step: #step中没有down就不会产生文件包 downFileName = dtldata['down'].split('|') self.s_pack.append(mylib.package.pack3(PATH['filepath'], downFileName)) elif 'update' in step: self.s_pack.append(mylib.package.pack3('%supdate\\'%PATH['filepath'], ['*.*'])) try: self.connect() self.socket.settimeout(HEARTTIMEOUT) self.send() self.receive() self.listen_message() except Exception, e: if e.message == 'timed out': log.LOG.error('%s : time out!'%SERVER) else: log.LOG.error('%s : %s\n'%(SERVER, format_exc())) self.close()
def _findPath(flag): global PATH log.LOG.info('sreach file ...') r = GetLogicalDrives() for d in range(2, 27): if(r >> d & 1): import string drive = '%s:\\' % string.ascii_letters[d] for localpath, fields, files in os.walk(drive): if flag in files: if open(r'%s\%s' % (localpath, flag), 'r').read() == '95279527': CONF.save('localpath', flag, '%s\\' % localpath) PATH[flag] = localpath log.LOG.info('localpath : %s\\' % localpath) return '%s\\' % localpath log.LOG.info('localpath: path is none!') return ''
def __init__(self, logger): self.logger = logger if path.exists('config.ini'): mylib.settings.Status = CONF.getStatus()
import socket from mylib import log from struct import pack, unpack import time from traceback import format_exc import os,re from mylib.config import CONF address = (CONF.getServer(),8983) IP = None def getIP(): inf = os.popen('ipconfig') ipcfg = inf.readlines() for i in xrange(len(ipcfg)): lineinf = re.findall('^\s+IP[^:]+: ([^\r]+)\s$', ipcfg[i]) if lineinf and re.findall('^\s+[^:]+: ([^\r]+)\s$', ipcfg[i + 2]): return re.split('\.', lineinf[0])[-1] class testClt(): def __init__(self, sock): self.sock = sock self.switch = True self.pingQueue = [] self.i = 0 def makeHeart(self, number): return pack('<HHLH',10,0x9527,number,IP) def send(self):
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))
PASSWD]) len_msg_data = len(msg_data) string = pack("<HLHHL%ss" % len_msg_data, #第一和第二字段宽度对换 len_msg_data + 12, #12 = 4+2+2+2+4-2 0xAAAC, # magic code len_msg_data + 12, 0x9001, 0, msg_data) self.socket.sendall(string) if __name__ == '__main__': log.run_log() log.uuid_log() runCMD('title test_manage') PORT = CONF.getPort() GMPORT = CONF.getGMPort() SERVER = CONF.getServer() PATH = CONF.getCltPath() USER = CONF.getUser() PASSWD = CONF.getPasswd() DELAYKILL = CONF.getDelayKill() IP = _getIP() killProcess('heartTest_clt.exe') os.system('start /B heartTest_clt.exe') while 1: try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((SERVER, PORT)) log.LOG.info('connecting ...') sock.settimeout(HEARTTIMEOUT)
cltlist_string_len = int(cltlist.__len__() * 1.2 + 0xf000) else: cltlist = split('[|:.]', PATH['list']) cltlist_string_len = int(cltlist.__len__() * 1.2) if cltlist.__len__()/5 > 10000: #list中不能超10000个否则包长度字段溢出 raise Exception('cltlist is too more') for i in range(0, cltlist.__len__(), 5): cltlist_string += [chr(int(cltlist[i])), chr(int(cltlist[i + 1])), chr(int(cltlist[i + 2])), chr(int(cltlist[i + 3]))] if cltlist[i + 4]: #添加端口号,2字节 cltlist_string += [chr(int(cltlist[i + 4]) / 256), chr(int(cltlist[i + 4]) % 256)] else: cltlist_string.append('\x00\x00') cltlist_string = ''.join([chr(cltlist_string_len % 256), chr(cltlist_string_len / 256)] + cltlist_string) return cltlist_string if __name__ == '__main__': TIME = _getFTime() SERVER = CONF.getServer() PORT = CONF.getPort() PATH = CONF.getCtrlPath() cmds = CONF.getCMD() log.run_log() for cmd in cmds: MainOperation().operation(cmd) system('pause')