python 实现远程上传文件夹

Posted dfg727

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 实现远程上传文件夹相关的知识,希望对你有一定的参考价值。

 

python2 upload.py "ip" "root" "password" "22" "Only Project DLL" "d:ENZOPublishWxcrm.Adminin
elease
etcoreapp2.1centos.7-x64publish" "/mondao/deployment/wxcrm-admin" 

#!/usr/bin/env python
import sys, time
import paramiko,datetime,os

hostname=
username=root
password=
port=22

def upload(local_dir, remote_dir, special_names):
    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):
            for name in dirs:
                local_path = os.path.join(root, name)
                a = local_path.replace(local_dir,‘‘).replace(\, /)
                remote_path = remote_dir + a
                try:
                    sftp.mkdir(remote_path)
                    print "mkdir path %s" % remote_path
                except Exception,e:
                    print("dir path is exists: %s") % remote_path
                    
            for filespath in files:
                local_file = os.path.join(root,filespath)
                a = local_file.replace(local_dir,‘‘).replace(\, /)
                remote_file = remote_dir + a
                isSpecialFile = len(special_names)==0
                if not isSpecialFile:
                    for special_name in special_names:
                        if special_name in local_file:
                            isSpecialFile = True
                if isSpecialFile:
                    try:
                        sftp.put(local_file, remote_file)
                    except Exception,e:
                        sftp.mkdir(os.path.split(remote_file)[0])
                        sftp.put(local_file, remote_file)
                    print "upload %s to remote %s" % (local_file, remote_file)
        print upload file success %s  % datetime.datetime.now()
        t.close()
    except Exception,e:
        print e

if __name__==__main__:
    args = sys.argv[1:]
    if not args:
        print("not args")
        sys.exit(1);
        
    hostname = args[0]
    username = args[1]
    password = args[2]
    port = int(args[3])
    type = args[4]
    local_dir = args[5]
    remote_dir = args[6]

    print("Paramaters:")
    print("hostname: " + hostname)
    print("username: " + username)
    print("password: " + password)
    print("port: " + str(port))
    print("type: " + type)
    print("local_dir: " + local_dir)
    print("remote_dir: " + remote_dir)
    
    t = time.time()
    
    special_names = []
    if type == "Project":
        special_names = ["Wxcrm.Admin.", "wwwrootjs", "config"]
    elif type == "Only Project DLL":
        special_names = ["Wxcrm.Admin."]
    elif type == "Only Project JS":
        special_names = ["wwwrootjs"]
    else:
        special_names = []
    #local_dir=r‘d:ENZOPublishWxcrm.Adminin
elease
etcoreapp2.1centos.7-x64publish‘
    #remote_dir=r‘/mondao/deployment/wxcrm-admin‘
    upload(local_dir, remote_dir, special_names)

    print("total run time:")
    e = time.time()
    print(e-t)

 

以上是关于python 实现远程上传文件夹的主要内容,如果未能解决你的问题,请参考以下文章

python 实现ssh远程执行命令 上传下载文件

Python脚本远程Linux创建目录上传文件

树莓派 python 如何将本地文件上传到指定的服务器页面上

paramiko库实现远程登录主机上传下载

挖洞姿势:特殊的上传技巧,绕过PHP图片转换实现远程代码执行(RCE)

python 实现脚本上传远程服务并执行脚本