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. Android RIL Log

    转载: 要调试 RIL,最好的方法就是打开 radio的log: $ adb logcat -b radio 最好加上 log语法亮度工具coloredlogcat.py ,一些常见的LOG TAG要 ...

  2. 关于springboot启动时候报错:springboot Failed to parse configuration class [Application]

    把运行的java类放在一个package下后就不再提示这个错误. 使用的ide是intellij,之前也有因为没有创建package报错的经历,可能这是intellij必须的

  3. C++设计模式-参考资料

    设计模式实例讲解: http://www.cnblogs.com/jiese/tag/ http://www.cnblogs.com/wanggary/category/294620.html 设计模 ...

  4. struts2 result type的类型

    一共十种类型 1.dispatcher 默认的类型,相当于servlet的foward,服务器端跳转.客户端看到的是struts2中配置的地址,而不是真正页面的地址.一般用于跳转到jsp页面 2.re ...

  5. JPA 系列教程11-复合主键-2个@Id

    复合主键 指多个主键联合形成一个主键组合 需求产生 比如航线一般是由出发地及目的地确定,如果要确定唯一的航线就可以用出发地和目的地一起来表示 ddl语句 CREATE TABLE `t_airline ...

  6. SharedPreferences的工具类

    import android.content.Context; import android.content.SharedPreferences; import android.content.Sha ...

  7. 用VS2012或VS2013在win7下编写的程序在XP下运行就出现“不是有效的win32应用程序

    经常创建项目的时候,采用空项目,那么编译成功后,最好修改下sbusystem . 链接器->系统->子系统->控制台或windows 后面的"最低版本"是5.01

  8. Java编译成功,用java 运行class时出现错误解决方法

    java -classpath class file's address; filename

  9. split a string in C++

    This is my favourite way to iterate through a string. You can do what you want per word. string line ...

  10. ios 控件 UIButton

    - (void)setTitle:(NSString *)title forState:(UIControlState)state; //设置标题 - (void)setTitleColor:(UIC ...