reboot command to network device from RB

Hi,
Apologies for the title.. not sure of the best way to explain what i want to achieve in a couple of words.

We use RB’s as a cellular router to allow internet connectivity to Raspberry PI’s. The RPI’s are connected via Ethernet to the RB but powered via the USB port.

Occasionally we have a cellular disconnection so we have scripts that run to ping google and if no reply restart the LTE interface. All works great so far. Sometimes when the LTE interface stops working for one reason or another the RPI stops transmitting so it needs a reboot.

We have another script that reboots the RB once a day to reset the power via USB to the RPI. On the RPI we have a cron job that shuts it down 5 mins before the RB reboot script runs to protect the file system from corruption and once run the RPI reboots as power is restored

What i would like is rather than having this set to run once a day is to automate it via google ping, so no response from google the LTE interface is reset AND resets the USB power, however prior to this i want to issue a shut down command to the RPI. I get the shutdown command may not work due to non connectivity but i at least want to try.

So to cut a long story short… is there a way to issue a shutdown command to the RPI using specific login details for the RPI via a RB script. i.e. remote connect and login into a third party Linux device and issue a command ?

Any pointers woudl be greatly appreciated.

Many Thanks

It is possible to ssh from ROS device to remote host and execute a command there. It is possible to use key authentication (instead of password) and it is possible to do that from within a ROS script.

Yess


Just example:

/system ssh-exec address=127.0.0.1 port=22 user=root command="/sbin/shutdown -r now\r\n"

Thanks everyone for the pointers, i have never set up a passwordless login before so will be interested to get that working. Looks well documented… pity i can’t just use “sshpass” but i don’t think the RB OS supports it.

Apologies for resurrecting this post.

So i have created the keys needed on the RPI, in fact i created private, public, PEM and PPK files as i wasn’t sure which ones i needed. For testing purposes i have copied them all to the RB and have managed to import the pub file only. I also tried adding the private key but it requires a passphrase which i dont want to add as it can’t be used in the ssh-exec command.

I have added the public key to the account i login into the RB. Not sure if this is relevant but for testing purposes i am using the root account on the RPI which is different to the login i use on the RB.

When i try to run the following command -

/system ssh-exec address=192.168.88.10 port=22 user=root command="/date"

it waits a few seconds and then i get

failure: authentication failure

I have tested the key on WinSCP using the PEM file and it connects with no issues to the RPI so i know the RPI is set up correctly. I have also tested SSH access to the RPI from the RB using

/system ssh address=192.168.88.10 user=root

and that also works.

No idea why i’m getting authentication error when trying to run a ssh-exec command. Any pointers ??

For me it works like this: one only needs 2 key files as created in linux running command

ssh-keygen -m PEM -f forMT

Append contents of .pub file to file authorized_keys on your RPI, e.g.

cat forMT.pub >>/root/.ssh/authorized_keys

Then copy both forMT* files over to mikrotik. In ROS and import them

/user ssh-keys private import user=<ROS account used for running remote commands> private-key-file=forMT public-key-file=forMT.pub passphrase=""

You can verify proper private key import by running /user ssh-keys private print, this should show some details about newly imported key. In my case it’s 2048 bit RSA key.


After that it works for me:

[admin@MT] > /system ssh-exec address=192.168.42.10 user=<linux_user> command="date"
  exit-code: 0
     output: Mon 24 May 14:52:18 CEST 2021

Without properly imported private key, I see the same as you do: after a second or two (I guess it depends on how quickly SSH server rejects whatever ROS tries to do) it prints out “failure: authentication failure”.

Hi @mkx,

Thank you for such a detailed response. I have used your instructions and they have worked perfectly. I think the difference is creating the key using the PEM option to ensure they are RSA keys.

Many Thanks