def GetConfigFile(ip,cname): ''' @ip unctrlpy端的ip地址 @cname 配置文件名 return 给PHP的文件名和文件大小 or False ''' port = get_fsocket_port() toUnctrlpyCmd = 'SYSTEM_RUN_COMMAND!{"getconfigfile":"'+cname+'|'+str(port)+'"}' fromUnctrlpyData = hostSocket.FproSocket(ip, SOCKET['REMOTE_PORT'], toUnctrlpyCmd) if not os.path.exists(DIRS['CTEMP']): os.system('mkdir -p '+ DIRS['CTEMP']) os.system('chmod 777 '+ DIRS['CTEMP']) lstr = (DIRS['CTEMP'])[-1:] fname = DIRS['CTEMP'] + str(cname) + '.tmp.' + time.strftime("%Y-%d-%m_%H%M%S", time.localtime()) if lstr != '/': fname = DIRS['CTEMP'] + str(cname) + '.tmp.' + time.strftime("%Y-%d-%m_%H%M%S", time.localtime()) fileseze = None try: fileseze = file_recv_main(host='0.0.0.0',port=int(port),filename = fname) except Exception as e: save_log('ERROR',e) if fileseze: r = "RETURN_SYSTEM_RUN_COMMAND!['"+fname+'|'+str(fileseze)+"']" os.system('chmod 777 '+fname) return r return "File get faid!"
def SaveConfigFile(ip,cname,filename): ''' @ip 远程IP地址 @cname 配置文件名称 ''' rport = GetRemotoPort(ip = ip,portlist = FSOCKET['portlist']) toUnctrlpyCmd = 'SYSTEM_RUN_COMMAND!{"saveconfigfile":"'+cname+'|'+str(rport)+'"}' fromUnctrlpyData = hostSocket.FproSocket(ip, SOCKET['REMOTE_PORT'], toUnctrlpyCmd) fname = filename result = None if hostSocket.PortIsAlive(ip,rport): try: result = osaSendFile(filename=fname,remoteip=ip,port=int(rport)) except Exception as e: save_log('ERROR',e) else: time.sleep(0.1) result = osaSendFile(filename=fname,remoteip=ip,port=int(rport)) if result == 1: r = "RETURN_SYSTEM_RUN_COMMAND!['"+cname+' save ok'+"']" return r else: time.sleep(0.1) result = osaSendFile(filename=fname,remoteip=ip,port=int(rport)) if result == 1: r = "RETURN_SYSTEM_RUN_COMMAND!['"+cname+' save ok'+"']" return r return None
def batchSendCmd(rev,ip,type): ''' @组装发送指令 ''' #重新组装发送指令 cmddict = revToDict(rev) cmddict['type'] = type cmddict['clientip'] = ip #判断远程受控端是否可以连接 try: isalive = hostSocket.PortIsAlive(ip,port=SOCKET['REMOTE_PORT']) except Exception as e: save_log('ERROR',ip+' port is not alive!'+str(e)) if isalive == False: result = "{'batchinfo':"+str(cmddict)+",'command':'batchresult','batchresult':{'status':'ERROR','result':'can not connected client ip!'}}" osaResult.batchresult(result) sys.exit() #通知远程服务器 fromUnctrlpyData = hostSocket.FproSocket(ip, SOCKET['REMOTE_PORT'], str(cmddict)) return fromUnctrlpyData
def batchFileSend(rev,ip,type): ''' @type 区分计划任务还是马上执行的批量操作 @文件发送函数 ''' ##握手之前先探测对方服务器可用端口 remotePort = GetRemotoPort(ip,portlist = FSOCKET['portlist']) #获取配置项 clist = osaBatchLib.getConfigItem(rev) #提取出需要发送的文件 sfile = clist['sourcefile'] #重组发送字符指令,添加远程端口等选项 cmddict = osaBatchLib.revToDict(rev) cmddict['fport'] = remotePort cmddict['type'] = type cmddict['clientip'] = ip #判断远程受控端是否可以连接 isalive = hostSocket.PortIsAlive(ip,port=SOCKET['REMOTE_PORT']) if isalive == False: result = "{'batchinfo':"+str(cmddict)+",'command':'batchresult','batchresult':{'status':'ERROR','result':'can not connected client ip!'}}" try: osaResult.batchresult(result) except Exception as reerror: sys.exit() #发送指令给unctrlpy端,告诉对方要进行文件接收了 fromUnctrlpyData = hostSocket.FproSocket(ip, SOCKET['REMOTE_PORT'], str(cmddict)) #初始化返回结果 result = None #检查远程端口是否己经成功开启 if fromUnctrlpyData: #如果端口己开启,开始传送文件,网络之间有延迟,对方开放端口也需要时间,可以尝试sleep一定间隔。 try: time.sleep(round(float(random.randrange(0, 10000, 1))/10000,4)) result = osaSendFile(filename=sfile,remoteip=ip,port=int(remotePort)) except Exception as e: save_log('DEBUG','BATCH file send error 1:'+str(e)) time.sleep(SOCKET['DELAY']) try: result = osaSendFile(filename=sfile,remoteip=ip,port=int(remotePort)) except Exception as e: save_log('ERROR','BATCH file send error,code:x0012,'+str(e)+',ip: '+ip) result = 'x0012' else: #防止信息未及时收到重试一次 try: time.sleep(SOCKET['DELAY']) result = osaSendFile(filename=sfile,remoteip=ip,port=int(remotePort)) except Exception as e: save_log('ERROR','BATCH file send error,code:x0011,'+str(e)+',ip: '+ip) result = 'x0011' return result