Advertisement
Guest User

Untitled

a guest
Aug 29th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. #! /usr/bin/env python2
  2.  
  3. """
  4. Welcome to auto_ssh.py
  5. ======================
  6.  
  7. This is a simple automizer script for ssh connections.
  8. To run, append the script with hostname, username and password.
  9. For example:
  10. $ python auto_ssh.py <hostname> <username> <password>
  11.  
  12. Another way to run the script is by hardcoding the paramters in main().
  13. And then simply run the script as:
  14. $ python auto_ssh.py
  15.  
  16. After the script is run it will make one attempt to connect to the SSH
  17. server.
  18. If successful, it will prompt for further attempts.
  19. You will be prompted to enter number of attempts until you enter ZERO to
  20. exit.
  21.  
  22. And to display this help, run:
  23. $ python auto_ssh.py -h
  24.  
  25. Dependencies
  26. ============
  27. * python 2.7
  28. * paramiko (run: "$ sudo pip install paramiko")
  29. * progressbar2 (run: "$ sudo pip install progressbar2")
  30. """
  31.  
  32. try:
  33. import sys
  34. import paramiko
  35. import progressbar
  36. except ImportError as ie:
  37. raise ie
  38.  
  39.  
  40. def get_input():
  41. input_msg = "Enter number of login attempts(or zero to exit): "
  42. err_msg = "Error! Number of attempts must be positive integer."
  43. try:
  44. num = int(raw_input(input_msg))
  45. except Exception as e:
  46. print err_msg
  47. isRetry = raw_input("Retry? (y/n):")
  48. if isRetry.lower() == "y" or isRetry.lower() == "yes":
  49. num = "" # cast num as string so its loop till it is int
  50. while not isinstance(num, int):
  51. try:
  52. num = int(raw_input(input_msg))
  53. except:
  54. print err_msg
  55. else:
  56. num = 0
  57. finally:
  58. return num
  59.  
  60.  
  61. def autoconnect_ssh(hostname, username, password):
  62. paramiko.util.log_to_file('ssh.log') # sets up logging
  63.  
  64. ssh = paramiko.SSHClient()
  65. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  66.  
  67. print ("Connecting: %s@%s" % (username, hostname))
  68. num = 1 # First login attempt is to test connectivity
  69. while num > 0:
  70. bar = progressbar.ProgressBar()
  71. for i in bar(range(0, num)):
  72. try:
  73. ssh.connect(hostname, username=username, password=password)
  74. # print "Successfully started SSH session."
  75. stdin, stdout, stderr = ssh.exec_command('ls -l')
  76. except Exception as e:
  77. # print "Error! Could not start SSH session."
  78. raise e
  79. num = get_input()
  80. print "Exiting..."
  81.  
  82.  
  83. def main(argv):
  84. if len(argv) == 1 and (argv[0] == '-h' or argv[0] == '--help'):
  85. print __doc__
  86. return 0
  87. elif len(argv) < 3:
  88. hostname = 'localhost'
  89. username = 'username'
  90. password = 'password'
  91. else:
  92. hostname = argv[0]
  93. username = argv[1]
  94. password = argv[2]
  95.  
  96. autoconnect_ssh(hostname, username, password)
  97.  
  98.  
  99. if __name__ == '__main__':
  100. main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement