Python 基于Python实现的ssh兼sftp客户端(下)
基于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
作者:授客
微信/QQ:1033553122
全国软件测试QQ交流群:7156436
Git地址:https://gitee.com/ishouke
友情提示:限于时间仓促,文中可能存在错误,欢迎指正、评论!
作者五行缺钱,如果觉得文章对您有帮助,请扫描下边的二维码打赏作者,金额随意,您的支持将是我继续创作的源动力,打赏后如有任何疑问,请联系我!!!
微信打赏
支付宝打赏 全国软件测试交流QQ群
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库