Advertisement
Guest User

Untitled

a guest
Sep 18th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. # coding:utf-8
  2. # AutoSSH.py: run a shell script in all machines of a certain LCC
  3. # author: Thúlio Carvalho
  4.  
  5. import paramiko
  6. import sys
  7. import threading
  8. import os
  9. import time
  10. import getpass
  11.  
  12. user = ""
  13. passwd = ""
  14. machines = []
  15. script = ""
  16.  
  17. def setupLCC(lab):
  18.     global machines
  19.  
  20.     if (lab == 1):
  21.         invalid = [1, 5, 11, 22, 24, 27]
  22.         machines = range(41)
  23.         for i in invalid: machines.remove(i)
  24.  
  25.         for i in range(len(machines)):
  26.             machines[i] = "%02d" % (machines[i])
  27.  
  28.     elif (lab == 2):
  29.         invalid = [2, 7, 8, 35]
  30.         machines = range(41)
  31.         for i in invalid: machines.remove(i)
  32.  
  33.         for i in range(len(machines)):
  34.             machines[i] = "%02d" % (machines[i])
  35.        
  36.     elif (lab == 3):
  37.         invalid = [120]
  38.         machines = range(121)
  39.         for i in invalid: machines.remove(i)
  40.  
  41.         for i in range(len(machines)):
  42.             machines[i] = "%03d" % (machines[i])
  43.            
  44.  
  45.  
  46. def login(self, host):
  47.     global user, passwd, script
  48.     ssh = paramiko.SSHClient()
  49.     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  50.  
  51.     try:
  52.         ssh.connect(host, port=23456, username=user, password=passwd)
  53.     except paramiko.AuthenticationException:
  54.         print "[E] Authentication Exception"
  55.     except paramiko.SSHException:
  56.         print "[E] SSH Exception"
  57.  
  58.     try:
  59.         (_, stdout, stderr) = ssh.exec_command("./" + script)
  60.         return (stdout.read(), stderr.read())
  61.     except Exception, e:
  62.         print "[E] Script Exception"
  63.     finally:
  64.         ssh.close()
  65.  
  66.  
  67. def configureUser():
  68.     global user, passwd
  69.  
  70.     user = raw_input("Username: ")
  71.     pprompt = lambda: (getpass.getpass(), getpass.getpass('Retype password: '))
  72.  
  73.     p1, p2 = pprompt()
  74.     while p1 != p2:
  75.         print('Passwords do not match. Try again')
  76.         p1, p2 = pprompt()
  77.  
  78.     lab = int(raw_input("Choose the LCC:\n" +
  79.                     "1 - LCC1\n" +
  80.                     "2 - LCC2\n" +
  81.                     "3 - LCC3\n"))
  82.    
  83.     return lab
  84.    
  85. def usage():
  86.     print "You must parse the script file!"
  87.     print "Example: $python AutoSSH.py script.sh"
  88.     print "Exiting..."
  89.  
  90. def main(argv):
  91.     if (len(argv) < 2):
  92.         usage()
  93.         return
  94.  
  95.     lab = configureUser()
  96.     setupLCC(lab)
  97.  
  98.     try:
  99.         for i in machines:
  100.             threading.Thread(target=login,args=(i)).start()
  101.             time.sleep(0.7)
  102.            
  103.     except Exception, e:
  104.         print '[-] General Exception'
  105.  
  106. if __name__ == "__main__":
  107.     main(sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement