赞
踩
用 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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。