Run python script on a remote raspberry pi


Is it possible to run a python script on Raspberry Pi remote from Raspberry Pi local (I don't know another word).
The purpose is that I want to run a python script on a second Raspi when I set a switch on the local Raspi. So the code is only starting when I switch it on.
I used this command:
cat | ssh user@ python -
When I deploy I get the message that the pyfiles doesn't exist.
This is the node:

[{"id":"15bf71d3.6f1f2e","type":"pythonshell in","z":"5b671f97.b2a04","name":"","pyfile":"cat | ssh sudo pi@ python -","virtualenv":"","continuous":false,"stdInData":false,"x":500,"y":680,"wires":[["4ed3e434.2763fc"]]},{"id":"a7235cd0.41a9c","type":"ui_switch","z":"5b671f97.b2a04","name":"","label":"switch","tooltip":"","group":"ca7ab20f.854e4","order":4,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":240,"y":680,"wires":[["15bf71d3.6f1f2e"]]},{"id":"4ed3e434.2763fc","type":"debug","z":"5b671f97.b2a04","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":750,"y":680,"wires":[]},{"id":"ca7ab20f.854e4","type":"ui_group","z":"","name":"Relais","tab":"32c55549.8b1c0a","order":1,"disp":true,"width":"6","collapse":false},{"id":"32c55549.8b1c0a","type":"ui_tab","z":"","name":"Relais","icon":"dashboard","order":3,"disabled":false,"hidden":false}]

I hope somebody can give me some advise.
Thank you.

So you have pi-A and pi-B and pi-A wants to trigger a python script on pi-B.

You could on pi-A use an mqtt-out node to send a msg to pi-B. On pi-B, you install NR, and add a mqtt-in node to listen for thea message from pi-A and then have pi-B run the python script

You will have to install mosquito on pi-A or pi-B but it is easy to do.

On any system reachable by both, actually, in case you prefer it that way.

It's also not necessary to install NR or your pi-B. I have a couple of Linux systems with bash scripts that subscribe to an MQTT topic and take action accordingly. For that you'll need any one of several MQTT tools, in various languages, or install the mosquitto broker to get it's tools and just not run the broker. Here's the script that sends my phone SMS, via email, as an example.


mosquitto_sub -q 1 -R -h localhost -t homie/steve-s-hubitat/hemessage/text | while read message
   echo $message | mail -s "Hubitat says:" $recipient

You can make the script as complex as you like.

EDIT: And, yes, python is one of the languages. On Arch Linux the package is python-paho-mqtt.

Thank you for your reply. I was not aware that you can set a mqtt-out node on raspiA and a mqtt-in node on RaspiB.
What I usually do is place the mqtt-out and mqtt-in node on one Raspi and run a code on RaspiB when certain criteria are met.
What is then the broker IP? Is that the IP address of RaspA on both Raspis?
Thank you again.

Thanks again. I will give it a try tomorrow and I let you know.

If the pi's are far away, means they are on different networks with internet "in between" I would never ever use a local mqtt broker on any of my pi's unless you have realized a secure connection between them (like a vpn tunnel)
No, then it is much more secure and easier to use a MQTT cloud service with a connection like this:

NR pi-A <--SSL--> MQTT Cloud Service <--SSL--> pi-B NR -> Python script

Installing NR on both pi's will make your setup easier, is a no-brainer. However, as already mentioned, with python it is also very simple to add MQTT support to the script. What I actually would do is to write a Python script that is running 24/7 and just waiting for your various commands received via MQTT to "do the various stuff". I would setup a monitoring of the script execution so that it can be automatically restarted if it stops or that the pi-B can be rebooted from remote if necessary. This type of script monitoring is also very easy to create with NR but it requires some careful thoughts

Thank you for your help.
My Raspberry Pi's are on the same local network. So no internet is needed.
I have tried some code and it seems to me that I will be able to manager as you suggested.
So, I think I'm done.

Kind regards,

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.