From 47dc7a07ba22b907f05227dbb1f403493fe00fd2 Mon Sep 17 00:00:00 2001 From: Gothack Date: Tue, 12 Jan 2016 14:49:57 +0800 Subject: [PATCH] Update logins.py add filter history --- logins.py | 103 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 66 insertions(+), 37 deletions(-) diff --git a/logins.py b/logins.py index 18fcbcc..c3dd1cd 100644 --- a/logins.py +++ b/logins.py @@ -16,6 +16,7 @@ def __init__(self,username,password): def ssh(self, ip, port='22'): ssh = pexpect.spawn('ssh %s@%s -p %s ' % (self.username, ip, port)) + ssh.setwinsize(40,130) try: i = ssh.expect(['continue connecting (yes/no)?','[pP]assword:'],timeout=10) if i == 0 : @@ -38,7 +39,7 @@ def ssh(self, ip, port='22'): - def searchDevice(self,device_info,_list='None'): + def searchDevice(self,device_info='',_list='None'): device_info = device_info.lower().replace('_','-') if _list == 'None': device = open(self.conffile,'r').readlines() @@ -51,13 +52,19 @@ def searchDevice(self,device_info,_list='None'): return result def runcmd(self,cmd): - cmd = cmd.strip().strip('/') + cmd = cmd.strip().strip('$') if cmd == '' or cmd == 'help': self.cmd_help() elif cmd.startswith('user'): self.cmd_user(cmd) + elif cmd.startswith('show'): + self.cmd_show(cmd) else: self.cmd_help() + + def cmd_show(self,cmd): + print 'Current user is: ',self.username + print def cmd_help(self): print ''' @@ -67,9 +74,13 @@ def cmd_help(self): To change username & password, use: -search> /user [username] +search> $user [username] search> Password:***** +To show current user, use: + +search> $show + *** ''' @@ -89,53 +100,72 @@ def cmd_user(self,cmd): def main(self): while True: - + history = [] + flag = -2 + device_list = self.searchDevice() + device_all = device_list + history.append(device_all) + print '''\nNotice: +\texit : exit program +\tend : all device list +\tq : previous device list +\t/3 : select device\n''' while True: - print '\nNotice: Search sth. from IP or Hostname, input "quit" to quit!\n' + notice = '' + print '#' + print ' Devices:'+str(len(device_list)) + print ' Filter times:'+str(len(history)) + print ' Filter length:',str([len(i) for i in history]) + print device_info = raw_input('search>') - if device_info.strip() == 'quit': + if device_info.strip() == 'exit': sys.exit() print 'Bye!' - if device_info.startswith('/'): + elif device_info.strip() == 'q': + try: + device_list = history[flag] + history.pop() + except: + history = [] + history.append(device_all) + continue + elif device_info.strip() == 'end': + device_list = history[0] + history = [] + history.append(device_all) + continue + elif device_info.startswith('$'): self.runcmd(device_info) + continue + elif device_info.startswith('/'): + try: + selected = device_list[int(device_info.strip('/'))] + break + except Exception, e: + print e + continue else: - break - - device_list=self.searchDevice(device_info) + device_list = self.searchDevice(device_info,device_list) + history.append(device_list) - if len(device_list) == 1: - ip, hostname = device_list[0].split(',') - elif len(device_list) == 0: - print 'No Device found!' - continue - else: - while True: - for i in device_list: - print '['+str(device_list.index(i))+'] '+i.strip() - print '\nNotice: Input device Number , or start with "/" to search!' - device_index = raw_input('select$') - if device_index.startswith('/'): - device_list=self.searchDevice(device_index.strip('/'),device_list) - if len(device_list) == 1: - selected = device_list[0] - break - continue - elif device_index.strip() == 'quit': + if len(device_list) == 1: + selected = device_list[0] break - try: - selected = device_list[int(device_index)] - break - except: + elif len(device_list) == 0: + notice = 'No device found!' + device_list = history[flag] + history.pop() continue - if device_index.strip() == 'quit': - continue + for i in device_list: + print '['+str(device_list.index(i))+'] '+i.strip() - ip, hostname = selected.split(',') + if notice:print notice + ip, hostname = selected.split(',') result = self.ssh(ip.strip()) if 'port 22: Connection refused' in str(result): - self.ssh(ip.strip(),'2222') + self.ssh(ip.strip(),'51899') def usage(): @@ -152,4 +182,3 @@ def usage(): lg = logins(user,passwd) lg.main() -