def run(id): if not allow(): LOGS.error('系统激活失败,请联系厂家获取支持') return #解析任务 db.connect() task = Task.get(Task.finger == id) if task is None: LOGS.error('找不到对应任务:'+str(id)) exit(-1) # 增加运行次数 task.count = task.count + 1 task.save() db.close() LOGS.info('开始任务:' + str(id)+":"+task.name) #执行任务 files =getDataFile(task) if len(files) == 0: LOGS.info('未获取到任何数据:' + str(id) + ':' + task.name) return 0 #发送文件 r = redis.Redis() for f in files: #subprocess.call('./transfer_file -s '+f, shell=True) #改为发送文件列表到redis print(f) r.lpush('file',f) #记录日志 LOGS.info('数据加入发送队列,等待发送:'+str(id)+':'+task.name)
def retrieveFile(samba, share, path, id_str): file_path = 'temp/' + share + path + '.' + id_str try: mkdir(file_path) with open(file_path, 'wb') as f: samba.retrieveFile(share, path, f) LOGS.info('获取文件:' + path) except Exception as e: LOGS.error('SMB获取文件错误:' + str(e)) return file_path
def changeNetwork(device, ip, netmask, gateway): """ @attention: change the network of the system """ #print(get_ip_address('eth5')) path = "/etc/sysconfig/network-scripts/ifcfg-" + str(device) file_handler = open(path, "r") network_content = file_handler.read() file_handler.close() conte = "IPADDR=%s\nNETMASK=%s\nGATEWAY=%s\n" % (ip, netmask, gateway) num = network_content.find("IPADDR") if num != -1: network_content = network_content[:num] + conte file_handler = open(path, "w") file_handler.write(network_content) file_handler.close() LOGS.info('ip修改成功:%s:%s:%s:%s' % (device, ip, gateway, netmask)) try: result = subprocess.call("sudo ifdown %s && sudo ifup %s" % (device, device), shell=True) if result == 0: LOGS.info('网卡重启成功:%s:%s:%s:%s' % (device, ip, gateway, netmask)) return True else: LOGS.info('网卡重启失败:%s:%s:%s:%s' % (device, ip, gateway, netmask)) return False except Exception as e: LOGS.error(str(e)) return False
def run(finger): import subprocess try: out_bytes = subprocess.check_output( '/usr/local/python3/bin/python3 run_sender.py ' + finger, shell=True, stderr=subprocess.PIPE) except Exception as e: LOGS.error('测试执行出现错误:' + str(e)) return render_template('fragment/message.html', message="运行过程中出现错误,请检查任务配置是否正常\n详情可参考日志\n信息:" + str(e)) out = out_bytes.decode('utf-8') return render_template('fragment/message.html', message=out)
def login(): form = LoginForm() #print(form.rememberme.data) if form.validate_on_submit(): try: user = User.get(User.username == form.username.data) if user.verify_password(form.password.data): login_user(user) return redirect( request.args.get('next') or url_for('main.index')) else: flash('用户名或密码错误') except Exception as e: LOGS.error(repr(e)) flash('用户名不存在') stat = '外网端' if current_app.config.get('CLIENT_TYPE') == 'receiver': stat = '内网端' return render_template('auth/login.html', form=form, stat=stat)
def getFile(task): ''' 复制指定文件 :param task: item.dir要求必须为绝对路径 :return: 已复制文件的相对路径 ''' files = [] if isFolder(task.dir): copy_tree(task.dir, 'temp/') # 目录的复制 orgFiles = getFileNames(task.dir) rootLen = len(task.dir) files = list(map(lambda x:'temp/'+x[rootLen:],orgFiles)) file_sender.rename(files, task.finger) else: shutil.copy(task.dir, 'temp') # 文件拷贝,src必须是一个文件,dst可以是一个文件或者目录 files.append('temp/' + os.path.basename(task.dir)) file_sender.rename(files, task.finger) LOGS.info("文件复制完成:" + task.dir) return files
def getSMB(task): files = [] try: samba = SMBConnection(task.username, task.password, 'mybase', task.target) samba.connect(task.dir, int(task.port)) list = task.tables.split(' ') share = list[0] path = list[1] id_str = str(task.finger) if len(list) == 3: #指定文件 file = list[2] p = path + '/' + file retrieveFile(samba, share, p, id_str) files.append(p) else: list = retrieveFileList(samba, share, path) for l in list: file = retrieveFile(samba, share, l, id_str) files.append(file) return files except Exception as e: LOGS.error('SMB连接出错:' + str(e))
def mkdir(path): # 取路径 path = os.path.split(path)[0] # 去除首位空格 path = path.strip() # 去除尾部 \ 符号 path = path.rstrip("\\") # 判断路径是否存在 # 存在 True # 不存在 False isExists = os.path.exists(path) # 判断结果 if not isExists: # 如果不存在则创建目录 # 创建目录操作函数 os.makedirs(path) LOGS.debug('创建文件夹:' + path) return True else: # 如果目录存在则不创建,并提示目录已存在 return False
files =getDataFile(task) if len(files) == 0: LOGS.info('未获取到任何数据:' + str(id) + ':' + task.name) return 0 #发送文件 r = redis.Redis() for f in files: #subprocess.call('./transfer_file -s '+f, shell=True) #改为发送文件列表到redis print(f) r.lpush('file',f) #记录日志 LOGS.info('数据加入发送队列,等待发送:'+str(id)+':'+task.name) # 删除文件 # try: # for f in files: # os.remove(f) # except: # pass if __name__ == '__main__': if len(sys.argv) > 1: id = sys.argv[1] run(id) else: LOGS.error('参数错误,未提供文件名') exit(-1)