python - SCP 通过 Python 子进程 Popen : "Identity File not accessible"

标签 python python-3.x subprocess popen scp

从 subprocess.popen 执行 SCP 时,我收到错误:Warning: Identity File ./id_rsa.key not accessible: no such file or directory.问题解释如下。

我想使用 python 脚本将文件从本地计算机 SCP 发送到远程计算机,并且我想指定执行 SCP 时使用的 key (使用 -i 选项)。

我的本​​地计算机是 Windows 10 企业版。我的“远程”机器当前是一个在我的本地主机上运行 Alpine linux 的 Docker 容器。

当我在提升的 shell 中运行该命令时,它可以工作:

scp -i ./id_rsa.key -P 2222 ./test.plan <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dfadb0b0ab9feeede8f1eff1eff1ee" rel="noreferrer noopener nofollow">[email protected]</a>:/go/

当我通过 Python 3.6 subprocess.popen 执行命令时:

SSH = subprocess.Popen(['scp', '-i ./id_rsa.key', '-P 2222', './test.plan', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b3c1dcdcc7f38281849d839d839d82" rel="noreferrer noopener nofollow">[email protected]</a>:/go/'],
    cwd = r'C:\ThePathToThisStuff',
    shell = False,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE
)

它提示我输入密码,<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0d7f6262794d3c3f3a233d233d233c" rel="noreferrer noopener nofollow">[email protected]</a>'s password: ,当我输入密码时,它会抛出以下错误,表明它无法找到 RSA key :

<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'> ERROR: [b'Warning: Identity file  ./id_rsa.key not accessible: No such file or directory.\n']

我尝试提供键的绝对路径,将参数列表中的每个命令分为选项和值、不同的分组等。

最佳答案

您不应将选项及其参数组合在同一个参数中,它们需要是单独的列表元素。

SSH = subprocess.Popen(['scp', '-i', './id_rsa.key', '-P', '2222', './test.plan', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b6974746f5b2a292c352b352b352a" rel="noreferrer noopener nofollow">[email protected]</a>:/go/'],

关于python - SCP 通过 Python 子进程 Popen : "Identity File not accessible",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50105545/

相关文章:

python - 与 ARRAY 列不同的值

python - 使用部分日期时间索引连接数据框

python - 如何确保用户输入了数字?

Python Popen 写入 stdin 在线程中时不起作用

python - 我可以在 Python 中将打开的 gzip 文件与 Popen 一起使用吗?

python - 使用 numpy.where 向量化 for 循环

django - 可以在 Firebase 中托管 Django 项目吗?如果是的话,给我一个想法

python - 从 R 到 Python : define multiple columns from multiple columns in a pandas dataframe

python - args 参数的 subprocess.Popen 最大长度是多少?

python - bash/cygwin/$路径 : Do I really have to reboot to alter $PATH?