1

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.

2
  • if you have ssh, then maybe you have scp or sftp. Commented Mar 21, 2017 at 9:41
  • Don't use missing_host_key=spur.ssh.MissingHostKey.accept! You are losing protection against Man-in-the-middle attacks! Commented Mar 21, 2017 at 10:06

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.