📜 ⬆️ ⬇️

Mikrotik. SMS control 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 them went to just a long methodical digging of the code and the wiki of Mikrotik itself.

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

There is:
')
  1. CRS317-1G-16S + secondary router
  2. Mikrotik NETMETAL 5 Access Point
  3. LTE R11e-LTE Modem

Let's start with the fact that the wonderful access point of Netmetal 5 has a wired SIM card connector 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 has been disassembled, everything is set in its place (although you need to know that the SIM card is located under the modem and you cannot reach it without removing the main board), therefore check the SIM card for operation, otherwise you will have to disassemble the access point several times.

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

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

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 allows only reading values. Therefore it was necessary to dig in other party, namely 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 accessible and working WEB server on the local network; it is charged with the need to control it via Mikrotik commands.

1. On Netmetal 5, you need to make a couple of scripts to turn 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 from the forum Mikrotik routeros_api.class.php and place it in an accessible directory on the server.

instead of sfp-sfpplus16, you must 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, and that in turn will execute the command on the WEB server.

The speed of operations when receiving SMS of a second. It works stably.

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

PS I now add about the shoals in previous versions of RouterOS, which were and how to deal with them.
1. The maximum length of the message and the characters used in the messages are limited, so I had to fight at the Zabbix level, namely, to correct the template for sending the message so that, in short, it was clear what the message was about.
Setup - Actions - Report to sms - Operations - Subject: Problem: {HOST.NAME} {TRIGGER.NAME}
And to Restore Report to sms - Restore Operations Topic: Resolved: {HOST.NAME} {TRIGGER.NAME}

2. Additionally, the script itself that sends data to the modem also cuts the maximum length of the message being sent, since if it is too long, the message will not be sent.
 #!/bin/bash strz=$1 $2 $3 php /usr/lib/zabbix/alertscripts/ro.php "8926" "${strz:0:150}" echo ${strz:0:150}\" >> /var/log/sendsms.history 


Php script sending data
 <?php require('/usr/lib/zabbix/alertscripts/routeros_api.class.php'); $API = new RouterosAPI(); $API->debug=true; if ($API->connect('IP ', ' ', ' ')) { $API->comm("/tool/sms/send", array( "port"=>"lte1", "phone-number"=>$argv[1], "message"=>$argv[2],)); } $API->disconnect(); echo $argv[1]; echo $argv[2]; ?> 


3. Clearing incoming messages for RouterOS <6.44
System sheduler
/system scheduler
add disabled=yes interval=1m name=removeSMS on-event="/system script run 7" \
policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \
start-date=nov/01/2018 start-time=19:32:00


If the script below you will have a different sequence number, then in the scheduler you will need to change run 7, to the corresponding number

System-script
/system script
add dont-require-permissions=no name=removeSMS owner=admin policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source="/\
tool sms inbox remove 0\r\
\n/delay 1\r\
\n/tool sms inbox remove 0\r\
\n/delay 1\r\
\n/tool sms inbox remove 0\r\
\n/delay 1\r\
\n/tool sms inbox remove 0\r\
\n/delay 1\r\
\n/tool sms inbox remove 0\r\
\n/delay 1\r\
\n/tool sms inbox remove 0\r\
\n/delay 1\r\
\n/tool sms inbox remove 0\r\
\n/delay 1\r\
\n/tool sms inbox remove 0\r\
\n/delay 1\r\
\n"

Script with serial number 7

4. On versions lower than 6.38, the modem also restarted with built-in scripts and a scheduler.
/system script
add dont-require-permissions=no name=rebootLTE owner=admin policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source="/\
interface lte disable 0\r\
\ndelay 10\r\
\n/interface lte enable 0\r\
\n/tool sms set receive-enabled=false\r\
\ndelay 10\r\
\n/tool sms set receive-enabled=true\r\
\n"


5. And a little about the replenishment of teams. For sending SMS, let's say on a Zabbix host, generate a RSC file, and then send it to ftp on Mikrotik by a script, then in the modem itself, launch the required file using the scheduler script, commands are executed, but it seemed to me more convenient to use the mechanism above.
In the case of such a dispatch, the generated code is quite simple.
/tool sms send lte1 +7926xxxxxxx message "Problem: High ICMP ping response time Problem started at 17:08:04 on 2018.07.10 Problem name: High ICMP ping response time Host: Netgear7212 Severity: Warning Original problem ID: 5403803"

In this example, the extra characters have already been removed, and the length is unlimited. The script that processes the launch using this method after processing should inside Mikrotik copy an empty RSC file to the existing one.
This method was not liked, because, in principle, there is no protection against glitches and uncontrolled non-sending of messages is possible.

In the version of RouterOS 6.44.1, the problems of overflowing the incoming ones have already been eliminated, therefore, you can not resort to collective cleaning methods

Source: https://habr.com/ru/post/448124/


All Articles