在Python中不使用FTP的情况下读取远程SSH服务器上的tar文件
问题描述:
我在远程服务器上创建了一些tar文件,我希望能够将它提供给我的机器。由于安全原因,我无法在该服务器上使用FTP。在Python中不使用FTP的情况下读取远程SSH服务器上的tar文件
所以,我怎么看它,我有两个选择:
得到的文件(如文件),在一些其他的方式,然后使用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来下载文件。见Download files over SSH using Python。
,或者一旦你使用的冲动,看到它的SshShell.open
method:
例如,复制通过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库)。
+1
非常感谢!它的工作原理是 。 –
如果你有ssh,那么也许你有scp或sftp。 –
请勿使用'missing_host_key = spur.ssh.MissingHostKey.accept'!你正在失去抵御[中间人攻击]的保护(https://en.wikipedia.org/wiki/Man-in-the-middle_attack)! –