Guest User

Untitled

a guest
Jul 14th, 2018
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. import os
  2. import select
  3. import sys
  4. import urllib.parse
  5. from multiprocessing.pool import Pool
  6.  
  7. import paramiko
  8.  
  9.  
  10. SSH_PORT = 22
  11. POOL_SIZE = 10
  12.  
  13.  
  14. class SSHCredetnial:
  15. def __init__(self, hostname, username, password, port):
  16. self.hostname = hostname
  17. self.username = username
  18. self.password = password
  19. self.port = port
  20.  
  21. def __str__(self):
  22. return f'{self.username}:{self.password}@{self.hostname}:{self.port}'
  23.  
  24. @classmethod
  25. def parse(cls, credetnial):
  26. user, host = urllib.parse.splituser(credetnial.strip())
  27. username, password = urllib.parse.splitpasswd(user)
  28. hostname, port = urllib.parse.splitnport(host, SSH_PORT)
  29. return cls(hostname, username, password, port)
  30.  
  31.  
  32. def check_credetnial(credetnial):
  33. client = paramiko.SSHClient()
  34. client.load_system_host_keys()
  35. client.set_missing_host_key_policy(paramiko.WarningPolicy)
  36.  
  37. try:
  38. credetnial = SSHCredetnial.parse(credetnial)
  39.  
  40. client.connect(
  41. credetnial.hostname,
  42. credetnial.port,
  43. credetnial.username,
  44. credetnial.password,
  45. timeout=3,
  46. auth_timeout=2
  47. )
  48.  
  49. return credetnial
  50. except Exception as e:
  51. pass
  52.  
  53.  
  54. def main(argv):
  55. if select.select([sys.stdin], [], [], 0)[0]:
  56. lines = sys.stdin.readlines()
  57. else:
  58. if not argv or argv[0] in ('-h', '--help'):
  59. print(
  60. 'Author: <buymethadone@gmail.com>\n'
  61. 'Description:\n\n'
  62. ' Получает список аккаунтов SSH в виде строк <username>:<password>@<hostname>[:<port>], проверяет их и возвращает валидные.\n\n'
  63. 'Usage:\n\n'
  64. ' $ python ssh_checker.py INPUT > OUTPUT\n'
  65. ' $ cat INPUT | python ssh_cheker.py > OUTPUT\n'
  66. )
  67. sys.exit(0)
  68.  
  69. filename = os.path.expanduser(argv[0])
  70.  
  71. with open(filename) as f:
  72. lines = f.readlines()
  73.  
  74. credetnials = [x for x in lines if not x.isspace()]
  75.  
  76. pool = Pool(POOL_SIZE)
  77. results = pool.map(check_credetnial, credetnials)
  78.  
  79. for result in results:
  80. if result:
  81. print(result)
  82.  
  83.  
  84. if __name__ == '__main__':
  85. main(sys.argv[1:])
Add Comment
Please, Sign In to add comment