Advertisement
Guest User

Untitled

a guest
Jun 16th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. # Data format
  4. # 64 bytes from 10.10.10.229: icmp_seq=8 ttl=64 time=263.922 ms
  5. from paramiko.ssh_exception import *
  6.  
  7. f = open("ips.txt", "r")
  8.  
  9. ips = []
  10. for line in f.readlines():
  11. ip = line.split(" ")[3]
  12. ips.append(ip[:len(ip)-1])
  13. f.close()
  14.  
  15. import paramiko
  16. username = 'username'
  17. password = 'password'
  18.  
  19.  
  20. def create_ssh(host="localhost", username=username, password=password):
  21. ssh = paramiko.SSHClient()
  22. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  23. try:
  24. print "creating connection {}".format(host)
  25. ssh.connect(host, username=username, password=password, timeout=5)
  26. print "connected"
  27. yield ssh
  28. finally:
  29. print "closing connection {}".format(host)
  30. ssh.close()
  31. print "closed"
  32.  
  33. for ip in set(ips):
  34. ssh = paramiko.SSHClient()
  35. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  36. try:
  37. print "creating connection {}".format(ip)
  38. ssh.connect(ip, username=username, password=password, timeout=5, look_for_keys=True)
  39. print "Connected!"
  40. ssh.close()
  41. raise Exception()
  42. except BadHostKeyException:
  43. print "Bad host"
  44. except AuthenticationException:
  45. print "Auth failed"
  46. except SSHException:
  47. print "Unable to establish connection {}".format(ip)
  48. except Exception as ex:
  49. if not type(ex).__name__ == 61:
  50. template = "An exception of type {0} occured. Arguments:\n{1!r}"
  51. message = template.format(type(ex).__name__, ex.args)
  52. else:
  53. message = u"Connected"
  54. raise Exception(message)
  55. print message
  56. finally:
  57. print "closing connection {}".format(ip)
  58. ssh.close()
  59. print "closed"
  60.  
  61. #create_ssh(host=ip, username=username, password=password)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement