def interactive(self): """启动交互脚本""" if self.auth(): print("Ready to print all the authorized hosts...to this user ...") while True: host_group_list = self.user.host_groups.all() for index, host_group_obj in enumerate(host_group_list): print("%s. \t%s [%s]" % (index, host_group_obj.name, host_group_obj.host_to_remote_users.count())) print("z.\t未分组主机 [%s]" % (self.user.host_to_remote_users.count())) choice = input("请选择主机组>>:").strip() if choice.isdigit(): choice = int(choice) selected_host_group = host_group_list[choice] elif choice == 'z': selected_host_group = self.user while True: for index, host_to_user_obj in enumerate(selected_host_group.host_to_remote_users.all()): print("%s. \t%s" % (index, host_to_user_obj)) choice = input("请选择主机>>:").strip() if choice.isdigit(): choice = int(choice) selected_host_to_user_obj = selected_host_group.host_to_remote_users.all()[choice] print("going to logon %s" % selected_host_to_user_obj) paramiko_ssh.ssh_connect(self, selected_host_to_user_obj) if choice == 'b': break
def interactive(self): """启动用户交互脚本""" if self.auth(): print("以下为本用户可操作的所有主机组及对应主机") while True: host_group_list = self.user.host_groups.all() for index,host_group in enumerate(host_group_list): print("%s.\t%s[%s]"%(index,host_group.name,host_group.host_to_remote_users.count())) print("z.\t未分组主机[%s]" % (self.user.host_to_remote_users.count())) choice = input("请选择要操作的主机组:").strip() if choice.isdigit(): choice=int(choice) selected_host_group = host_group_list[choice] elif choice =='z': selected_host_group = self.user while True: for index,host_to_user in enumerate(selected_host_group.host_to_remote_users.all()): print("%s.\t%s"%(index,host_to_user)) choice = input("请选择要操作的主机:").strip() if choice.isdigit(): choice = int(choice) selected_host_to_user_obj = selected_host_group.host_to_remote_users.all()[choice] print('going to logon %s' %selected_host_to_user_obj) ssh_connect(self,selected_host_to_user_obj) if choice == 'b': break
def interactive(self, log): """启动交互脚本""" if self.auth(): print("Ready to print all the authorized hosts...to this user ...") while True: try: host_group_list = self.user.host_groups.select_related( ).all() for index, host_group_obj in enumerate(host_group_list): print("%s.\t%s[%s]" % (index, host_group_obj.name, host_group_obj.host_to_remote_users.count())) print("z.\t未分组主机[%s]" % (self.user.host_to_remote_users. select_related().count())) choice = input("请选择主机组>>:").strip() if choice.isdigit(): choice = int(choice) if choice < len(host_group_list): selected_host_group = host_group_list[choice] else: log("No this option!", 'err') elif choice == 'z': selected_host_group = self.user while True: host_list = selected_host_group.host_to_remote_users.select_related( ).all() for index, host_to_user_obj in enumerate(host_list): print("%s.\t%s" % (index, host_to_user_obj)) choice = input("请选择主机>>:").strip() if choice.isdigit(): choice = int(choice) if choice < len(host_list): selected_host_to_user_obj = host_list[choice] print("going to logon %s" % selected_host_to_user_obj) try: paramiko_ssh.ssh_connect( self, selected_host_to_user_obj) except Exception as e: print("\033[31;1m%s\033[0m" % e) finally: self.flush_audit_log( selected_host_to_user_obj) else: log("No this option!", 'err') if choice == "b": break elif choice == 'exit': log('Bye!', 'warning', exit=True) except (KeyboardInterrupt, EOFError): log("input 'exit' to logout!", 'err') except UnicodeEncodeError as e: log("%s, make sure you terminal supports utf8 charset!" % str(e), 'err', exit=True)
def interactive(self): """启动交互脚本""" if self.auth(): print("Ready to print all the authorized hosts...to this user ...") while True: host_list = settings.host_dic[self.user].keys() host_list = list(host_list) for index,host in enumerate(host_list): print("%s.\t%s"%(index,host)) choice = input("请选择host>>:").strip() if choice.isdigit(): choice = int(choice) print("going to logon %s" % host_list[choice] ) paramiko_ssh.ssh_connect(self, host_list[choice])
def interactive(self): if self.auth(): print('以下为所有认证过的主机。。。') # 所有主机组 while True: host_group_list = self.user.host_groups.all() # <QuerySet [<HostGroup: WEB Server>, <HostGroup: DB>]> # 循环主机分组对象,取出主机分组名、已经对应的主机账号个数 for index1, host_group_obj in enumerate(host_group_list): print('%s \t %s [%s]' % (index1, host_group_obj.name, host_group_obj.host_to_remote_users.count())) print('z. \t未分组主机 [%s]' % (self.user.host_to_remote_users.count())) # 选择输入代码 choices = input('请选择主机组:>>>') if choices.isdigit(): # 如果是数字,则继续 choices = int(choices) # 已选择的主机分组 selected_host_group = host_group_list[choices] # <QuerySet [<HostGroup: WEB Server>, <HostGroup: DB>]>[1] elif choices == 'z': selected_host_group = self.user # 赋值 while True: """selected_host_group.host_to_remote_users.all() = <QuerySet [<HostToRemoteUser: test:root: 123>, <HostToRemoteUser: centOS:shit: 123>, <HostToRemoteUser: centOS:root: 12 345>]> """ # 所有关联的主机账户信息 all_related_host_user_list = selected_host_group.host_to_remote_users.all() for index2, host_to_user_obj in enumerate(all_related_host_user_list): print('%s \t %s' % (index2, host_to_user_obj)) choices = input('请选择主机>>>:').strip() if choices.isdigit(): choices = int(choices) # 用户已选择的主机账户对象 seleced_host_to_user_obj = all_related_host_user_list[choices] print('开始登录 %s' % seleced_host_to_user_obj) paramiko_ssh.ssh_connect(self, seleced_host_to_user_obj) elif choices == 'b': break
def interactive(self): """启动交互脚本""" if self.auth(): print("Ready to print all the authorized hosts ... to this user ...") while True: host_group_list = self.user.host_groups.all() #该用户所有主机组,如<QuerySet [<HostGroup: WEB server>, <HostGroup: DB>]> for index,host_group_obj in enumerate(host_group_list): print("%s.\t%s[%s]"%(index, host_group_obj.name, host_group_obj.host_to_remote_users.count())) #最后一个是打印每个主机组关联的第三张表host_to_remote_users的数量 print("z.\t未分组主机[%s]" % (self.user.host_to_remote_users.all()).count()) choice = input("请选择主机组>>:").strip() if choice.isdigit(): #是正数数字的话 choice = int(choice) if choice <= host_group_list.count()-1: selected_host_group = host_group_list[choice] #选择该用户的主机组 else: print('请正确输入主机组!!!!') continue elif choice == 'z': selected_host_group = self.user #没有分组的时候 else: print('请正确输入主机组!!!!') continue while True: for index,host_to_remote_obj in enumerate(selected_host_group.host_to_remote_users.all()): #打印所选主机组关联的第三张表host_to_remote_users print("%s.\t%s" % (index,host_to_remote_obj)) choice = input("请选择主机>>:").strip() if choice.isdigit(): choice = int(choice) if choice <= selected_host_group.host_to_remote_users.all().count(): selected_host_to_user_obj = selected_host_group.host_to_remote_users.all()[choice] #选择关联的第三张表host_to_remote_users print("going to logon %s" % selected_host_to_user_obj) paramiko_ssh.ssh_connect(self, selected_host_to_user_obj) continue #退出连接之后,再次进入主机选择项。避免报错 else: print('请正确输入主机!!!!') continue if choice == 'b': break else: print('请正确输入主机!!!!') continue
def token_auth(self): count = 0 while count < 3: token = input( "press ENTER if you don't have token, [input your token]:" ).strip() if len(token) == 0: return None filter_date = datetime.timedelta(seconds=-300) token_list = models.Token.objects.filter( token=token, date__gt=django.utils.timezone.now() + filter_date) if len(token_list) > 0: if len(token_list) > 1: print( "Found more than 1 matched tokens,I cannot let you login,please contact your IT guy!" ) else: #auth correct bind_host_obj = token_list[0].host self.login_user = token_list[0].user self.user_id = token_list[0].user.id self.print_msg( "--- logging host[%s@%s(%s)], be patient,it may takes a minute --- " % (bind_host_obj.host_user.username, bind_host_obj.host.hostname, bind_host_obj.host.ip_addr), 'normal') try: #ssh_interactive.login(self,bind_host_obj) paramiko_ssh.ssh_connect(self, bind_host_obj) self.print_msg('Bye!', 'warning', exit=True) except Exception as e: print(e) finally: self.flush_audit_log(bind_host_obj) else: count += 1 print("Invalid token,got %s times to try!" % (3 - count)) else: sys.exit("Invalid token, too many attempts,exit.")
def interactive(self): """启动交互脚本""" # 如果用户登陆成功,进入循环程序 if self.auth(): print('Ready to print all the authorized hosts...to this user...') while True: host_group_list = self.user.host_groups.all() for index, host_group_obj in enumerate(host_group_list): print('%s.\t%s[%s]' % (index, host_group_obj.name, host_group_obj.host_to_remote_users.count())) # 打印所有未分组的主机,注意:数据库里要保证单独分给用户的主机不在分组里 print('z.\t未分组主机[%s]' % (self.user.host_to_remote_users.count())) choice = input('请选择主机组>>:').strip() selected_host_group = '' if choice.isdigit(): choice = int(choice) # 取出用户选择的组里所有的主机名加帐号 selected_host_group = host_group_list[choice] elif choice == 'z': # 取出未分组里所有的主机名加帐号 selected_host_group = self.user while True: for index, host_to_user_obj in enumerate( selected_host_group.host_to_remote_users.all()): print('%s.\t%s' % (index, host_to_user_obj)) choice = input('请选择主机>>:').strip() if choice.isdigit(): choice = int(choice) selected_host_to_user_obj = selected_host_group.host_to_remote_users.all( )[choice] print('going to logon %s' % selected_host_to_user_obj) # 开始连接 paramiko_ssh.ssh_connect(self, selected_host_to_user_obj) if choice == 'b': break
def interactive(self): if self.auth(): print('Ready to display all the authorized host...to this user...') while True: host_group_list = self.user.host_groups.all() for index, host_group_obj in enumerate(host_group_list): print('%s\t%s:[%s]' % (index, host_group_obj.name, host_group_obj.host_to_remote_users.count())) print('z.\t未分组主机:[%s]' % (self.user.host_to_remote_users.count())) choice = input('请选择主机组>>:').strip() if choice.isdigit(): choice = int(choice) selected_host_group = host_group_list[choice] elif choice == 'z': selected_host_group = self.user # else: # print('The choice is "0,1,z"') while True: for index, host_to_user_obj in enumerate( selected_host_group.host_to_remote_users.all()): # self.host_to_user_obj = host_to_user_obj print('%s\t%s' % (index, host_to_user_obj)) choice = input('请选择主机>>:').strip() if choice.isdigit(): choice = int(choice) selected_host_to_user_obj = selected_host_group.host_to_remote_users.all( )[choice] print('going to logon %s' % selected_host_to_user_obj) paramiko_ssh.ssh_connect(self, selected_host_to_user_obj) # elif choice == 'z': # for index,host_to_user_obj in enumerate(self.user.host_to_remote_users.all()): # print('%s.\t%s' % (index, host_to_user_obj)) elif choice == 'b': break
def interactive(self): """启动用户交互脚本""" if self.auth(): print("<准备列出该用户可操作的主机组列表>") print(self.user.host_groups.all()) while True: host_group_list = self.user.host_groups.all() for index,host_group_obj in enumerate(host_group_list): print("%s.\t%s[%s]"%(index,host_group_obj,host_group_obj.host2remote_users.count())) print("z.\t未分组[%s]"%(self.user.host2remote_users.count())) choice = input("请输入选择主机组序号:") if choice.isdigit(): choice = int(choice) selected_host_group_obj = host_group_list[choice] elif choice == "z": selected_host_group_obj = self.user while True: for index,host2remote_users_obj in enumerate(selected_host_group_obj.host2remote_users.all()): print("%s.\t%s" % (index, host2remote_users_obj)) choice = input("请输入选择主机序号:").strip() if choice.isdigit(): choice = int(choice) selected_host2user_obj = selected_host_group_obj.host2remote_users.all()[choice] print("going to login %s" % selected_host2user_obj) paramiko_ssh.ssh_connect(self, selected_host2user_obj) if choice == "b": break
def interactive(self): """ 现在开始验证堡垒机账号和密码,并列出该账号下的主机和主机组列表 """ if self.validateUser(): print( "Welcome to CrazyEye system! The following host or hostgroup you can login" ) #列出主机组 while True: host_group_list = models.HostGroup.objects.all() #ungrouped_host_list = models.UserProfile.objects.filter(username=self.username).host_to_remote_user.all() for index, host_group_obj in enumerate(host_group_list): print("%s\t[%s]%s" % (index, host_group_obj, host_group_obj.host_to_remote_user.count())) print( "z ungroup_host[%s]" % models.UserProfile.objects.filter(username=self.username). first().host_to_remote_user.count()) #选择主机组 group_choice = input(">>>please select hostgroup:").strip() if group_choice.isdigit(): choice_group = host_group_list[int(group_choice)] elif group_choice == 'z': choice_group = models.UserProfile.objects.filter( username=self.username).first() else: continue #列出该组下的所有主机和用户 while True: for index, htru in enumerate( choice_group.host_to_remote_user.all()): print("%s\t[%s]" % (index, htru)) #选择某一台主机并开始登入 host_choice = input(">>>please select host:").strip() if host_choice.isdigit(): hostobj = choice_group.host_to_remote_user.all()[int( host_choice)] #print(hostobj.host.ip) #print(hostobj.host.port) #print(hostobj.remote_user.username) #print(hostobj.remote_user.pwd) #将所选的主机ip,port,user,pwd,传给paramiko来进行远程连接 #打印一下所选的主机ip,port,user,pwd print("Wait...loinging to %s" % hostobj.host) print('--------------info start---------------------') print("host: %s, port: %s, user: %s, pwd: %s" % (hostobj.host.ip, hostobj.host.port, hostobj.remote_user.username, hostobj.remote_user.pwd)) print('--------------info end-----------------------') # ready to connect! #paramiko_ssh.ssh_connect(self, # hostobj.host.ip, # hostobj.host.port, # hostobj.remote_user.username, # hostobj.remote_user.pwd) paramiko_ssh.ssh_connect(self, hostobj) if host_choice == "b": break else: print("you have no rights to login this system.")