Skip to content

Commit

Permalink
Update logins.py
Browse files Browse the repository at this point in the history
add filter history
  • Loading branch information
gothack329 committed Jan 12, 2016
1 parent b5d9b4e commit 47dc7a0
Showing 1 changed file with 66 additions and 37 deletions.
103 changes: 66 additions & 37 deletions logins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Expand All @@ -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()
Expand All @@ -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 '''
Expand All @@ -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
***
'''

Expand All @@ -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():
Expand All @@ -152,4 +182,3 @@ def usage():
lg = logins(user,passwd)
lg.main()


0 comments on commit 47dc7a0

Please sign in to comment.