[英]Execute on Raspberry Pi using SSH a command that let run a .py script on another Raspberry Pi
I am trying to let my Raspberry Pi run a script that triggers a SSH command to another Raspberry Pi on my LAN network that runs another script.我试图让我的 Raspberry Pi 运行一个脚本,该脚本会触发一个 SSH 命令到我的 LAN 网络上运行另一个脚本的另一个 Raspberry Pi。 How do I go about connecting them?
我该如何连接它们?
I don't understand your question well, you want to run a script remotely?我不太明白你的问题,你想远程运行脚本吗?
Using ssh, you can just pass as extra argument the command you want to execute, for example on pi1:使用 ssh,您可以将要执行的命令作为额外参数传递,例如在 pi1 上:
ssh user@pi2 python my_script.py
Or if you want to do that in python too:或者如果你也想在 python 中做到这一点:
import subprocess
subprocess.call("ssh user@pi2 python my_script.py", shell=True)
First, you need to enable passwordless login on the you want to connect with.首先,您需要在要连接的设备上启用无密码登录。 Log in to it, and run in Terminal
登录它,并在终端中运行
ssh-keygen
ssh-copy-id pi@raspberrypi
You'll need to enter your local pi password to allow your local Pi to access the remote one您需要输入本地 pi 密码以允许本地 Pi 访问远程密码
import subprocess
subprocess.call(["ssh","USER@HOSTNAME","./PATH"])
USER is your username on the remote Pi. USER 是您在远程 Pi 上的用户名。
HOSTNAME is the IP address (get this by running hostname -I
on the remote Pi.) HOSTNAME 是 IP 地址(通过在远程 Pi 上运行
hostname -I
来获取。)
PATH is the path to the script that you want to run on the remote Pi. PATH 是要在远程 Pi 上运行的脚本的路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.