This article describes how you can execute commands from NodeRed on a remote Linux system and thus be able to reboot and shutdown another system.
Safety instructions
I know the following notes are always kind of annoying and seem unnecessary. Unfortunately, many people who knew "better" have lost eyes, fingers or other things due to carelessness or injured themselves. Data loss is almost negligible in comparison, but even these can be really annoying. Therefore, please take five minutes to read the safety instructions. Because even the coolest project is not worth injury or other trouble. https://www.nerdiy.de/sicherheitshinweise/
Affiliate links/advertising links
Requirements
Helpful articles: So that you can install new nodes, NodeRed should of course already be installed.
How to prepare a RaspberryPi and then install NodeRed on it is described in the following articles.
In the following list you will find all the parts you need to implement this article.
Set up login via SSH without password request
For this Node code to work you first need to create an SSH key pair on the RaspberryPi you want to send the commands from - in this case the RaspberryPi running NodeRed - and exchange it with the RaspberryPi you want to control - in this case the RaspberryPi running the MagicMirror installation.
How to do this is described in the article RaspberryPi - Login via SSH without password request explained.
Log into the NodeRed configuration interface
Before you can edit your NodeRed configuration, you must - if activated - first log into the NodeRed configuration interface.
View of the login dialog. Here you have to enter the login data that you specified during the configuration of the login. You can find information about this in the article NodeRed - set up user login.
Node code
The following NodeCode causes that you can shutdown or restart the remote system from the dashboard of your NodeRed installation. Don't forget to position the two buttons on your dashboard after inserting the node code. You can find more information about this in the article NodeRed - Create a user interface with dashboard nodes.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[{"id":"73cd461.3b36fb8","type":"exec","z":"5ebf035f.49459c","command":"ssh pi@raspi 'sudo reboot'","addpay":false,"append":"","useSpawn":"","timer":"","oldrc":false,"name":"Reboot","x":900,"y":200,"wires":[[],[],[]]},{"id":"1a778b31.9113b5","type":"exec","z":"5ebf035f.49459c","command":"ssh pi@raspi 'sudo shutdown -h now'","addpay":false,"append":"","useSpawn":"","timer":"","oldrc":false,"name":"Shutdown","x":900,"y":280,"wires":[[],[],[]]},{"id":"91b9aaff.d3c2a8","type":"ui_button","z":"5ebf035f.49459c","name":"","group":"86d0df05.3b41b","order":3,"width":0,"height":0,"passthru":false,"label":"MagicMirror Neustarten","color":"","bgcolor":"","icon":"","payload":"Sicher?","payloadType":"str","topic":"","x":190,"y":200,"wires":[["32a5cb04.d66954"]]},{"id":"4244b4dc.e34d0c","type":"ui_button","z":"5ebf035f.49459c","name":"","group":"86d0df05.3b41b","order":4,"width":0,"height":0,"passthru":false,"label":"MagicMirror Herunterfahren","color":"","bgcolor":"red","icon":"","payload":"Sicher?","payloadType":"str","topic":"","x":200,"y":280,"wires":[["16e4d592.cf3ffa"]]},{"id":"32a5cb04.d66954","type":"ui_toast","z":"5ebf035f.49459c","position":"dialog","displayTime":"3","highlight":"","outputs":1,"ok":"OK","cancel":"Abbrechen","topic":"MagicMirror neu starten?","name":"MagicMirror neu starten?","x":472,"y":198.99994277954102,"wires":[["470e3428.e7f03c"]]},{"id":"470e3428.e7f03c","type":"function","z":"5ebf035f.49459c","name":"","func":"if (msg.payload=="Abbrechen")
In this way, of course, you can also execute other commands on the remote system.
To do this, simply change the specified command between the quotes. This command is then executed on the remote system.
Have fun with the project
I hope everything worked as described for you. If not or you have questions or suggestions please let me know in the comments. I will then add this to the article if necessary.