pysftp-tools
import pysftp
import paramiko
import os
import unittest
import json
import warnings
warnings.filterwarnings("ignore") class SftpUtils(object):
"""
cmd: execute shell command on remote linux server.
exist_file : if remote path exists on remote return True ,else return False.
exist_dir : if remote path exists on remote return True ,else return False.
is_dir: if remote path is dir return True ,else return False.
is_file : if remote path is file return True ,else return False.
remove: remove the remote file on remote linux .
rm: remove dir and all files/dir under remote dir and dir it self.
mkdir: mkdir dir on remote linux but it parent dir must exists ,if it already exist delete remote dir exists first and mkdir remote dir .
makedirs: mkdir the dir on remote linux , if it parent dir and dir self not exists,
it will mkdir parent dir and dir, else mkdir dir.
get: get remote linux file to local ,local filename and remote filename must absolute filepath.
put: upload local file to remote linux local filename and remote filename must absolute filepath.
rename:
rename a file or directory on the remote host
:param str remote_src: the remote file/directory to rename
:param str remote_dest: the remote file/directory to put it
open: read remote file content and return readlines list
cwd : cd curdir to newdir
getcwd: get remote linux cursor current dir path and return it
chmod : change file r,w,x grants.
chown: change file or dir owner
opens: read and write file on remote linux ,if remote file not exists it will create it first when write .
exists: if dir or file exists on remote linux return True ,else return False .
list_dir: query all files under dir ,the same with shell ls -l -a dir/ .
ger_recursive:
recursive copy remote files or dir under remote dir to localdir put_recursive: recursive copy localdir's all file or dir under it to remote dir
""" def __init__(self, ip, port, user, pwd):
self.ip = ip
self.port = port
self.user = user
self.pwd = pwd
self.sftp = self.sftp_client() def sftp_client(self):
# https://pysftp.readthedocs.io/en/release_0.2.9/cookbook.html#pysftp-cnopts
# private_key="/home/app/privatekey_file"
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
sftp = pysftp.Connection(host=self.ip, username=self.user, password=self.pwd, port=self.port, cnopts=cnopts)
return sftp def cmd(self, shell_cmd: str):
"""get stdout or err""" std = self.sftp.execute(shell_cmd)
if std:
readlines = ''
for line in std:
readlines = readlines + line.decode("utf-8")
return readlines def exists_file(self, filepath):
try:
self.sftp.stat(filepath)
except Exception:
return False
else:
return True def exists_dir(self, dirpath):
try:
self.sftp.stat(dirpath)
except Exception:
return False
else:
return True def is_dir(self, dirpath): return True if self.sftp.isdir(remotepath=dirpath) else False def is_file(self, filepath):
return True if self.sftp.isfile(remotepath=filepath) else False def remove(self, filepath):
"""remove remote file"""
if self.exists_file(filepath):
self.sftp.remove(filepath)
else:
raise FileNotFoundError("%s file not find on remote ! " % filepath) def rm(self, dirpath):
""" rmdir just delete empty dirs """
if self.exists_dir(dirpath):
files = self.sftp.listdir(dirpath)
for file in files:
filepath = os.path.join(dirpath, file).replace("\\",'/')
if self.is_dir(filepath):
self.rm(filepath)
else:
self.remove(filepath)
self.sftp.rmdir(dirpath)
else:
raise FileNotFoundError("%s dir not find on remote !" % dirpath) def mkdir(self, remote_dir):
"""parent dir must exists"""
if self.exists(remote_dir):
self.rm(remote_dir)
self.sftp.mkdir(remotepath=remote_dir)
else:
self.sftp.mkdir(remotepath=remote_dir) def mkdirs(self, remote_dir):
"""mkdir -p /usr/local/mkdir,it can create dir that parent dirs not exists,if exists,it can also create"""
self.sftp.makedirs(remotedir=remote_dir) def get(self, remote_path, local_path):
"""sftp get,download """
self.sftp.get(remotepath=remote_path, localpath=local_path, callback=None) def put(self, local_path, remote_path):
"""sftp put,upload """
self.sftp.put(localpath=local_path, remotepath=remote_path) def rename(self, remote_path, new_name_path):
"""rename file or dir on remote """
self.sftp.rename(remote_src=remote_path, remote_dest=new_name_path) def open(self, remote_filepath, mode):
"""open 'r+' or 'r' file ,return str"""
readlines = self.sftp.open(remote_file=remote_filepath, mode=mode)
return list(readlines) def getcwd(self):
return self.sftp.getcwd() def cwd(self, remote_path):
"""change dir to given remote_path"""
return self.sftp.cwd(remotepath=remote_path) def chmod(self, remote_path, mode: int):
""" change file grants for w,r,x"""
self.sftp.chmod(remotepath=remote_path, mode=mode) def chown(self, remote_path, uid: int, gid: int):
"""change owner of user and group """
self.sftp.chown(remote_path, uid, gid) def chdir(self, remote_path):
"""cwd()== chdir()"""
self.sftp.chdir(remote_path) def touch(self, filepath):
"""if exists ,pass it or raise exception """
if self.exists_file(filepath):
self.remove(filepath)
self.sftp_client().execute("touch %s" % filepath)
else:
self.sftp_client().execute("touch %s" % filepath) def close(self):
self.sftp.close() def opens(self, filepath, mode, file_data=None):
"""file remote read and write ,return str"""
tp = paramiko.Transport(self.ip, self.port)
tp.connect(username=self.user, password=self.pwd)
pipe = paramiko.sftp_client.SFTPClient.from_transport(tp)
if mode == "w" or mode == "w+":
with pipe.open(filepath, mode) as f:
f.write(file_data)
else:
if mode == "r" or mode == "r+":
with pipe.open(filepath, mode)as f:
return f.read().decode("utf-8")
def exists(self,remotepath):
return True if self.sftp.exists(remotepath) else False def list_dir(self,dir):
return self.sftp.listdir(dir) def get_recursive(self,remote_dir,localpdir,):
"""
preserve modification time on files if True keep modify file or dir last operate time ,else False
localpdir : the parent dir path of local receive linux dir
remote_dir : the target of copy remote_dir ,the result dir name is same with remote
"""
remote_pdir=os.path.dirname(remote_dir)
# local_pdir = os.path.dirname(localdir)
remote_target_dir=os.path.split(remote_dir)[-1]
self.sftp.chdir(remote_pdir) #cd remote_pdir
if self.exists(localpdir):
self.sftp.get_r(remote_target_dir,localpdir,preserve_mtime=False)
else:
#local create localdir pdir that not exisit
os.makedirs(localpdir)
self.sftp.get_r(remote_target_dir, localpdir, preserve_mtime=False) def put_recursive(self,localdir,remote_pdir,preserve_mtime=False):
"""
remote_pdir: the parent dir of receive local targetdir ,if parent exists copy target same
target dirname diredctly to remote_dir,if parent dir not exists ,first makedirs parent dir of remote_pdir,then copy same name
target dirname to remote_pdir
localdir: local target dir absolute dir path
"""
local_targetdir = os.path.split(localdir)[-1]
remote_receivedir = os.path.join(remote_pdir, local_targetdir)
local_pdir=os.path.dirname(localdir) os.chdir(local_pdir) #cd local pdir
if self.sftp.exists(remote_receivedir):
self.sftp.put_r(local_targetdir,remote_receivedir)
else:
#remote create pdir not exists self.sftp.makedirs(remote_receivedir)
self.sftp.put_r( local_targetdir,remote_receivedir,preserve_mtime)
def local_rm(dirpath):
if os.path.exists(dirpath):
files = os.listdir(dirpath)
for file in files:
filepath = os.path.join(dirpath, file).replace("\\",'/')
if os.path.isdir(filepath):
local_rm(filepath)
else:
os.remove(filepath)
os.rmdir(dirpath) class TestSftp(unittest.TestCase): @classmethod
def setUpClass(cls):
cls.sftp=SftpUtils(ip="192.168.110.151",port=22,user="root",pwd="admin")
@classmethod
def tearDownClass(cls):
pass def test_cmd(self):
shell="ip addr "
readlines=self.sftp.cmd(shell)
self.assertIn("192.168.110.151",readlines) def test_touch(self):
path="/usr/local/test.txt"
self.sftp.touch(path)
res=self.sftp.exists(path)
self.assertEqual(res,True)
r=self.sftp.is_file(path)
self.assertEqual(r,True)
self.sftp.remove(path)
res2 = self.sftp.exists(path)
self.assertEqual(res2, False)
def test_isdir(self):
r=self.sftp.is_dir("/usr/local")
self.assertEqual(r,True)
k=self.sftp.is_dir("/fff/fcdh")
self.assertEqual(k,False)
def test_isfile(self):
r="/usr/tets.txt"
res=self.sftp.is_file(r)
self.assertEqual(res,False)
def test_mkdir(self):
self.sftp.mkdir("/usr/local/testmkdir")
r=self.sftp.exists("/usr/local/testmkdir")
self.assertEqual(r,True)
# self.sftp.rm("/usr/local/testmkdir") def test_makedirs(self):
path="/usr/local/makedirs/mk1"
self.sftp.mkdirs(path)
r=self.sftp.exists(path)
self.assertEqual(r,True) def test_rm(self):
path="/usr/local/testrm/rmdir/"
self.sftp.mkdirs(path)
files=self.sftp.list_dir("/usr/local/testrm")
self.assertIn('rmdir',files) self.sftp.touch(path+"test.txt")
self.sftp.rm("/usr/local/testrm")
r=self.sftp.exists(path)
self.assertEqual(r,False)
def test_opens(self):
path="/usr/local/test.txt"
data={"k":99,"v":{"a":9990},"dd":0,"oo":6556}
self.sftp.opens(path,'w+',json.dumps(data))
lines=self.sftp.open(path,'r') def test_get_r(self):
remote="/usr/local/listdir"
local="/usr/local/ttt"
# self.sftp.chdir("/usr/local")
# get current dir
# print(self.sftp.getcwd())
self.sftp.get_recursive(remote,local)
local_rm(local) def test_put_r(self):
local="/usr/local/upload/listdir"
remote="/usr/local/rrre"
self.sftp.put_recursive(local,remote)
self.sftp.rm(remote) if __name__ == '__main__':
unittest.main(verbosity=2)
测试:
python -m unittest sftp_tools.TestSftp
[root@hostuser local]# sh sftptool.sh
..........
----------------------------------------------------------------------
Ran 10 tests in 3.017s
OK
pysftp-tools的更多相关文章
- 解决 Could not find com.android.tools.build:gradle 问题
今天拉同事最新的代码,编译时老是报如下错误: Error:Could not find com.android.tools.build:gradle:2.2.0.Searched in the fol ...
- 免费的精品: Productivity Power Tools 动画演示
Productivity Power Tools 是微软官方推出的 Visual Studio 扩展,被用以提高开发人员生产率.它的出现一定程度上弥补和完善了 Visual Studio 自身的不足, ...
- 2.Kali安装VMware tools(详细+异常处理)
dnt@MT:~$ cd /media/cdrom0 进入光驱内 dnt@MT:/media/cdrom0$ ls 查看当前目录下有哪些内容manifest.txt run_upgrader.sh V ...
- 第三篇:Entity Framework CodeFirst & Model 映射 续篇 EntityFramework Power Tools 工具使用
上一篇 第二篇:Entity Framework CodeFirst & Model 映射 主要介绍以Fluent API来实作EntityFramework CodeFirst,得到了大家一 ...
- Chrome Developer Tools:Timeline Panel说明
一.Timeline panel 概况 Timeline工具栏可以详细检测出Web应用在加载过程中,时间花费情况的概览.这些应用包括下载资源,处理DOM事件, 页面布局渲染或者向屏幕绘制元素. 如下图 ...
- linux-虚拟机centos6.5安装tools
1.在VMWare选VM -> Install VMWare Tools-,就会在桌面出现VMWare Tools图示让你安裝 2.进入CentOS桌面后,将光盘打开,看到了VMWareTool ...
- VMWare Tools 和 Shared folder(共享文件夹)
转自: http://www.51testing.com/html/38/225738-143732.html 使用vmwar下shared folders功能实现vmware中host与ghost间 ...
- 用hibernate tools生成对应的sql应用代码
参考资料: eclipse在线配置hibernate tools http://jingyan.baidu.com/article/db55b609959d154ba20a2f5d.html [图]H ...
- 解决Maven工程中报 Missing artifact jdk.tools:jdk.tools:
jdk.tools:jdk.tools是与JDK一起分发的一个JAR文件,可以如下方式加入到Maven项目中:<dependency> <groupId>jdk.tool ...
- android中xml tools属性详解
第一部分 安卓开发中,在写布局代码的时候,ide可以看到布局的预览效果. 但是有些效果则必须在运行之后才能看见,比如这种情况:TextView在xml中没有设置任何字符,而是在activity中设置了 ...
随机推荐
- nodemon:无法将“nodemon”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。
主要问题还是你的终端(也就是你的nodemon命令执行的地方没有这个环境) 解决方法:设置全局的nodemon,在终端执行 npm install -g nodemon
- HCTF2018-admin[条件竞争]
附上网上师傅的wp 学习链接: https://www.jianshu.com/p/f92311564ad0 按照师傅的wp复现一下: 源代码审计,去看路由里的login函数和change函数都在没 ...
- SpringBoot学习- 6、MetaData描述无法tip显示问题
SpringBoot学习足迹 (一)先说说现象 1.在application.properties可以定义属性并在MetaData中增加description 如 生成additional-sprin ...
- vue学习指南:第十三篇(详细) - Vue的 路由 第三篇 ( 路由的缓存 )
路由的缓存 路由缓存是 Vue组件优化的一个重要方法 为什么实现路由缓存? 为了 组件间 相互切换不会重复加载数据,影响用户体验,我们通常需要将组件的数组实现缓存,当我们点过来,在点的时候会再次发送 ...
- word中去除所有table键
1.打开word---文件---选项---显示,不勾选制表符 2.选择需要的文本,按[CTRL+H]查找替换, 3.将鼠标定位至“查找内容”,点击“特殊格式-制表符”,然后出现^t,在“替换为”中输入 ...
- C#.net连接Sybase的方法
一 .ODBC方式连接 1 安装Sybase客户端,安装ODBC驱动,配置DSN<略> 2 连接代码 string strconn = "DSN=TEST;SRVR=TEST;D ...
- laravel Type error: Argument 2 passed to Illuminate\Routing\UrlGenerator::__construct() must be an instance of Illuminate\Http\Request
第一种情况: 传递给 UrlGenerator 的第二个参数是 Request 实例 你传进去的是null, 可以检查 config文件夹下的配置文件是否有用到 url() 函数的,如果有,将 url ...
- 电脑出现kernelbase.dll错误的两种解决方法
KernelBase.dll是Windows操作系统的重要文件,它为各种应用程序提供服务.如果电脑提示kernelbase.dll错误,这该怎么处理?大家可以用电脑自带的防火墙或者是第三方软件来进行故 ...
- 刷题75. Sort Colors
一.题目说明 题目75. Sort Colors,给定n个整数的列表(0代表red,1代表white,2代表blue),排序实现相同颜色在一起.难度是Medium. 二.我的解答 这个是一个排序,还是 ...
- leetcode 64. 最小路径和 动态规划系列
目录 1. leetcode 64. 最小路径和 1.1. 暴力 1.2. 二维动态规划 2. 完整代码及执行结果 2.1. 执行结果 1. leetcode 64. 最小路径和 给定一个包含非负整数 ...