【问题标题】:Why Can't I Call an SSH Terminal Command from Python?为什么我不能从 Python 调用 SSH 终端命令?
【发布时间】:2020-04-18 19:14:37
【问题描述】:

我正在尝试通过 SSH 将文件复制到我的 Raspberry pi,并且我想从 python 执行 windows 终端命令,以便稍后将其自动化。但是,每次尝试执行它时都会出错。当我手动将 SSH 命令放入控制台时,SSH 命令按预期工作,但当我从该脚本调用它时它不起作用。我以前从未在 python 中调用过控制台命令,所以我正在尝试使用我发现的其他一些线程。我可能做错了什么?我还尝试运行os.system,它没有返回错误,但也没有按应有的方式执行命令。我正在使用 Python 3.8.1。这是我的脚本和错误,感谢您的帮助。

代码:

import subprocess

subprocess.run(['scp <text file path> pi@<IP>:here/'])
print ("done")

错误

Traceback (most recent call last):
  File "<script file path>.py", line 3, in <module>
    subprocess.run(['scp <text file path>.txt pi@<IP>:here/'])
  File "<python file path>\Python38-32\lib\subprocess.py", line 489, in run
    with Popen(*popenargs, **kwargs) as process:
  File <python file path>\Python38-32\lib\subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File <python file path>\Python38-32\lib\subprocess.py", line 1307, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified

编辑:这是我在拆分这样的一系列命令时遇到的错误:

代码:

import subprocess

subprocess.run(['scp', '<text file path>.txt', 'pi@<IP>:here/'])
print ("done")

错误:

Traceback (most recent call last):
  File "<script file path>.py", line 3, in <module>
    subprocess.run(['scp', '<text file path>.txt', 'pi@<IP>:here/'])
  File "<python file path>\Python38-32\lib\subprocess.py", line 489, in run
    with Popen(*popenargs, **kwargs) as process:
  File "<python file path>\Python38-32\lib\subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File <Python file path>\Python38-32\lib\subprocess.py", line 1307, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified

当然,我在 中输入的任何内容都是为了保护我的信息。

编辑:运行方法:

【问题讨论】:

  • 您确定&lt;text file path&gt; 存在吗?
  • 只是拆分参数:['scp', '&lt;text file path&gt;', 'pi@&lt;IP&gt;:here/']。现在它正在寻找具有这个长名称的可执行文件,包括空格和 args
  • 感谢您的快速回复。是的,我确信文件路径存在,因为将其直接复制并粘贴到命令提示符中是可行的。我尝试拆分它,但我得到了同样的错误。
  • 我同意@Marat 的评论。阅读subprocess 的文档 参数通常应该分开。如果之后您有任何其他错误,请提供确切的错误消息/输出
  • 当我分离参数时,我得到完全相同的错误,除了我拆分的任何参数,它们也会显示拆分。例如,在错误消息的第三行,而不是说 subprocess.run(['scp &lt;text file path&gt; pi@&lt;IP&gt;:here/']) 它会说 subprocess.run(['scp', '&lt;text file path&gt;', 'pi@&lt;IP&gt;:here/'])

标签: python shell ssh command-line subprocess


【解决方案1】:

要么删除列表,让 shell 解析字符串

import subprocess

subprocess.run('scp <text file path> pi@<IP>:here/', shell=True)
print ("done")

或者你自己把你的命令拆分成它的组成部分

import subprocess

subprocess.run(['scp', <text file path>, 'pi@<IP>:here/'])
print ("done")

至于您的错误,&lt;text file path&gt; 是不正确的相对路径(给定当前工作目录)或scp 不在您的PATH 中。从错误信息中并不清楚找不到哪个文件。

【讨论】:

  • 感谢您的回复。这两个我都试过了。第一个问题与我的另一个问题的结果相同,第二个问题与我在这个问题中概述的错误相同。我不确定它在找不到文件中的含义,因为它通过命令提示符运行良好。
  • 您实际上是如何运行脚本的?工作目录可能不同,改变相对路径的解析方式。
  • 我在问题的末尾做了并编辑,展示了我是如何运行它的。
  • 好的,当前工作目录可能不是包含您的脚本的目录;它是从您正在使用的 IDE 继承的。
  • 你建议如何运行它?
【解决方案2】:

对于任何回到这篇文章的人来说,解决方案是使用不同的方法来运行脚本。我使用的是默认的 python IDLE,它似乎不喜欢它。尝试在您的计算机控制台中运行脚本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-02
    • 1970-01-01
    • 2016-06-06
    相关资源
    最近更新 更多