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

SCP via Python Subprocess Popen: "Identity File not accessible"

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

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

我的本地机器是 Windows 10 Enterprise。我的 'remote' 机器目前是我本地主机上的 Docker 容器 运行ning Alpine linux。

当我 运行 在提升的 shell 中使用命令时,它有效:

scp -i ./id_rsa.key -P 2222 ./test.plan root@127.0.0.1:/go/

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

SSH = subprocess.Popen(['scp', '-i ./id_rsa.key', '-P 2222', './test.plan', 'root@127.0.0.1:/go/'],
    cwd = r'C:\ThePathToThisStuff',
    shell = False,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE
)

它提示我输入密码,root@127.0.0.1's password:,当我输入密码时它抛出下面的错误,表明它找不到 RSA 密钥:

<_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', 'root@127.0.0.1:/go/'],