Exemple #1
0
    def fetch_hosts(self):
        host_groups = list(self.login_user.userprofile.host_groups.select_related())

        while True:
            try:
                print 'z. Ungrouped [%s]' % self.login_user.userprofile.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.bindhosts_set.select_related()
                    print '%s. %s [%s]' % (index, h_group.name,len(host_list))

                user_choice = raw_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 )
                            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 = raw_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(self,h)
                                    except Exception,e:
                                        print "\033[31;1m%s\033[0m" %e
                                    finally:
                                        self.flush_audit_log(h)
                                else:
                                    print_msg("No this option!", 'err')
Exemple #2
0
    def token_auth(self):
        count = 0
        while count <3:
            token = raw_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.user
                    self.user_id = token_list[0].user.user.id

                    print_msg("--- logging host[%[email protected]%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)
                        print_msg('Bye!','warning',exit=True)
                    except Exception,e:
                        print e
                    finally:
                        self.flush_audit_log(bind_host_obj)
Exemple #3
0
                    else:
                        print_msg("No this option!", 'err')
                elif user_choice == 'z': #for ungrouped hosts
                    hosts = self.login_user.userprofile.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 = raw_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(self,h)
                                except Exception,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,e: