def __write_login_log(self): try: if not self.exists: op_msg = ("用户{user}登录系统失败!".format(user=self.username),) else: op_msg = ("用户{user}登录系统成功!".format(user=self.username),) op_type = "login" op_log = BLL_Op_Logs(self, self.mysql) # 获取当前上次登录的时间 op_date = op_log.last_login_date() if op_date: self.last_login_date = op_date # 写日志 op_log.save_log(op_msg, op_type) # 执行完在内存中删除此对象 del op_log except Exception as e: common.write_log("[bll.login_user.__write_log_log] {0}".format(e), "ERROR")
def __write_login_log(self): try: if not self.exists: op_msg = ("用户{user}登录系统失败!".format(user=self.username), ) else: op_msg = ("用户{user}登录系统成功!".format(user=self.username), ) op_type = "login" op_log = BLL_Op_Logs(self, self.mysql) # 获取当前上次登录的时间 op_date = op_log.last_login_date() if op_date: self.last_login_date = op_date # 写日志 op_log.save_log(op_msg, op_type) # 执行完在内存中删除此对象 del op_log except Exception as e: common.write_log("[bll.login_user.__write_log_log] {0}".format(e), "ERROR")
def user_run(user, sqlhelper): """ 普通用户登录操作模块 :param user: :return: """ try: op_log = BLL_Op_Logs(user, sqlhelper) exit_flag = False while not exit_flag: tmpi = 0 tmpflag = False # 打印标题 print(title_menu(user)) common.show_message("<-- 请选择要操作的主机 -->", "INFORMATION") # 获取用户所属组列表 host_list = user.load_hosts_by_uid() # 打印主机列表 for host in host_list: print("\033[1;32m [{0}] 主机名:{1} IP:{2} 所属组:{3}\033[0m".format(str(tmpi + 1), host['hostname'], host['ipaddr'], host['groupname'] )) tmpi += 1 # 选择一个要登录的主机 while not tmpflag: exit_flag = False choose_id = common.input_msg("\n请选择要登录的主机编号(exit 退出): ") if choose_id == "exit": exit_flag = True break if int(choose_id) > len(host_list): common.show_message("主机编号不存在!", "ERROR") continue else: choose_host = host_list[int(choose_id) - 1] break # 是否要退出 if exit_flag == True: continue # 要登录的主机IP ssh_host_ip = choose_host['ipaddr'] ssh_host_port = choose_host['port'] ssh_host_hostname = choose_host['hostname'] # 获取选择的主机中用户可以使用的ssh用户列表 ssh_users = user.load_sshusers(choose_host['hostid']) if len(ssh_users) == 0: # 未找到可以使用的账户 common.show_message("\n未配置登录用户,请联系系统管理员!", "ERROR") continue elif len(ssh_users) == 1: # 如果当前主机只有一个用户可以操作 ssh_user = ssh_users[0] else: # 有多个用户,需要选择一个 tmpi = 0 for sshuser in ssh_users: print(" [{0}] {1}".format(str(tmpi + 1), sshuser['username'])) tmpi += 1 choose = common.input_msg("请选择要使用的SSH用户编号:", int=True) ssh_user = ssh_users[int(choose) - 1] # 记录一条op日志 op_log.save_log(("用户[{0}]使用SSH用户[{1}]登录主机[{2}](IP:{3}).".format(user.username, ssh_user['username'], ssh_host_hostname, ssh_host_ip),), "ssh") # 调用paramiko进行登录 # print(ssh_host_ip, ssh_host_port, ssh_user['username'], ssh_user['auth_type'], ssh_user['passwd']) interactive.run(user, sqlhelper, ssh_host_ip, ssh_host_port, ssh_user['username'], ssh_user['auth_type'], ssh_user['passwd']) op_log.save_log(("用户[{0}]使用SSH用户[{1}]登出主机[{2}](IP:{3}).".format(user.username, ssh_user['username'], ssh_host_hostname, ssh_host_ip),), "ssh") # 释放对象 del op_log except Exception as e: common.write_log("[bin.main.user_run] {0}".format(e), "error")
def shell(userobj, sqlhelper, ipaddr, chan): """ 执行终端操作 :param userobj: 当前用户对象 :param sqlhelper: 数据库访问对象 :param ipaddr: 当前登录的主机IP :param chan: 登录的终端通道 :return: """ import select cmd = '' full_cmd_list = [] input_tab_flag = False op_log = BLL_Op_Logs(userobj, sqlhelper) oldtty = termios.tcgetattr(sys.stdin) try: tty.setraw(sys.stdin.fileno()) tty.setcbreak(sys.stdin.fileno()) chan.settimeout(0.0) while True: r, w, e = select.select([chan, sys.stdin], [], []) if chan in r: try: x = u(chan.recv(1024)) if input_tab_flag: cmd += x input_tab_flag = False if len(x) == 0: # 如果用户exit退出系统,则将剩下的不够5条的记录也入库 if len(full_cmd_list) > 0: op_log.save_log(tuple(full_cmd_list), "command") #sys.stdout.write('\r\n*** EOF\r\n') break sys.stdout.write(x) sys.stdout.flush() except socket.timeout: pass if sys.stdin in r: x = sys.stdin.read(1) if ord(x) == 9: # 用户输入tab键则记录一个标识,并不记录到命令字符中,在接收字符补全 input_tab_flag = True elif ord(x) == 127: # 用户输入backspace键,删除一个字符 cmd = cmd[0:-1] else: cmd += x.strip() # 如果输入回车 if str(x) in ['\r', '\n', '\r\n']: full_cmd_list.append("用户[{0}]在服务器[{1}]上执行 {2} 命令".format( userobj.username, ipaddr, cmd.strip())) # 每输入5条进行一次入库 if len(full_cmd_list) == 5: op_log.save_log(tuple(full_cmd_list), "command") full_cmd_list.clear() cmd = '' if len(x) == 0: break chan.send(x) except Exception as e: common.write_log("[module.interactive.shell] {0}".format(e), "error") finally: termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
def shell(userobj, sqlhelper, ipaddr, chan): """ 执行终端操作 :param userobj: 当前用户对象 :param sqlhelper: 数据库访问对象 :param ipaddr: 当前登录的主机IP :param chan: 登录的终端通道 :return: """ import select cmd = '' full_cmd_list = [] input_tab_flag = False op_log = BLL_Op_Logs(userobj,sqlhelper) oldtty = termios.tcgetattr(sys.stdin) try: tty.setraw(sys.stdin.fileno()) tty.setcbreak(sys.stdin.fileno()) chan.settimeout(0.0) while True: r, w, e = select.select([chan, sys.stdin], [], []) if chan in r: try: x = u(chan.recv(1024)) if input_tab_flag: cmd += x input_tab_flag = False if len(x) == 0: # 如果用户exit退出系统,则将剩下的不够5条的记录也入库 if len(full_cmd_list) > 0: op_log.save_log(tuple(full_cmd_list), "command") #sys.stdout.write('\r\n*** EOF\r\n') break sys.stdout.write(x) sys.stdout.flush() except socket.timeout: pass if sys.stdin in r: x = sys.stdin.read(1) if ord(x) == 9: # 用户输入tab键则记录一个标识,并不记录到命令字符中,在接收字符补全 input_tab_flag = True elif ord(x) == 127: # 用户输入backspace键,删除一个字符 cmd = cmd[0:-1] else: cmd += x.strip() # 如果输入回车 if str(x) in ['\r', '\n', '\r\n']: full_cmd_list.append("用户[{0}]在服务器[{1}]上执行 {2} 命令".format(userobj.username, ipaddr, cmd.strip())) # 每输入5条进行一次入库 if len(full_cmd_list) == 5: op_log.save_log(tuple(full_cmd_list), "command") full_cmd_list.clear() cmd = '' if len(x) == 0: break chan.send(x) except Exception as e: common.write_log("[module.interactive.shell] {0}".format(e), "error") finally: termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)