[英]Read tar file on remote SSH server without using FTP in Python
我正在远程服务器上创建一些tar文件,我希望能够将其下载到我的计算机上。 出于安全原因,我无法在该服务器上使用FTP。
因此,我有两种选择:
以其他方式获取文件(作为文件),然后使用tarfile库-如果是这样,我需要在没有FTP的情况下获取文件的帮助。
获取文件的内容,然后将其提取。
如果还有其他方法,我想听听。
import spur
#creating the connection
shell = spur.SshShell(
hostname=unix_host,
username=unix_user,
password=unix_password,
missing_host_key=spur.ssh.MissingHostKey.accept
)
# running ssh command that is creating a tar file on the remote server
with shell:
command = "tar -czvf test.gz test"
shell.run(
["sh", "-c", command],
cwd=unix_path
)
# getting the content of the tar file to gz_file_content
command = "cat test.gz"
gz_file_content = shell.run(
["sh", "-c", command],
cwd=unix_path
)
更多信息:我的项目在virtualenv上运行。 我正在使用Python 3.4。
如果您具有SSH访问权限,则您有99%的SFTP访问权限。
因此,您可以使用SFTP下载文件。 请参阅使用Python通过SSH下载文件 。
或一旦使用spur,请查看其SshShell.open
方法 :
例如,假设您已经有
SshShell
实例,SshShell
通过SSH复制二进制文件:with ssh_shell.open("/path/to/remote", "rb") as remote_file: with open("/path/to/local", "wb") as local_file: shutil.copyfileobj(remote_file, local_file)
SshShell.open
方法在SshShell.open
使用SFTP(通过Paramiko库)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.