从 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/