用 sshtunnel 跳转
用paramiko的SFTP get或put整个目录
连接远程服务器
from paramiko import SFTPClient, Transport
from sshtunnel import SSHTunnelForwarder
server = SSHTunnelForwarder(
(host,port), # 跳板机
ssh_username = name,
ssh_password = passwd,
remote_bind_address=(host,22))# 远程服务器
server.start()# 开启隧道
transport = Transport(('127.0.0.1',server.local_bind_port))
transport.connect(username=name,password=passwd)
sftp = SFTPClient.from_transport(transport)
传输文件
## 上传文件
sftp.put(local_path,remote_path)
## 下载文件
sftp.get(remote_path,local_path)
## 列出目录及文件
sftp.listdir(remote_path)
获取远端上指定目录及其子目录下的所有文件
def get_allremote_files(sftp,remote_path):
allfiles = []
files = sftp.listdir(remote_path)
for fp in files:
fp = remote_path + '/' + fp
stat = sftp.stat(fp)
if stat.st_mode == 33188: ## 文件
allfiles.append(fp)
elif stat.st_mode == 16877: ## 目录
allfiles.extend(get_allremote_files(fp))
return allfiles