def exec_cmd(self, host_list, cmd): """ 批量命令 :param host_list: 主机列表 :param cmd: 执行命令 :return: 命令执行结果 """ results_list = [] # 结果列表 for host in host_list: host_obj = Host.filter(host)[0] # 获取主机对象 password = decrypt_oralce(host_obj.password) # 解密 conn = Lazyconnection(host_obj.addr, host_obj.name, password, host_obj.port) with conn as s: stdin, stdout, stderr = s.exec_command(cmd) stdout_result, stderr_result = stdout.read(), stderr.read() print(str(stdout_result, encoding='utf-8'), str(stderr.read(), encoding='utf-8')) if stderr_result: results_list.append( {host_obj.addr: str(stderr_result, encoding='utf-8')}) else: results_list.append( {host_obj.addr: str(stdout_result, encoding='utf-8')}) return results_list
def do_run_command(host, command, uid): try: host_obj = Host.filter(host)[0] password = decrypt_oralce(host_obj.password) # 解密 # print(host_obj.addr, host_obj.port, host_obj.name, password) ssh.connect(hostname=host_obj.addr, port=host_obj.port, username=host_obj.name, password=password) # 连接服务器 stdin, stdout, stderr = ssh.exec_command(command) channel = stdout.channel pending = err_pending = None while not channel.closed or channel.recv_ready( ) or channel.recv_stderr_ready(): readq, _, _ = select.select([channel], [], [], 1) for c in readq: # 有标准输出 if c.recv_ready(): chunk = c.recv(len(c.in_buffer)) if pending is not None: chunk = pending + chunk lines = chunk.splitlines() if lines and lines[-1] and lines[-1][-1] == chunk[-1]: pending = lines.pop() else: pending = None [push_log(line.decode(), uid) for line in lines] # 有标准错误输出 if c.recv_stderr_ready(): chunk = c.recv_stderr(len(c.in_stderr_buffer)) if err_pending is not None: chunk = err_pending + chunk lines = chunk.splitlines() if lines and lines[-1] and lines[-1][-1] == chunk[-1]: err_pending = lines.pop() else: err_pending = None [push_log(line.decode(), uid) for line in lines] except Exception as e: logger.error("远程连接发生错误:%s" % e) print("远程连接发生错误:%s" % e) finally: logger.info("远程连接关闭:%s" % ssh) ssh.close()
def get(self, local_path, remote_path, host_list): """ 从远程主机下载文件 :param local_path: 本地文件路径 :param remote_path: 远程主机路径 :param host_list: 主机列表 :return: """ for host in host_list: host_obj = Host.filter(host)[0] # 获取主机对象 password = decrypt_oralce(host_obj.password) # 解密 conn = LazyFileconnection(host_obj.addr, host_obj.name, password, host_obj.port) with conn as s: s.get(remote_path, local_path) # 将remove_path 下载到本地 local_path
def send(self, local_path, remote_path, host_list): """ 上传文件到远程主机 :param local_path: 本地文件路径 :param remote_path: 远程主机路径 :param host_list: 主机列表 :return: """ for host in host_list: host_obj = Host.filter(host)[0] # 获取主机对象 password = decrypt_oralce(host_obj.password) # 解密 conn = LazyFileconnection(host_obj.addr, host_obj.name, password, host_obj.port) with conn as s: s.put(local_path, remote_path)
def post(self): form = HostForm(request.form) if form.validate_on_submit(): host, user = request.form['host'], request.form['user'] pwd, port = request.form['password'], request.form['port'] # 检查主机是否已存在 if Host.filter(host): flash('主机已存在!') else: # 测试密码是否正确,是否能连接成功 result = Host.test_connect(host, user, pwd, port) if result['code'] == 0: password = encrypt_oracle(pwd) host_obj = Host(addr=host, name=user, password=password, port=port) db.session.add(host_obj) db.session.commit() return redirect(url_for('index')) else: flash('添加失败,请检查参数是否正确!') return render_template('order.html', form=form)
def post(self, host): form = DBForm(request.form) if form.validate_on_submit(): user, db_name = request.form['user'], request.form['db'] pwd, port = request.form['password'], request.form['port'] # 检查数据库是否存在 if DB.filter(db_name): flash('数据库已存在!') else: # 验证测试 result = DB.test_connect(host, user, pwd, port, db_name) if result['code'] != 0: flash('连接失败,请检查相关配置!') # 存入数据库 host_id = Host.filter(host)[0].id db_obj = DB(user=user, password=encrypt_oracle(pwd), port=port, db_name=db_name, host_id=host_id) db.session.add(db_obj) db.session.commit() return redirect(url_for('index')) return render_template('db.html', form=form, host=host)