【发布时间】:2018-10-10 20:18:07
【问题描述】:
从 subprocess.popen 执行 SCP 时,我收到错误:Warning: Identity File ./id_rsa.key not accessible: no such file or directory. 问题解释如下。
我想使用 python 脚本将文件从本地机器 SCP 到远程机器,并且我想指定执行 SCP 时使用的密钥(使用 -i 选项)。
我的本地计算机是 Windows 10 企业版。我的“远程”机器目前是在我的本地主机上运行 Alpine linux 的 Docker 容器。
当我在提升的 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']
我尝试过提供键的绝对路径,将参数列表中的每个命令分成选项和值、不同的分组等。
【问题讨论】:
-
./id_rsa.key位于您当前的目录中。那是什么print(os.getcwd())?另外,您当前用户的名称是什么,您从哪里获得 scp 可执行文件? -
感谢您的参与。下面接受的答案解决了这个问题。
标签: python python-3.x subprocess popen scp