Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ssh:
- shell = None
- client = None
- transport = None
- Output = ""
- c= ""
- b =""
- syncOutput=""
- def __init__(self, address, username, password):
- print("Connecting to server on ip", str(address) + ".")
- self.client = paramiko.SSHClient()
- self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- self.client.connect(address, username=username, password=password, look_for_keys=False)
- self.transport = paramiko.Transport((address, 22))
- self.transport.connect(username=username, password=password)
- thread = threading.Thread(target=self.process)
- thread.daemon = True
- thread.start()
- def closeConnection(self):
- if(self.client != None):
- self.client.close()
- self.transport.close()
- def openShell(self):
- self.shell = self.client.invoke_shell()
- def sendShell(self, command):
- if(self.shell):
- self.shell.send(command + "n")
- else:
- print("Shell not opened.")
- def process(self):
- global connection
- while True:
- # Print data when available
- if self.shell != None and self.shell.recv_ready():
- alldata = self.shell.recv(1024)
- while self.shell.recv_ready():
- alldata += self.shell.recv(1024)
- strdata = str(alldata)
- strdata.replace('r', '')
- self.Output+=strdata
- print(strdata, end = "")
- if(strdata.endswith("$ ")):
- print("n$ ", end = "")
- a = re.compile("[Ss]*(sentsd*sbytess*receivedsd*sbytess*[d.]*sbytes/sec\r\ns*totalssizesissd*s*speedupsiss[d.]*)[Ss]*")
- if a.match(self.Output):
- self.b =a.match(self.Output).group(1)
- print (self.b)
- 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):
- connection.sendShell("exit")
- sys.exit()
- sshServer='',
- sshUsername=''
- sshPasswor=''
- connection = ssh(sshServer, sshUsername, sshPassword)
- connection.openShell()
- connection.sendShell("sudo su -")
- connection.sendShell("rsync -av source destination")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement