繁体   English   中英

无需使用Python中的FTP即可读取远程SSH服务器上的tar文件

[英]Read tar file on remote SSH server without using FTP in Python

我正在远程服务器上创建一些tar文件,我希望能够将其下载到我的计算机上。 出于安全原因,我无法在该服务器上使用FTP。

因此,我有两种选择:

  1. 以其他方式获取文件(作为文件),然后使用tarfile库-如果是这样,我需要在没有FTP的情况下获取文件的帮助。

  2. 获取文件的内容,然后将其提取。

如果还有其他方法,我想听听。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM