在 Python 中不使用 FTP 读取远程 SSH 服务器上的 tar 文件
Read tar file on remote SSH server without using FTP in Python
我正在远程服务器上创建一些 tar 文件,我希望能够将它传送到我的机器上。由于安全原因,我无法在该服务器上使用 FTP。
所以我怎么看,我有两个选择:
以其他方式获取文件(作为文件),然后使用 tar 文件库 - 如果是这样,我需要帮助获取没有 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下载文件了。参见 。
或者一旦你使用了 spur,请查看它的 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)
SshShell.open
方法在后台使用 SFTP(通过 Paramiko 库)。
我正在远程服务器上创建一些 tar 文件,我希望能够将它传送到我的机器上。由于安全原因,我无法在该服务器上使用 FTP。
所以我怎么看,我有两个选择:
以其他方式获取文件(作为文件),然后使用 tar 文件库 - 如果是这样,我需要帮助获取没有 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下载文件了。参见
或者一旦你使用了 spur,请查看它的 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)
SshShell.open
方法在后台使用 SFTP(通过 Paramiko 库)。