Sftp利用Python+paramiko同步数据

常用脚本 0 656 团子精英 收藏

废话不多说,直接上脚本

需要paramiko和six模块,注意,paramiko版本建议2.8.1,太高了不能用

pip install paramiko==2.8.1
pip install six


脚本内容如下,请自行修改一些配置

import os
import sys
import paramiko
    
def main():
    host = 'fasteda.cn'
    port = 22
    username = 'fasteda'
    prefix = '.'
    local_paths = ['/sftp/v-test/aa', '/sftp/v-test/ab', '/sftp/v-test/ac']
    remote_paths = ['/bbb/Tester/Advantest', '/bbb/Prober/Taaa']
    private_key = paramiko.RSAKey.from_private_key_file('~/.ssh/id_rsa')
    try:
        transport = paramiko.Transport((host, port))
        transport.connect(username=username, pkey=private_key)
        transport.set_keepalive(60)
        sftp = paramiko.SFTPClient.from_transport(transport)
    except Exception as e:
        print('Error connecting to SFTP server: %s' % e)
        sys.exit(1)
    for i, local_path in enumerate(local_paths):
        remote_path = remote_paths[0] if i < 2 else remote_paths[1]
        print('Uploading files from %s to %s' % (local_path, remote_path))
        upload_files(sftp, local_path, remote_path, prefix)
def upload_files(sftp, local_path, remote_path, prefix):
    for item in os.listdir(local_path):
        if os.path.isfile(os.path.join(local_path, item)):
            if item.startswith(prefix):
                continue
            local_file = os.path.join(local_path, item)
            remote_file = os.path.join(remote_path, item)
            remote_file_prefix = os.path.join(remote_path, prefix + item)
            try:
                sftp.stat(remote_file)
                print('File %s already exists, skipping' % item)
                continue
            except IOError:
                pass
            print('Uploading file %s' % item)
            sftp.put(local_file, remote_file_prefix)
            if sftp.stat(remote_file_prefix).st_size != os.stat(local_file).st_size:
                print('Upload of file %s failed, deleting incomplete file' % item)
                sftp.remove(remote_file_prefix)
                continue
            sftp.rename(remote_file_prefix, remote_file)
        elif os.path.isdir(os.path.join(local_path, item)):
            remote_dir = os.path.join(remote_path, item)
            try:
                sftp.stat(remote_dir)
            except IOError:
                print('Creating remote directory %s' % remote_dir)
                sftp.mkdir(remote_dir)
            upload_files(sftp, os.path.join(local_path, item), remote_dir, prefix)
    sftp.close()
    transport.close()
if __name__ == '__main__':
    main()


20230227做一些更新,解决上传失败后无法删除,加了删除重新传

import os
import sys
import paramiko
def upload_files(sftp, local_path, remote_path, prefix):
    for item in os.listdir(local_path):
        if os.path.isfile(os.path.join(local_path, item)):
            if item.startswith(prefix):
                continue
            local_file = os.path.join(local_path, item)
            remote_file = os.path.join(remote_path, item)
            remote_file_prefix = os.path.join(remote_path, prefix + item)
            try:
                sftp.stat(remote_file)
                print('File %s already exists, skipping' % item)
                continue
            except IOError:
                pass
            print('Uploading file %s' % item)
            try:
                sftp.put(local_file, remote_file_prefix)
            except IOError as e:
                print('Error uploading file %s: %s' % (item, e))
            if sftp.stat(remote_file_prefix).st_size != os.stat(local_file).st_size:
                print('Upload of file %s failed, deleting incomplete file' % item)
                sftp.remove(remote_file_prefix)
                continue
            sftp.rename(remote_file_prefix, remote_file)
        elif os.path.isdir(os.path.join(local_path, item)):
            remote_dir = os.path.join(remote_path, item)
            try:
                sftp.stat(remote_dir)
            except IOError:
                print('Creating remote directory %s' % remote_dir)
                sftp.mkdir(remote_dir)
            upload_files(sftp, os.path.join(local_path, item), remote_dir, prefix)
  
    
paramiko.util.log_to_file('paramiko.log')    
def main():
    host = 'aaaaaa'
    port = 22
    username = 'bbbbbb'
    prefix = '.'
    local_paths = ['/path1', '/path2', '/path3']
    remote_paths = ['/remote_path1', '/remote_path1', '/remote_path1']
    private_key = paramiko.RSAKey.from_private_key_file('/../rsa')
    try:
        transport = paramiko.Transport((host, port))
        transport.connect(username=username, pkey=private_key)
        transport.set_keepalive(60)
        sftp = paramiko.SFTPClient.from_transport(transport)
    except Exception as e:
        print('Error connecting to SFTP server: %s' % e)
        sys.exit(1)
    for i, local_path in enumerate(local_paths):
        remote_path = remote_paths[0] if i < 2 else remote_paths[1]
        print('Uploading files from %s to %s' % (local_path, remote_path))
        upload_files(sftp, local_path, remote_path, prefix)
    sftp.close()
    transport.close()
    
if __name__ == '__main__':
    main()


相关推荐:

网友留言:

您需要 登录账户 后才能发表评论

我要评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
验证码