基于Python实现ssh兼sftp客户端

 

by:授客 QQ:1033553122

otherTools.py

#!/usr/bin/env/ python
#
-*- coding:utf-8 -*-


__author__
=
'laifuyu'


import

os
import

subprocess

class

OtherTools:
    def

__init__(self):
        self.filepath_list
= []

#
批量创建目录

    def

mkdirs_once_many(self,
path):
        path
= os.path.normpath(path)  #
去掉路径最右侧的 \\
、/

        path
= path.replace('\\',

'/'
)

# 将所有的\\转为/,避免出现转义字符串


        head,
tail = os.path.split(path)
        new_dir_path
=
''  
#
反转后的目录路径

        root
=
''  
#根目录

        if
not

os.path.isdir(path)
and

os.path.isfile(path):  #
如果path指向的是文件,则继续分解文件所在目录

            head,
tail = os.path.split(head)

if

tail ==
''
:
            return

        while

tail:
            new_dir_path
= new_dir_path + tail +
'/'

            head,
tail = os.path.split(head)
            root
= head
        else:
            new_dir_path
= root + new_dir_path

#
批量创建目录

            new_dir_path
= os.path.normpath(new_dir_path)
            head,
tail = os.path.split(new_dir_path)
            temp
=
''

            while

tail:
                temp
= temp +
'/'

+ tail
                dir_path
= root + temp
                if
not

os.path.isdir(dir_path):
                    os.mkdir(dir_path)
                head,
tail = os.path.split(head)


# 测试

ssh_client
= MySSHClient()
ssh_client.connect(hostname='192.168.1.102',

port=22,

username='root',password='huozhe')
ssh_client.exec_command('ls
-l'
)

ssh_client.download_file('/root/dirForDownload/file',

'./test1.txt'
)
ssh_client.download_file('/root/dirForDownload/file',

'.
\test2.txt')
ssh_client.download_file('/root/dirForDownload/file',

'd:
\\test3.txt')
ssh_client.download_file('/root/dirForDownload/file',

'd:
\test4.txt')
ssh_client.download_file('/root/dirForDownload/file',

'd:\mytest4.txt'
)
ssh_client.download_file('/root/dirForDownload/file',

'd:/test5.txt'
)
ssh_client.download_file('/root/dirForDownload/file',

'd:\dir1\dir2
\test6.txt')

ssh_client.upload_file('./test1.txt','/root/test1.txt'

)
ssh_client.upload_file('d:\mytest4.txt','/root/mytestfile.txt'

)
ssh_client.upload_file('d:\dir1\dir2\test6.txt','./test6.txt'

)
ssh_client.close()

运行结果:

注意事项

# 1. 下载文件
#
1) 不支持目录级的下载,即只能下载指定的单个非目录文件
#
2) 本地目标文件路径只支持文件路径,不支持目录(比如 localpath='d:\\'),目标文件所在的上级路径可以不存在(但路径必须位于分区下)
#
比如欲下载到本地路径:d:\dir1\dir2\test.txt, d:\dir1\dir2\可以不存在
#
3) 本地目标文件支持相对路径,比如./text.txt,远程目标文件仅支持绝对路径

#
2. 上传文件
#
1) 不支持目录级的上传,只能上传指定的单个文件
#
2) 远程目标文件所在的上级路径必须存在,比如remotepath='/root/dir1/tarfile' ,其中/root/dir1必须存在
#
3) 远程目标文件、本地文件路径都支持相对路径,比如./text.txt

#
3. 重复下载文件、上传文件,会自动覆盖已经下载的文件、已上传的文件

参考文档:

http://docs.paramiko.org/en/2.4/api/channel.html

http://docs.paramiko.org/en/2.4/api/sftp.html#paramiko.sftp_client.SFTPClient

Python 基于Python实现的ssh兼sftp客户端(下)的更多相关文章

  1. Python 基于Python实现的ssh兼sftp客户端(上)

    基于Python实现的ssh兼sftp客户端   by:授客 QQ:1033553122 实现功能 实现ssh客户端兼ftp客户端:实现远程连接,执行linux命令,上传下载文件 测试环境 Win7 ...

  2. Python 基于Python及zookeeper实现简单分布式任务调度系统设计思路及核心代码实现

    基于Python及zookeeper实现简单分布式任务调度系统设计思路及核心代码实现   by:授客 QQ:1033553122 测试环境 功能需求 实现思路 代码实践(关键技术点实现) 代码模块组织 ...

  3. Python 基于python操纵zookeeper介绍

    基于python操纵zookeeper介绍 by:授客  QQ:1033553122 测试环境 Win7 64位 Python 3.3.4 kazoo-2.6.1-py2.py3-none-any.w ...

  4. Python 基于Python结合pykafka实现kafka生产及消费速率&主题分区偏移实时监控

    基于Python结合pykafka实现kafka生产及消费速率&主题分区偏移实时监控   By: 授客 QQ:1033553122   1.测试环境 python 3.4 zookeeper- ...

  5. Python 基于Python从mysql表读取千万数据实践

    基于Python 从mysql表读取千万数据实践   by:授客 QQ:1033553122 场景:   有以下两个表,两者都有一个表字段,名为waybill_no,我们需要从tl_waybill_b ...

  6. Python基于Python实现批量上传文件或目录到不同的Linux服务器

    基于Python实现批量上传文件或目录到不同的Linux服务器   by:授客 QQ:1033553122 实现功能 1 测试环境 1 使用方法 1 1. 编辑配置文件conf/rootpath_fo ...

  7. Python 基于python编写一些算法程序等

    基于python编写一些算法程序等 by:授客 QQ:1033553122 QQ群:7156436 没特意去研究,只是这对群友在QQ群里(7156436)提出的一些小程序实现.编程题,算法.问题等,本 ...

  8. Python 基于python+mysql浅谈redis缓存设计与数据库关联数据处理

    基于python+mysql浅谈redis缓存设计与数据库关联数据处理 by:授客  QQ:1033553122 测试环境 redis-3.0.7 CentOS 6.5-x86_64 python 3 ...

  9. Python 基于python操纵redis入门介绍

    基于python操纵redis入门介绍 by:授客  QQ:1033553122 测试环境 redis-3.0.7 CentOS 6.5-x86_64 python 3.3.2 基于Python操作R ...

随机推荐

  1. 机器学习入门05 - 泛化 (Generalization)

    原文链接:https://developers.google.com/machine-learning/crash-course/generalization 泛化是指模型很好地拟合以前未见过的新数据 ...

  2. Python档案袋(变量与流程控制)

    变量与运算 得到数据类型: ii=100 print(type(ii)) #输出:<class 'int'> 强制转换: ii=100 iix=str(ii) #可为int str flo ...

  3. Python的串口通信(pyserial)

    串口通信是指外设和计算机间,通过数据信号线 .地线.控制线等,按位进行传输数据的一种通讯方式.这种通信方式使用的数据线少,在远距离通信中可以节约通信成本,但其传输速度比并行传输低.串口是计算机上一种非 ...

  4. promise与async-await

    一. async/await 相对 promise 的优势 async/await 拥有更通用的作用域,使得代码有更好的易读性和可维护性. promise 由于其链式调用,每一个函数都有自己的作用域, ...

  5. ubuntu16.04 在线安装docker ce

    官方文档:https://docs.docker.com/install/linux/docker-ce/ubuntu/   ubuntu创建普通用户: adduser dk001 给该用户添加sud ...

  6. [Jenkins]Jenkins构建时提示java.io.IOException: No space left on device

    突然发现Jenkins的Job全部都停了,打开Jenkins发现所有的slave机器,均提示: 点开Dead(!),提示Thread has died,如下图: 看图好像说是Jenkins所在的服务器 ...

  7. ①泡茶看数据结构-表ADT

    前言     小朽,晚上回到寝室.烧了开水,又泡了一杯下午喝了的小毛尖.耳机听着萨克斯,总结下今天学的数据结构和算法中的表ADT.       表ADT节点: #单链表   #双链表   #循环链表 ...

  8. js变量提升与函数提升的详细过程

    大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...

  9. for循环输出树木的形状【java】

    使用for循环语句输出以下“树木”效果: * *** ***** ******* ********* * * * * * 代码: /* * *** ***** ******* ********* * ...

  10. 【原创】驱动加载之OpenService

    SC_HANDLE WINAPI OpenService( _In_ SC_HANDLE hSCManager, _In_ LPCTSTR lpServiceName, _In_ DWORD dwDe ...