Guest User

Untitled

a guest
Jun 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. WhatThisIS="""
  4.  
  5. This is a hacked together script to connect to all of your running EC2 instances, and to provide a single SSH prompt amongst all of them, and to assist in uploading, splitting, and downloading files. email me at winniningham at and email server called gmail.com :P
  6.  
  7. """
  8.  
  9. import paramiko
  10. from numpy import *
  11. from libcloud.types import Provider
  12. from libcloud.providers import get_driver
  13.  
  14. EC2 = get_driver(Provider.EC2)
  15. driver = EC2(YOURKEY, YOURSECRET)
  16.  
  17. class myConnection():
  18. def _connect(self):
  19. ssh = paramiko.SSHClient()
  20. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  21. ssh.connect(self._host, self._port, self._user, key_filename=self._keypath)
  22. self.ssh=ssh
  23. def _command(self, command):
  24. inp,out,err = self.ssh.exec_command(command)
  25. inp.close()
  26. return (out.readlines(), err.readlines())
  27.  
  28. import cmd
  29.  
  30. class bot(cmd.Cmd):
  31. def __init__(self):
  32. cmd.Cmd.__init__(self)
  33. self.bots=[]
  34. self.prompt="cluster> "
  35. def append(self,h,u,k,name='remote'):
  36. a=myConnection()
  37. a._host=h
  38. a._user=u
  39. a._port=22
  40. a._keypath=k
  41. a._connect()
  42. a.name=name
  43. self.bots.append(a)
  44. print(repr([h,u]))
  45. def mdo(self,command):
  46. print "----------------------------------------"
  47. for b in self.bots:
  48. out, err = b._command(command)
  49. if len(out) == 0 and len(err) == 0:
  50. print b.name + ':\t no output'
  51. for x in out:
  52. print b.name + ':\t' + x.strip('\n')
  53. for x in err:
  54. print b.name + ': ERR: \t' + x.strip('\n')
  55. print "----------------------------------------"
  56. def onecmd(self, line):
  57. if line.strip() == 'exit':
  58. exit()
  59. elif line.strip()[:4] == 'PUT ':
  60. options=line.strip().split('PUT ')[1].split(' ')
  61. self.put_all(options[0],options[1])
  62. elif line.strip()[:4] == 'GET ':
  63. options=line.strip().split('GET ')[1].split(' ')
  64. elif line.strip()[:8] == 'GETSAFE ':
  65. options=line.strip().split('GETSAFE ')[1].split(' ')
  66. self.get_all_safe(options[0],options[1],options[2])
  67. elif line.strip()[:6] == 'SPLIT ':
  68. options=line.strip().split('SPLIT ')[1].split(' ')
  69. self.split_file(options[0],options[1])
  70. elif line.strip()[:9] == 'POSITION ':
  71. options=line.strip().split('POSITION ')[1].split(' ')
  72. self.position_files(options[0],options[1])
  73. else:
  74. self.mdo(line)
  75. def put_all(self, filename,target_path):
  76. for b in self.bots:
  77. sftp=b.ssh.open_sftp()
  78. sftp.put(filename,target_path+filename)
  79. print "PUT on " + b.name + ': ' + target_path+filename
  80. def get_all(self, filename, target_path):
  81. for b in self.bots:
  82. sftp=b.ssh.open_sftp()
  83. target=b.name.replace(' ','_') + '_' + filename
  84. sftp.get(filename,target_path+target)
  85. def get_all_safe(self, src_path, filename, target_path):
  86. for b in self.bots:
  87. sftp=b.ssh.open_sftp()
  88. target=b.name.replace(' ','_') + '_' + filename
  89. sftp.get(src_path+filename,target_path+target)
  90. def position_files(self, pad, target_path):
  91. Count=0
  92. for b in self.bots:
  93. filename=str(Count).rjust(int(pad),'0')+'.txt'
  94. sftp=b.ssh.open_sftp()
  95. sftp.put(filename,target_path)
  96. print "POSITIONED on " + b.name + ': ' + target_path
  97. Count+=1
  98. def split_file(self,inputfile,target_path):
  99. f=open(inputfile,'r').readlines()
  100. pieces=array_split(f,len(self.bots))
  101. #print repr(pieces)
  102. Count=0
  103. for b in self.bots:
  104. sftp=b.ssh.open_sftp()
  105. piece=[x for x in pieces[Count]]
  106. g=sftp.open(target_path,'w')
  107. g.write(''.join(piece))
  108. g.close()
  109. print "SPLIT on " + b.name + ': ' + target_path + ' (' + str(len(piece))+ ')'
  110. Count +=1
  111. def run_ec2(self):
  112. Count=0
  113. for x in driver.list_nodes():
  114. if x.state==0:
  115. self.append(x.public_ip[0],'root','/home/thomas/Downloads/mini9.pem',x.name + ' ' + str(Count).rjust(2,'0'))
  116. Count += 1
  117. self.mdo('uptime')
  118. def interactive(self):
  119. self.cmdloop()
  120.  
  121.  
  122. if __name__ == "__main__":
  123. b=bot()
  124. b.run_ec2()
  125. b.interactive()
Add Comment
Please, Sign In to add comment