Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. class ssh:
  2. shell = None
  3. client = None
  4. transport = None
  5.  
  6. Output = ""
  7. c= ""
  8. b =""
  9. syncOutput=""
  10. def __init__(self, address, username, password):
  11. print("Connecting to server on ip", str(address) + ".")
  12. self.client = paramiko.SSHClient()
  13. self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  14.  
  15. self.client.connect(address, username=username, password=password, look_for_keys=False)
  16. self.transport = paramiko.Transport((address, 22))
  17. self.transport.connect(username=username, password=password)
  18.  
  19. thread = threading.Thread(target=self.process)
  20. thread.daemon = True
  21. thread.start()
  22.  
  23. def closeConnection(self):
  24.  
  25. if(self.client != None):
  26. self.client.close()
  27. self.transport.close()
  28.  
  29. def openShell(self):
  30. self.shell = self.client.invoke_shell()
  31.  
  32. def sendShell(self, command):
  33. if(self.shell):
  34. self.shell.send(command + "n")
  35. else:
  36. print("Shell not opened.")
  37.  
  38. def process(self):
  39. global connection
  40. while True:
  41. # Print data when available
  42. if self.shell != None and self.shell.recv_ready():
  43. alldata = self.shell.recv(1024)
  44. while self.shell.recv_ready():
  45. alldata += self.shell.recv(1024)
  46. strdata = str(alldata)
  47. strdata.replace('r', '')
  48. self.Output+=strdata
  49. print(strdata, end = "")
  50.  
  51.  
  52.  
  53. if(strdata.endswith("$ ")):
  54. print("n$ ", end = "")
  55. a = re.compile("[Ss]*(sentsd*sbytess*receivedsd*sbytess*[d.]*sbytes/sec\r\ns*totalssizesissd*s*speedupsiss[d.]*)[Ss]*")
  56.  
  57. if a.match(self.Output):
  58. self.b =a.match(self.Output).group(1)
  59. print (self.b)
  60. if ("not found" in self.Output) or ("disconnected" in self.Output) or ("rsync error:" in self.Output) or ("No such file or directory" in self.Output):
  61. connection.sendShell("exit")
  62.  
  63. sys.exit()
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71. sshServer='',
  72. sshUsername=''
  73. sshPasswor=''
  74. connection = ssh(sshServer, sshUsername, sshPassword)
  75. connection.openShell()
  76. connection.sendShell("sudo su -")
  77. connection.sendShell("rsync -av source destination")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement