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 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) ssh_interactive.login_raw(self,bind_host_obj) 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 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 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) ssh_interactive.login_raw(self, bind_host_obj) 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 fetch_hosts(self): host_groups = list(self.login_user.host_groups.select_related()) while True: try: print('z. Ungrouped [%s]' % self.login_user.bind_hosts.select_related().count()) for index, h_group in enumerate(host_groups): #host_list = models.BindHosts.objects.filter(host_group__id=h_group.id) host_list = h_group.bind_hosts.select_related() print('%s. %s [%s]' % (index, h_group.name, len(host_list))) user_choice = input("\033[32;1m>>:\033[0m").strip() if user_choice.isdigit(): user_choice = int(user_choice) if user_choice < len(host_groups): while True: #hosts = models.BindHosts.objects.filter(host_group__id=host_groups[user_choice].id ) hosts = host_groups[ user_choice].bind_hosts.select_related() for index, h in enumerate(hosts): print(" %s.\t%s(%s) %s" % (index, h.host.hostname, h.host.ip_addr, h.host_user.username)) user_choice2 = input( "\033[32;1m['b'(back)]>>>:\033[0m").strip() if user_choice2.isdigit(): user_choice2 = int(user_choice2) if user_choice2 < len(hosts): h = hosts[user_choice2] print( '\033[32;1m-----connecting [%s] with user [%s]-----\033[0m' % (h.host.ip_addr, h.host_user.username)) try: ssh_interactive.login_raw(self, h) except Exception as e: print("\033[31;1m%s\033[0m" % e) finally: self.flush_audit_log(h) else: print_msg("No this option!", 'err') elif user_choice2 == 'b': break else: print_msg("No this option!", 'err') elif user_choice == 'z': #for ungrouped hosts hosts = self.login_user.bind_hosts.select_related() while True: for index, h in enumerate(hosts): print(" %s.\t%s(%s) %s" % (index, h.host.hostname, h.host.ip_addr, h.host_user.username)) user_choice2 = input( "\033[32;1m['b'(back)]>>>:\033[0m").strip() if user_choice2.isdigit(): user_choice2 = int(user_choice2) if user_choice2 < len(hosts): h = hosts[user_choice2] print( '\033[32;1m-----connecting [%s] with user [%s]-----\033[0m' % (h.host.ip_addr, h.host_user.username)) try: ssh_interactive.login_raw(self, h) except Exception as e: print("\033[31;1m%s\033[0m" % e) finally: self.flush_audit_log(h) else: print_msg("No this option!", 'err') elif user_choice2 == 'b': break elif user_choice == 'exit': print_msg('Bye!', 'warning', exit=True) except (KeyboardInterrupt, EOFError): print_msg("input 'exit' to logout!", 'err') except UnicodeEncodeError as e: print_msg("%s, make sure you terminal supports utf8 charset!" % str(e), 'err', exit=True)
def fetch_hosts(self): host_groups = list(self.login_user.host_groups.select_related()) while True: try: print('z. Ungrouped [%s]' % self.login_user.bind_hosts.select_related().count()) for index,h_group in enumerate(host_groups): #host_list = models.BindHosts.objects.filter(host_group__id=h_group.id) host_list = h_group.bind_hosts.select_related() print('%s. %s [%s]' % (index, h_group.name,len(host_list)) ) user_choice = input("\033[32;1m>>:\033[0m").strip() if user_choice.isdigit(): user_choice = int(user_choice) if user_choice < len(host_groups): while True: #hosts = models.BindHosts.objects.filter(host_group__id=host_groups[user_choice].id ) hosts = host_groups[user_choice].bind_hosts.select_related() for index,h in enumerate(hosts): print(" %s.\t%s(%s) %s" %(index,h.host.hostname,h.host.ip_addr,h.host_user.username)) user_choice2 = input("\033[32;1m['b'(back)]>>>:\033[0m").strip() if user_choice2.isdigit(): user_choice2 = int(user_choice2) if user_choice2 <len(hosts): h= hosts[user_choice2] print('\033[32;1m-----connecting [%s] with user [%s]-----\033[0m' %(h.host.ip_addr,h.host_user.username)) try: ssh_interactive.login_raw(self,h) except Exception as e: print("\033[31;1m%s\033[0m" %e) finally: self.flush_audit_log(h) else: print_msg("No this option!", 'err') elif user_choice2 == 'b': break else: print_msg("No this option!", 'err') elif user_choice == 'z': #for ungrouped hosts hosts = self.login_user.bind_hosts.select_related() while True: for index,h in enumerate(hosts): print(" %s.\t%s(%s) %s" %(index,h.host.hostname,h.host.ip_addr,h.host_user.username)) user_choice2 = input("\033[32;1m['b'(back)]>>>:\033[0m").strip() if user_choice2.isdigit(): user_choice2 = int(user_choice2) if user_choice2 <len(hosts): h= hosts[user_choice2] print('\033[32;1m-----connecting [%s] with user [%s]-----\033[0m' %(h.host.ip_addr,h.host_user.username)) try: ssh_interactive.login_raw(self,h) except Exception as e: print("\033[31;1m%s\033[0m" %e) finally: self.flush_audit_log(h) else: print_msg("No this option!", 'err') elif user_choice2 == 'b': break elif user_choice == 'exit': print_msg('Bye!','warning',exit=True) except (KeyboardInterrupt,EOFError): print_msg("input 'exit' to logout!",'err') except UnicodeEncodeError as e: print_msg("%s, make sure you terminal supports utf8 charset!" % str(e),'err',exit=True)