def start(argvs): ''' 堡垒机主函数 :param argvs: 命令行参数,本函数用不到 :return: 无 ''' user = auth() # 调用认证函数,认证 # user = dbconn.session.query(dbmodels.UserProfile).filter(dbmodels.UserProfile.username == 'zhangxiaoyu', dbmodels.UserProfile.password == '123.com').first() if user: # 认证成功执行 flag = True hostusers = user.hostusers # 获取为分组的远端主机用户 groups = user.groups # 获取组列表 while flag: try: print_welcome(user) print(mylib.color('Ungrouped hosts: (%s)', 35) %len(hostusers)) for index, hostuser in enumerate(hostusers, 1): # 遍历打印未分组远端主机用户信息 print(' %s.\t %s@%s(%s)' %(index, hostuser.username, hostuser.host.hostname, hostuser.host.ip_addr)) print(mylib.color('Groups: (%s)', 35) %len(groups)) for index, group in enumerate(groups, len(hostusers) + 1): # 遍历打印组列表 print(' %s.\t %s' %(index, group.name)) chose = input('%s (q)quit>> ' %user.username) # 获取用户输入 if chose == 'q': flag = False elif chose.isdigit(): chose = int(chose) if 0 < chose < len(hostusers) + 1: # 判断是否在未分组主机列表中 hostuser = hostusers[chose - 1] # 获取远端主机信息 open_session(user, hostuser) # 打开会话 elif len(hostusers) < chose < (len(hostusers) + len(groups) + 1): # 否则选择的就是组 group = groups[chose - len(hostusers) -1] # 获取组 group_flag = True while group_flag: for index, hostuser in enumerate(group.host_users, 1): # 遍历打印当前组内的远端主机列表 print(' %s.\t %s@%s(%s)' %(index, hostuser.username, hostuser.host.hostname, hostuser.host.ip_addr)) chose2 = input('zhangxiaoyu (q)quit, (b)break>> ').strip() if chose2.isdigit(): chose2 = int(chose2) if 0 < chose2 < (len(group.host_users) + 1): # 判断是否在远端主机用户列表中 hostuser = group.host_users[chose2 - 1] open_session(user, hostuser) # 打开会话 group_flag = False else: mylib.print_err(conf.ERRORNO['2002']) elif chose2 == 'q': group_flag = False flag = False elif chose2 == 'b': group_flag = False else: mylib.print_err(conf.ERRORNO['2002']) else: mylib.print_err(conf.ERRORNO['2002']) else: mylib.print_err(conf.ERRORNO['2002']) except (EOFError,BlockingIOError) as e: continue exit(1)
def think(self, msg): ''' 想方法,主角的内心活动 :param msg: 想的内容 :return: ''' input(mylib.color(msg, 32))
def print_welcome(user): ''' 打印欢迎信息函数 :param user: :return: ''' WELCOME_MSG = conf.WELCOME_MSG print(mylib.color(WELCOME_MSG, 36))
def help_msg(): ''' 打印帮助信息 :return: ''' print(mylib.color('Available commands:', 32)) for key in action_registers.actions: print(' ', key)
def run_cmd(host, cmd): ''' 远程执行命令函数 :param host: 主机相关参数 :param cmd: 命令 :return: 日志消息 ''' ssh = get_ssh(host) # 获取ssh对象 stdin, stdout, stderr = ssh.exec_command(cmd) # 执行命令 # 获取标准输出或错误输出内容 stdout_msg = stdout.read().decode() stderr_msg = stderr.read().decode() print("[%s] : [%s]" %(host['hostname'], cmd)) if not stderr_msg: # 如果标准错误输出内容为空说明执行成功 print(stdout_msg) msg = 'info|[%s] : [%s] is successful' %(host['hostname'], cmd) return msg else: print(mylib.color(stderr_msg)) msg = 'error|[%s] : [%s] is fail [%s]' %(host['hostname'], cmd, stderr_msg) return msg
def run_cmd(host, cmd): ''' 远程执行命令函数 :param host: 主机相关参数 :param cmd: 命令 :return: 日志消息 ''' ssh = get_ssh(host) # 获取ssh对象 stdin, stdout, stderr = ssh.exec_command(cmd) # 执行命令 # 获取标准输出或错误输出内容 stdout_msg = stdout.read().decode() stderr_msg = stderr.read().decode() print("[%s] : [%s]" % (host['hostname'], cmd)) if not stderr_msg: # 如果标准错误输出内容为空说明执行成功 print(stdout_msg) msg = 'info|[%s] : [%s] is successful' % (host['hostname'], cmd) return msg else: print(mylib.color(stderr_msg)) msg = 'error|[%s] : [%s] is fail [%s]' % (host['hostname'], cmd, stderr_msg) return msg
def start(argvs): ''' 堡垒机主函数 :param argvs: 命令行参数,本函数用不到 :return: 无 ''' user = auth() # 调用认证函数,认证 # user = dbconn.session.query(dbmodels.UserProfile).filter(dbmodels.UserProfile.username == 'zhangxiaoyu', dbmodels.UserProfile.password == '123.com').first() if user: # 认证成功执行 flag = True hostusers = user.hostusers # 获取为分组的远端主机用户 groups = user.groups # 获取组列表 while flag: try: print_welcome(user) print( mylib.color('Ungrouped hosts: (%s)', 35) % len(hostusers)) for index, hostuser in enumerate(hostusers, 1): # 遍历打印未分组远端主机用户信息 print(' %s.\t %s@%s(%s)' % (index, hostuser.username, hostuser.host.hostname, hostuser.host.ip_addr)) print(mylib.color('Groups: (%s)', 35) % len(groups)) for index, group in enumerate(groups, len(hostusers) + 1): # 遍历打印组列表 print(' %s.\t %s' % (index, group.name)) chose = input('%s (q)quit>> ' % user.username) # 获取用户输入 if chose == 'q': flag = False elif chose.isdigit(): chose = int(chose) if 0 < chose < len(hostusers) + 1: # 判断是否在未分组主机列表中 hostuser = hostusers[chose - 1] # 获取远端主机信息 open_session(user, hostuser) # 打开会话 elif len(hostusers) < chose < ( len(hostusers) + len(groups) + 1): # 否则选择的就是组 group = groups[chose - len(hostusers) - 1] # 获取组 group_flag = True while group_flag: for index, hostuser in enumerate( group.host_users, 1): # 遍历打印当前组内的远端主机列表 print(' %s.\t %s@%s(%s)' % (index, hostuser.username, hostuser.host.hostname, hostuser.host.ip_addr)) chose2 = input( 'zhangxiaoyu (q)quit, (b)break>> ').strip() if chose2.isdigit(): chose2 = int(chose2) if 0 < chose2 < (len(group.host_users) + 1): # 判断是否在远端主机用户列表中 hostuser = group.host_users[chose2 - 1] open_session(user, hostuser) # 打开会话 group_flag = False else: mylib.print_err(conf.ERRORNO['2002']) elif chose2 == 'q': group_flag = False flag = False elif chose2 == 'b': group_flag = False else: mylib.print_err(conf.ERRORNO['2002']) else: mylib.print_err(conf.ERRORNO['2002']) else: mylib.print_err(conf.ERRORNO['2002']) except (EOFError, BlockingIOError) as e: continue exit(1)