Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import select
- import sys
- import urllib.parse
- from multiprocessing.pool import Pool
- import paramiko
- SSH_PORT = 22
- POOL_SIZE = 10
- class SSHCredetnial:
- def __init__(self, hostname, username, password, port):
- self.hostname = hostname
- self.username = username
- self.password = password
- self.port = port
- def __str__(self):
- return f'{self.username}:{self.password}@{self.hostname}:{self.port}'
- @classmethod
- def parse(cls, credetnial):
- user, host = urllib.parse.splituser(credetnial.strip())
- username, password = urllib.parse.splitpasswd(user)
- hostname, port = urllib.parse.splitnport(host, SSH_PORT)
- return cls(hostname, username, password, port)
- def check_credetnial(credetnial):
- client = paramiko.SSHClient()
- client.load_system_host_keys()
- client.set_missing_host_key_policy(paramiko.WarningPolicy)
- try:
- credetnial = SSHCredetnial.parse(credetnial)
- client.connect(
- credetnial.hostname,
- credetnial.port,
- credetnial.username,
- credetnial.password,
- timeout=3,
- auth_timeout=2
- )
- return credetnial
- except Exception as e:
- pass
- def main(argv):
- if select.select([sys.stdin], [], [], 0)[0]:
- lines = sys.stdin.readlines()
- else:
- if not argv or argv[0] in ('-h', '--help'):
- print(
- 'Author: <buymethadone@gmail.com>\n'
- 'Description:\n\n'
- ' Получает список аккаунтов SSH в виде строк <username>:<password>@<hostname>[:<port>], проверяет их и возвращает валидные.\n\n'
- 'Usage:\n\n'
- ' $ python ssh_checker.py INPUT > OUTPUT\n'
- ' $ cat INPUT | python ssh_cheker.py > OUTPUT\n'
- )
- sys.exit(0)
- filename = os.path.expanduser(argv[0])
- with open(filename) as f:
- lines = f.readlines()
- credetnials = [x for x in lines if not x.isspace()]
- pool = Pool(POOL_SIZE)
- results = pool.map(check_credetnial, credetnials)
- for result in results:
- if result:
- print(result)
- if __name__ == '__main__':
- main(sys.argv[1:])
Add Comment
Please, Sign In to add comment