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_project\git_api_excel'
    remote_dir = '/opt/api_excel/'     #只支持一级目录
    upload(local_dir, remote_dir)

  

 


python 从windows上传文件到linux脚本的更多相关文章

  1. Windows上传文件到linux 使用winscp

    Windows上传文件到linux 使用winscp, winscp下载目录 https://sourceforge.net/projects/winscp/postdownload?source=d ...

  2. windows上传文件到 linux的hdfs

    一.windows上传文件到 linux的hdfs 1.先在 centos 上开启 hdfs, 用 jps 可以看到下面信息, 说明完成开启 2.在win上配置 hadoop (https://www ...

  3. Xshell实现Windows上传文件到Linux主机

    我是怎么操作的: 1.打开一台本地Linux虚拟机,使用mount 挂载Windows的共享文件夹到Linux上,然后拷贝数据到Linux虚拟机里面:(经常第一步都不顺利,无法挂载Windows的文件 ...

  4. linux安装lrzsz支持rz从windows上传文件到linux

    1.下载lrzsz wget https://wangxuejin-data-1252194948.cos.ap-shanghai.myqcloud.com/lrzsz-0.12.20.tar.gz ...

  5. windows 上传文件到 Linux 服务器

    方法一: pscp E:\javaWP\new11111.txt username@130.75.7.156:/home/

  6. windows上传文件到linux

    1.在putty的网站上下载putty跟pscp 2.安装ssh跟putty sudo apt-get install openssh-server sudo apt-get install putt ...

  7. 使用FileZilla解决从Windows上传文件到Linux vsftpd的乱码问题!

    日前将golang的开发环境从windows转移到了CentOS6上,为了把以前写得项目代码上传到centos,架设了vsftpd服务,设置为本地用户登录,然后用惯用的ftp软件flashfxp上传了 ...

  8. 利用Xshell从windows上传文件到linux

    1.首先,打开你的xshell客户端. 2.我用的是ubuntu 所以用 apt-get install lrzsz 命令来安装这个上传软件. 安装成功以后,可以使用rz上传,sz下载. 然后等待上传 ...

  9. windows上传文件到linux云服务器上

    安装putty,将pscp.exe移到 C:\Windows\System32 目录下. 在cmd 中执行,pscp -l rot -pw [password] -ls [ip]:/opt 查看目录 ...

随机推荐

  1. ajaxpro——js调用后台的方法

    前提:添加并引用类库ajaxpro.dll 1.把引用的类库改为自己(如果是自己的话,就不用修改): <%@ Page Language="C#" AutoEventWire ...

  2. zabbix3.2在lamp环境安装

    zabbix官网下载zabbix-3.2.1.tar.gz wget http://jaist.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20 ...

  3. mybatis遇见的奇葩问题(返回null)

    1.问题描述 select 语句没有问题,执行完毕后通过日志也可以看出 select到数据了,但是拿到的值就是null 2.原因 原来是有人将对象变量命名给改了,导致select到结果后不能映射成为对 ...

  4. html5 画个圈

    <html><body><canvas id="myCanvas" width="200" height="100&qu ...

  5. Alyona and flowers

    Alyona and flowers time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. elipse图标注解

    Java中的访问修饰符(访问控制符)包括:public,protected,default,private.分别代表了不同的访问权限.如果省略,则被视为使用了默认的default作为访问修饰符.从字面 ...

  7. win7 xp 双系统安装记录

    原机win7 64 增加xp x86 win7在c盘,xp装h盘 1.老毛桃pe,雨林木风gho,蓝屏,0000007b 2.通用pe.雨林木风gho,蓝屏,00000007b 3.pe设置h盘为系统 ...

  8. 《剑指Offer》算法题——替换空格

    题目:请实现一个函数,将一个字符串中的空格替换成“ % 20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are % 20Happy. class Solution ...

  9. select 1 from table

    1.select 1 from mytable;与select anycol(目的表集合中的任意一行) from mytable;与select * from mytable 作用上来说是没有差别的, ...

  10. linux 架设 postfix 邮件服务器

    http://www.cnblogs.com/dudu/archive/2012/12/12/linux-postfix-mailserver.html