Reboot device when an SMS is received

Hello everyone,

I’m working on a company which works with Mikrotik devices and I’m struggling with its scripting language so much.

What I need to do is reboot the device whenever an SMS is sent to it. The SMS will work out like a password, so, whenever the devices sees an incoming SMS, it’ll reboot by itself (regardless the text on the SMS).

I already have my script named “reboot” set as /system reboot

I also read the docs regarding running a script when an SMS is sent, but no success

What I tried, though, was: :cmd password script reboot, but I get weird msgs like "bad command name cmd(line 1 column 2)

Apart from all that, I thought of doing the following: Check the inbox, if SMS counter > 0, /system reboot, but I have no idea of how to implement it

Can someone please help me out?

Any help is appreciated.

Thank you very much

You need to create a script at “/system scheduler” (and set it at an interval), and on every run, check the inbox for the key SMS, and then before rebooting, delete it (so that you don’t reboot indefinetly).

If this makes it sound like the SMS will reboot the router with a delay, it’s because that’s exactly right - the router will reboot only after the SMS is received AND the scheduler script runs. If you set the interval for too short, your router will start consuming lots of CPU, and if you make it too long, the delay between the SMS and the actual reboot will be longer. So pick your poison.

The scheduler script itself should be something like:
#Replace with the authorized phone number
:local phone “00000000000”

:local rebootSmsMessages [/tool sms inbox find where phone=$phone]
:if ([:len $rebootSmsMessages] > 0) do={
/tool sms inbox remove $rebootSmsMessages
/system reboot
}
Alternatively, you could use the API, and set a “/listen” command on the SMS menu. This will enable you to reboot the router without a delay, but since the API is triggered from a separate device, this means that if the router becomes unresponsive or otherwise disconnected from the network, you won’t be able to reboot at all. Ideally, you’d want to use both the API and a scheduler - API primarily, scheduler as a fallback.

Hello, Boen!

Thank you very much. I haven’t tested it yet but your solution makes a lot of sense.

Will test it soon and post a reply!

Warmest Regards

Worked amazingly well!

Thank you very much and you have a nice weekend

Regards

I also confirm the script above works well. I wanted to enhance it and to only reboot it when the SMS message was a specific string, e.g “reboot”.

#Replace with the authorised phone number
:local phone "00000000000"

:local rebootSmsMessages [/tool sms inbox find where phone=$phone]
:if ($rebootSmsMessages = "reboot") do={
    /tool sms inbox remove $rebootSmsMessages
    /system reboot   
}

However this doesn’t work…what did I do wrong in the line I modified? All I need to do is to compare the content of the SMS to the string “reboot”, right?
Cheers
Denis

Simply, after enabling the SMS receiving capability, and setting the SMS password,
create a script called “RebootRouter” with inside “/system reboot”
and send to the device from any number:
:cmd password script RebootRouter
Why complicate things if the function already exist…

Why complicate things if the function already exist…

  1. to protect our jobs by adding an extra layer of obscure complexity
  2. to make us feel important by configuring stuff that looks impressive when your boss is looking over your shoulder
  3. just for the fun of it because we’re engineers
  4. because we can!

:laughing:

Just kidding, you’re absolutely right, and I did it the way you recommended, it works perfectly well of course.
The only drawback is that the line to be sent via SMS is not very readable for non-IT folks. Sending an SMS like “Reboot Router” would be nicer, but of course only IT staff is supposed to reboot the router anyways, and it’s not like it would need a reboot 10x a day…
Therefore I’m perfectly happy with your solution!

Thanks,
Denis