Advertisement
Guest User

Untitled

a guest
May 10th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. #!/usr/bin/python
  2. # Script to check a single set of credentials across multiple IP addresses.
  3. # IPs are provided via a text file
  4. import paramiko
  5. import sys
  6. import os
  7. import socket
  8. import argparse
  9.  
  10. def sshConnection(ip, user, pw):
  11. ssh = paramiko.SSHClient()
  12. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  13. # Attempt logon, and if unsuccessful try to determine why
  14. try:
  15. ssh.connect( ip , username = user, password = pw, timeout = 3)
  16. print "Connection a success for IP: " + ip
  17. ssh.close
  18. except socket.timeout:
  19. print "Connection failed due to timeout waiting for port 22 to reply for IP: " + ip
  20. except paramiko.AuthenticationException:
  21. print "Connection failed due to authentication error for IP: " + ip
  22. except paramiko.SSHException:
  23. print "Connection failed due SSH error for IP: " + ip
  24. except paramiko.ssh_exception.NoValidConnectionsError:
  25. print "Connection failed because host is not listening on port 22 (reset) : " + ip
  26. # Define command line arguments:
  27. def buildArgParser():
  28. parser = argparse.ArgumentParser(
  29. prog='sshchecker.py', description='Check a host for ssh authentication')
  30. parser.add_argument(
  31. '--file', help='Text file containing a list of IPs to check',
  32. required=True)
  33. parser.add_argument(
  34. '--user', help='Username to attempt authentication with',
  35. required=True)
  36. parser.add_argument(
  37. '--password', help='Password',
  38. required=True)
  39. return parser.parse_args()
  40.  
  41.  
  42. args = buildArgParser()
  43.  
  44.  
  45. if args.file:
  46. filename = args.file
  47. file = open(filename, 'r')
  48. print 'Checking the list of host in ' + filename
  49. else:
  50. print "Please use --file to point the script to a list of IP addresses"
  51. if args.user:
  52. user = args.user
  53. print 'Trying to ssh to the hosts with username: ' + user
  54. else:
  55. print "Please provide a username with --user"
  56. if args.password:
  57. pw = args.password
  58. print "Using the password provided"
  59. else:
  60. print "A password should be included with --password"
  61.  
  62.  
  63. for ip in file.readlines():
  64. sshConnection(ip, user, pw)
  65. sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement