简体   繁体   中英

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

I am creating some tar file on a remote server and I want to be able to get it to my machine. Due to security reasons I can't use FTP on that server.

So how I see it, I have two options:

  1. get the file (as file) in some other way and then use tarfile library - if so, I need help with getting the file without FTP.

  2. get the content of the file and then extract it.

If there is another way, I would like to hear it.

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
        )

More info: My project is running on a virtualenv. I am using Python 3.4.

If you have SSH access, you have SFTP access for 99%.

So you can use the SFTP to download the file. See Download files over SSH using Python .


Or once you are using spur, see its SshShell.open method :

For instance, to copy a binary file over SSH, assuming you already have an instance of 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) 

The SshShell.open method uses SFTP under the hood (via Paramiko library).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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