Mikrotik. Management via SMS using WEB server

Good day to all!

This time I decided to describe a situation that seems to be not particularly described on the Internet, although there are some hints of it, but most of it was just a long methodical digging of the code and the wiki of Mikrotik itself.

Actually the task: to implement with the help of SMS the control of several devices, using the example of turning ports on and off.

There is:

  1. Secondary Router CRS317-1G-16S+
  2. Access point Mikrotik NETMETAL 5
  3. LTE modem R11e-LTE

Let's start with the fact that the wonderful Netmetal 5 access point has a soldered SIM card slot and a port for installing an LTE modem on board. Therefore, for this point, in fact, the best modem was purchased from what was available and supported by the operating system of the point itself, namely R11e-LTE. The point was disassembled, everything was installed in its place (although you need to know that the SIM card is located under the modem and it is impossible to get it without removing the main board), so check the SIM card for operability, otherwise you will have to disassemble the access point several times.

Next, we drilled a couple of holes in the case, installed 2 pigtails and fixed the ends on the modem. Unfortunately, the photo of the process has not been preserved. On the other hand, universal antennas with a magnetic base were fixed to the pigtails.

The main setup steps are described on the Internet quite well, except for small interaction jambs. For example, the modem stops receiving SMS messages when 5 messages are received and they hang in the Inbox, clearing messages, restarting the modem does not always solve the problem. But in version 6.44.1, the reception works more stably. The last 4 sms are displayed in Inbox, the rest are automatically erased and do not interfere with life.

The main goal of the experiment is to extinguish and raise interfaces on two routers in the same physical network. The main difficulty was that Mikrotik does not support management via SNMP, but only allows you to read values. Therefore, I had to dig in the other direction, namely the Mikrotik API.

There is no clear documentation on how to manage, so I had to experiment and this instruction was made for future attempts.

To manage multiple devices, you will need an available and working WEB server on the local network, it is responsible for managing Mikrotik commands.

1. On Netmetal 5, you need to make a couple of scripts to turn it on and off, respectively

system script
add dont-require-permissions=no name=disableiface owner=admin policy=
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=
    "/tool fetch http://WEB_SERVER_IP/di.php "
add dont-require-permissions=no name=enableiface owner=admin policy=
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=
    "/tool fetch http://WEB_SERVER_IP/en.php "

2. Create 2 scripts on the web server (of course, php must be installed on the system in this case):

<?php
# file en.php enable interfaces    
require('/usr/lib/zabbix/alertscripts/routeros_api.class.php');

    $API = new RouterosAPI();
    $API->debug=true;

if ($API->connect('IP управляСмого Mikrotik', 'Π»ΠΎΠ³ΠΈΠ½ администратора', 'ΠΏΠ°Ρ€ΠΎΠ»ΡŒ администратора')) {
    $API->comm("/interface/ethernet/enable", array(
    "numbers"=>"sfp-sfpplus16",));
}
   $API->disconnect();
?>

<?php
#file di.php disable interfaces
    require('/usr/lib/zabbix/alertscripts/routeros_api.class.php');

    $API = new RouterosAPI();
    $API->debug=true;

if ($API->connect('IP управляСмого Mikrotik', 'Π»ΠΎΠ³ΠΈΠ½ администратор', 'ΠΏΠ°Ρ€ΠΎΠ»ΡŒ администратора')) {
    $API->comm("/interface/ethernet/disable", array(
    "numbers"=>"sfp-sfpplus16",));
}
   $API->disconnect();
?>

3. Download routeros_api.class.php from the Mikrotik forum and place it in an accessible directory on the server.

instead of sfp-sfpplus16, you need to specify the name of the interface to be disabled/enabled.

Now when sending a message to a number in the form

:cmd Π‘Π•ΠšΠ Π•Π’ΠΠ«Π™ΠšΠžΠ” script enableiface
ΠΈΠ»ΠΈ
:cmd Π‘Π•ΠšΠ Π•Π’ΠΠ«Π™ΠšΠžΠ” script disableiface 

NETMETAL will run the corresponding script, which in turn will execute the command on the WEB server.

The speed of operations when receiving SMS is a fraction of a second. Works stably.

In addition, there is the functionality of sending SMS to phones by the Zabbix monitoring system and opening a backup Internet connection when the optics fall. Perhaps this is beyond the scope of this article, but I will say right away that when sending SMS, their length should fit into the standard size of one message, because. Mikrotik does not divide them into parts, and when a long message arrives, it simply does not send it, in addition, it is necessary to filter the characters transmitted in messages, otherwise the SMS will not be sent.

Source: habr.com

Add a comment