Guest User

Untitled

a guest
May 14th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -- coding: utf-8 --
  3.  
  4. import paramiko
  5.  
  6. class conectaSftp():
  7.  
  8. def __init__(self, HOST, PORTA, USUARIO, SENHA):
  9. self.host = HOST
  10. self.porta = PORTA
  11. self.usuario = USUARIO
  12. self.senha = SENHA
  13.  
  14. def conecta(self):
  15. try:
  16. self.transport = paramiko.Transport((self.host, self.porta))
  17. self.transport.connect(username=self.usuario, password=self.senha)
  18. self.sftp = paramiko.SFTPClient.from_transport(self.transport)
  19. except:
  20. print "error ao conectar"
  21.  
  22.  
  23. def exibirLstDiretorios(self):
  24. #dirlist on remote host
  25. dirlist = self.sftp.listdir('.')
  26. return dirlist
  27.  
  28. def lerConteudoArquivo(self, endLocalArq):
  29. try:
  30. dadosArquivo =self.sftp.open(endLocalArq, 'r').read()
  31. except:
  32. print "Arquivo nao encontrado"
  33. return dadosArquivo
  34.  
  35. def upload(self, local, remote):
  36. #(self, localpath, remotepath, callback=None, confirm=True)
  37. self.sftp.put(local, remote)
  38.  
  39. def download(self, remote, local):
  40. self.sftp.get(remote, local)
  41.  
  42. def close(self):
  43. if self.transport.is_active():
  44. self.sftp.close()
  45. self.transport.close()
  46. print"conexao fechada"
  47. return
  48.  
  49. x = conectaSftp(HOST, PORTA, USUARIO, SENHA)
  50. x.conecta()
  51. #print x.exibirLstDiretorios()
  52.  
  53. pastaServerArq = "/home/dyesten/testes/anotProjetoFacu.txt"
  54. pastaServ = "/home/dyesten/testes"
  55.  
  56. pastaLocalArq = "C:\Users\dyesten\Desktop\teste.txt"
  57. pastaLocal = "C:\Users\dyesten\Desktop"
  58.  
  59. x.download(pastaServerArq, pastaLocal)
  60.  
  61. x.upload(pastaLocal, pastaServ)
  62.  
  63. x.close()
Add Comment
Please, Sign In to add comment