Community discussions

MikroTik App
 
Rivins
just joined
Topic Author
Posts: 3
Joined: Wed Sep 11, 2013 10:11 pm

Reboot device when an SMS is received

Wed Sep 11, 2013 10:58 pm

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
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Reboot device when an SMS is received

Thu Sep 12, 2013 12:32 pm

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:

ros code

#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.
 
Rivins
just joined
Topic Author
Posts: 3
Joined: Wed Sep 11, 2013 10:11 pm

Re: Reboot device when an SMS is received

Thu Sep 12, 2013 3:11 pm

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:

ros code

#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
 
Rivins
just joined
Topic Author
Posts: 3
Joined: Wed Sep 11, 2013 10:11 pm

Re: Reboot device when an SMS is received

Fri Sep 13, 2013 2:58 pm

Worked amazingly well!

Thank you very much and you have a nice weekend

Regards
 
dot02
Member Candidate
Member Candidate
Posts: 108
Joined: Tue Jun 15, 2021 1:23 am

Re: Reboot device when an SMS is received

Thu Sep 15, 2022 1:34 am

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
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Reboot device when an SMS is received

Thu Sep 15, 2022 11:37 am

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:

SMS code

:cmd password script RebootRouter
Why complicate things if the function already exist...
 
dot02
Member Candidate
Member Candidate
Posts: 108
Joined: Tue Jun 15, 2021 1:23 am

Re: Reboot device when an SMS is received

Thu Sep 15, 2022 5:20 pm

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!

:lol:

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

Who is online

Users browsing this forum: wirelesslywired and 14 guests