Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2.7
- import paramiko
- import os
- class MySFTPClient(paramiko.SFTPClient):
- def exists(self, path):
- """os.path.exists for paramiko's SCP object
- """
- try:
- self.stat(path)
- except IOError, e:
- if e[0] == 2:
- return False
- raise
- else:
- return True
- def put_dir(self, source, target):
- self.mkdir(target, ignore_existing=True)
- for root, dirs, files in os.walk(source):
- for f in files:
- local_file = os.path.join(root, f)
- remote_file = os.path.join(target, local_file.replace(source, '').replace('/', '', 1))
- try:
- self.put(local_file, remote_file)
- except Exception, e:
- self.mkdir(os.path.join(target, os.path.split(remote_file)[0]))
- self.put(local_file, remote_file)
- for d in dirs:
- local_path = os.path.join(root, d)
- remote_path = os.path.join(target, local_path.replace(source, '').replace('/', '', 1))
- try:
- self.mkdir(remote_path)
- except Exception, e:
- print str(e)
- def mkdir(self, path, mode=0775, ignore_existing=False):
- try:
- super(MySFTPClient, self).mkdir(path, mode)
- except IOError as e:
- if ignore_existing:
- pass
- else:
- print "[ERROR] mkdir failed! %s" % (str(e),)
- raise
- if __name__ == "__main__":
- client = paramiko.SSHClient()
- client.load_system_host_keys()
- client.set_missing_host_key_policy(paramiko.WarningPolicy())
- client.connect('10.10.192.68', username='ubuntu', password='ubuntu')
- sftp = MySFTPClient.from_transport(client.get_transport())
- sftp.put_dir('build/nginx/any-static', '/home/ubuntu/any-static')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement