基于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. No bean named 'xxxxx' is defined异常,已解决,这个坑很难发现,你get了吗

    出现No bean named 'xxxxx' is defined异常 没有定义名为xxx的bean 如果你的代码写的都对,根本问题只有一个地方出错了,那就是你的 basePackage=的包名路径 ...

  2. 【spring】task 任务调度(定时任务)

    1.定时任务的几种实现可以看这里:http://gong1208.iteye.com/blog/1773177 2.需要导入spring的jar包,可以参看之前的[spring]相关文章 3.这里使用 ...

  3. Kubernetes集群搭建之Master配置篇

    本次系列使用的所需部署包版本都使用的目前最新的或最新稳定版,安装包地址请到公众号内回复[K8s实战]获取 今天终于到正题了~~ 生成kubernets证书与私钥 1. 制作kubernetes ca证 ...

  4. HBase之HRegionServer启动(含与HMaster交互)

    在我的博文<HBase——HMaster启动之一>.<HBase——HMaster启动之二>中已经详细介绍过HMaster在启动过程中调用的各种方法.下面,单就HRegionS ...

  5. Python xlrd xlwt 读取写入Excel.

    import xlrd import xlwt #读取 xlrd.Book.encoding = "gbk" wb = xlrd.open_workbook(filename='s ...

  6. Django -- 发送HTML格式的邮件

    提前在setting中设置邮箱server from django.core.mai import EmailMessage subject, from_email, to = 'xxx', 'nor ...

  7. Centos7单机部署ELK+x-pack

    ELK分布式框架作为现在大数据时代分析日志的常为大家使用.现在我们就记录下单机Centos7部署ELK的过程和遇到的问题. 系统要求:Centos7(内核3.5及以上,2核4G) elk版本:6.2. ...

  8. [ Talk is Cheap Show me the CODE ] : jQuery Mobile页面布局

    当我们专注地研究人类生活的空虚,并考虑荣华富贵空幻无常时,也许我们正在阿谀逢迎自己懒惰的天性. Written In The Font 为了app的手机端,我选择了 jQuery Mobile  ,学 ...

  9. 项目ITP(四) javaweb http json 交互 in action (服务端 spring 手机端 提供各种工具类)勿喷!

    前言 系列文章:[传送门] 洗了个澡,准备写篇博客.然后看书了.时间 3 7 分.我慢慢规律生活,向目标靠近.  很喜欢珍惜时间像叮当猫一样 正文 慢慢地,二维码实现签到将要落幕了.下篇文章出二维码实 ...

  10. Windows2008/2012/2016多用户同时远程连接终端服务授权

    win2016多用户登录: 添加角色“远程桌面服务”,子角色“远程桌面会话主机”和“远程桌面授权”,重启 远程桌面授权,激活服务器,企业协议,协议号6565792,授权模式“每用户” 本地策略管理器g ...