通过 Python 中的 scp 和 os 模块从远程服务器安全复制文件 [英] Secure Copy File from remote server via scp and os module in Python

查看:61
本文介绍了通过 Python 中的 scp 和 os 模块从远程服务器安全复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Python 和编程还很陌生.我正在尝试通过 python 脚本在两台计算机之间复制文件.但是代码

I'm pretty new to Python and programming. I'm trying to copy a file between two computers via a python script. However the code

os.system("ssh " + hostname + " scp " + filepath + " " + user + "@" + localhost + ":" cwd)

行不通.我认为它需要一个密码,如 如何使用 SCP 或 SSH 在 Python 中将文件复制到远程服务器?.我没有收到任何错误日志,文件只是不会显示在我当前的工作目录中.

won't work. I think it needs a password, as descriped in How to copy a file to a remote server in Python using SCP or SSH?. I didn't get any error logs, the file just won't show in my current working directory.

然而,所有其他带有 os.system("ssh " + hostname + "command")os.popen("ssh " + hostname + "command") 的命令 确实有效.-> command = 例如ls

However every other command with os.system("ssh " + hostname + "command") or os.popen("ssh " + hostname + "command") does work. -> command = e.g. ls

当我尝试ssh 主机名 scp 文件 user@local:directory在命令行中,它无需输入密码即可工作.

When I try ssh hostname scp file user@local:directory in the commandline it works without entering a password.

我尝试将 os.popen 命令与 getpass 和 pxssh 模块结合起来建立到远程服务器的 ssh 连接并使用它直接发送命令(我只测试了一个简单的命令):

I tried to combine os.popen commands with getpass and pxssh module to establish a ssh connection to the remote server and use it to send commands directly (I only tested it for an easy command):

import pxssh
import getpass

ssh = pxssh.pxssh()
ssh.force_password = True
hostname = raw_input("Hostname: ")
user = raw_input("Username: ")
password = getpass.getpass("Password: ")
ssh.login(hostname, user, password)
test = os.popen("hostname")
print test

但是我无法将命令传递到远程服务器(print test 显示,该主机名 = 本地而不是远程服务器),但是我确定连接已建立.我认为建立连接比在 bash 命令中总是使用 "ssh " + hostname 更容易.我还尝试了 如何使用 SCP 或 SSH 在 Python 中将文件复制到远程服务器?,但我必须承认,由于缺乏经验,我没有让它们工作.

But I'm not able to put commands through to the remote server (print test shows, that hostname = local and not the remote server), however I'm sure, the conection is established. I thought it would be easier to establish a connection than always use "ssh " + hostname in the bash commands. I also tried some of the workarounds in How to copy a file to a remote server in Python using SCP or SSH?, but I must admit due to lack of expirience I didn't get them to work.

非常感谢您帮助我.

推荐答案

我认为最简单(避免必须输入密码)和最安全的方法是首先设置 公钥/私钥认证.完成后,您可以通过执行 ssh user@hostname 登录到远程系统,下面的 bash 命令可以解决问题:

I think the easiest (to avoid having to enter a password) and most secure way to go about this is to first set public/private key authentication. Once that is done, and you can log in to the remote system by doing ssh user@hostname, the following bash command would do the trick:

scp some/complete/path/to/file user@remote_system:some/remote/path

对应的 Python 代码为:

The corresponding Python code would be:

import subprocess

filepath = "some/complete/path/to/file"
hostname = "user@remote_system"
remote_path = "some/remote/path"

subprocess.call(['scp', filepath, ':'.join([hostname,remote_path])])

这篇关于通过 Python 中的 scp 和 os 模块从远程服务器安全复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆