zoukankan      html  css  js  c++  java
  • python 从windows上传文件到linux脚本

    
    
    import paramiko
    import datetime
    import os
    
    hostname = '192.168.112.132'
    username = 'root'
    password = '123456'
    port = 22
    
    
    def upload(local_dir, remote_dir):
        try:
            t = paramiko.Transport((hostname, port))
            t.connect(username=username, password=password)
            sftp = paramiko.SFTPClient.from_transport(t)
            print('upload file start %s ' % datetime.datetime.now())
            for root, dirs, files in os.walk(local_dir):
                print('[root%s][dirs%s][files%s]' % (root, dirs, files))
                for filespath in files:
                    local_file = os.path.join(root, filespath)
                    print(11, '[%s][%s][%s][%s]' % (root, filespath, local_file, local_dir))
                    a = local_file.replace(local_dir, '').replace('\', '/').lstrip('/')
                    print('01', a, '[%s]' % remote_dir)
                    remote_file = os.path.join(remote_dir, a)
                    print(22, remote_file)
                    try:
                        sftp.put(local_file, remote_file)
                    except Exception as e:
                        sftp.mkdir(os.path.split(remote_file)[0])
                        sftp.put(local_file, remote_file)
                        print("66 upload %s to remote %s" % (local_file, remote_file))
                for name in dirs:
                    local_path = os.path.join(root, name)
                    print(0, local_path, local_dir)
                    a = local_path.replace(local_dir, '').replace('\', '')
                    print(1, a)
                    print(1, remote_dir)
                    remote_path = os.path.join(remote_dir, a)
                    print(33, remote_path)
                    try:
                        sftp.mkdir(remote_path)
                        print(44, "mkdir path %s" % remote_path)
                    except Exception as e:
                        print(55, e)
            print('77,upload file success %s ' % datetime.datetime.now())
            t.close()
        except Exception as e:
            print(88, e)
    
    
    if __name__ == '__main__':
        local_dir = r'E:git_projectgit_api_excel'
        remote_dir = '/opt/api_excel/'     #只支持一级目录
        upload(local_dir, remote_dir)
    
    
    

      

     


  • 相关阅读:
    Day015 PAT乙级 1013 数素数
    Day014 PAT乙级 1012 数字分类
    Day013 PAT乙级 1007 素数对猜想
    Day012 PAT乙级 1005 继续(3n+1)猜想
    Day011 PAT乙级 1003 我要通过
    Day010 PAT乙级 1002 写出这个数
    Day009 洛谷 P5707 上学迟到
    Day008 洛谷 P2181 对角线
    Day007 Java异常处理
    Fetch()
  • 原文地址:https://www.cnblogs.com/xczssl/p/6289881.html
Copyright © 2011-2022 走看看