在Python中不使用FTP读取远程SSH服务器上的tar文件

在Python中不使用FTP读取远程SSH服务器上的tar文件,python,ssh,ftp,tar,Python,Ssh,Ftp,Tar,我正在远程服务器上创建一些tar文件,我希望能够将它放到我的机器上。由于安全原因,我无法在该服务器上使用FTP 因此,在我看来,我有两个选择: 以其他方式获取文件(作为文件),然后使用tarfile库-如果是这样,我需要在不使用FTP的情况下获取文件的帮助 获取文件的内容,然后将其解压缩 如果还有别的办法,我想听听 import spur #creating the connection shell = spur.SshShell( hostname=unix_host, us

我正在远程服务器上创建一些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
            )
    
    更多信息:
    我的项目正在VirtualNV上运行。我使用的是Python3.4。

    如果您有SSH访问权限,那么您有99%的SFTP访问权限

    因此,您可以使用SFTP下载该文件。看


    或者使用spur后,请查看其:

    例如,要通过SSH复制二进制文件,假设您已经有一个
    SshShell
    的实例:

    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
    方法在引擎盖下使用SFTP(通过Paramiko库)。

    如果您有ssh,那么您可能有scp或SFTP。不要使用
    missing\u host\u key=spur.ssh.MissingHostKey.accept
    !你正在失去对敌人的保护!