Advertisement
Guest User

Untitled

a guest
Jan 5th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. for host in hosts():
  2. ssh = paramiko.SSHClient()
  3. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  4. try:
  5. ssh.connect(host, username=l_user, password=l_password, timeout=1000)
  6. except paramiko.SSHException:
  7. print("Connection Failed")
  8. except paramiko.AuthenticationException:
  9. raise 259
  10. except socket.error:
  11. raise 261
  12. transport = ssh.get_transport()
  13. session = transport.open_session()
  14. session.set_combine_stderr(True)
  15. session.get_pty()
  16. for commands in l_commands:
  17. session = transport.open_session()
  18. session.set_combine_stderr(True)
  19. session.get_pty()
  20. session.exec_command(commands)
  21. stdin = session.makefile('wb', -1)
  22. stdout = session.makefile('rb', -1)
  23. # you have to check if you really need to send password here
  24. stdin.write(l_password + 'n')
  25. stdin.flush()
  26. key_list = []
  27. for line in stdout.read().splitlines():
  28. input_line = line
  29. input_line = input_line.replace(b'[sudo] password for ufitadmin: ', b'')
  30. key_individual = str(input_line, 'utf-8')
  31. key_individual = key_individual.replace(
  32. 'cat: /home/%s/.ssh/authorized_keys: No such file or directory' % user,
  33. 'No key exists for this user.')
  34. key_list.append(key_individual)
  35. # key_list = list(filter(None, key_list))
  36. # list2 = [x for x in key_list if x]
  37.  
  38.  
  39. global keyDict
  40. keyDict = {}
  41. keyDict[host] = key_list
  42. filterString = "No key exists for this user."
  43. filteredKeylist = []
  44.  
  45. def filteredKeys():
  46. for key,value in keyDict.items():
  47. for values in value:
  48. if values == filterString:
  49. return key
  50.  
  51.  
  52.  
  53. filteredKeylist = list(filteredKeys())
  54.  
  55. print(filteredKeylist)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement