Advertisement
Guest User

Untitled

a guest
May 19th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. #!/usr/local/lib/python2.7
  2. # Copyright (c) 2017 Arista Networks, Inc. All rights reserved.
  3. # Arista Networks, Inc. Confidential and Proprietary.
  4.  
  5. import paramiko
  6. import argparse
  7. import sys
  8. import os
  9.  
  10. sftp_server = "<sftp_server>"
  11. sftp_username = "<sftp_username>"
  12. sftp_passphrase = "<sftp_passphrase>"
  13.  
  14. class SftpSession:
  15. """
  16. SFTP Session
  17. """
  18.  
  19. def __init__(self, server, username, passphrase):
  20. self.sftp_port = 22
  21. self.key = paramiko.RSAKey.from_private_key_file(os.path.expanduser('~/.ssh/id_rsa'), password=self.passphrase)
  22. self._transport = paramiko.Transport(self.sftp_server, self.sftp_port)
  23. self._transport.connect(username=self.sftp_username, password=self.passphrase, pkey=self.key)
  24. self._sftp = paramiko.SFTPClient.from_transport(self._transport)
  25.  
  26. def __enter__(self):
  27. return self
  28.  
  29. def __exit__(self, *args):
  30. self.close()
  31.  
  32. def close(self):
  33. self._transport.close()
  34.  
  35. def get_dir(self, path):
  36. """Returns the list of files/directories at the path on the sftp server"""
  37. return self._sftp.listdir(path)
  38.  
  39. def set_dir(self, path):
  40. """Creates a directory on the sftp server"""
  41. self._sftp.mkdir(path)
  42.  
  43. def copy_to_sftp(self, local_path, sftp_path):
  44. """Copies the file to the specified path"""
  45. self._sftp.put(local_path, sftp_path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement