Mikrotik. SMS control using WEB server

    Good day to all!

    This time I decided to describe the situation, which seems to be not particularly described on the Internet, although there are some hints at it, but most of it 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, on the example of turning ports on and off.

    There is:

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

    To begin with, the wonderful Netmetal 5 access point has on board a soldered connector for a SIM card and a port for installing an LTE modem. Therefore, for this point, in fact, the best modem was bought from what was available and supported by the operating system of the point itself, namely R11e-LTE. The point was disassembled, everything was set in place (although you need to know that the SIM card is located under the modem and it is not possible to get it without removing the main board), so 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 to the modem. Unfortunately, the photo of the process was not preserved. On the other hand, universal antennas with a magnetic base were attached to the pigtails.

    The main configuration steps are described on the Internet quite well, except for small jambs of interaction. For example, the modem stops receiving SMS messages when 5 pieces come from and they hang in Inbox, clearing messages, restarting the modem does not always solve the problem. But in version 6.44.1, the reception works more stably. Inbox displays 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 one physical network. The main difficulty was that Mikrotik does not support management via SNMP, but only allows reading 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 need an accessible and working WEB server on the local network, it is necessary to manage it using 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):

    debug=true;
    if ($API->connect('IP управляемого Mikrotik', 'логин администратора', 'пароль администратора')) {
        $API->comm("/interface/ethernet/enable", array(
        "numbers"=>"sfp-sfpplus16",));
    }
       $API->disconnect();
    ?>
    

    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 need to specify the name of the disconnected / included interface.

    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 fractions of a second. It works stably.

    In addition, there is the functionality of sending SMS to phones by the Zabbix monitoring system and the opening of 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, you need to filter the characters sent to messages, otherwise SMS will not be sent.

    PS I am supplementing now about the jambs in previous versions of RouterOS that 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 fix the message sending template, so that in brief, it was clear what the message was about.
    Settings - Actions - Report to sms - Operations - Subject: Problem: {HOST.NAME} {TRIGGER.NAME}
    And on recovery Report to sms - Recovery operations Subject: Resolved: {HOST.NAME} {TRIGGER.NAME}

    2. Additionally The script itself, which sends data to the modem, also cuts the maximum length of the message sent, because if it is too long, then 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
    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. Cleaning incoming messages for RouterOS <6.44
    System-Sheduler If the script below you will have a different serial number, then in the scheduler you will need to change run 7 to the corresponding number System-Script The script with serial number 7 4. On versions below 6.38 it helped reboot modem also with built-in scripts and scheduler 5. And a little about replenishment of commands. To send an SMS, it’s permissible to say on a host with Zabbix to generate an RSC file, and then send it to ftp on Mikrotik with a script, then in the modem itself, by the script in the scheduler, run the required file, the commands are executed, but it seemed to me more convenient to use the mechanism above. In the case of such sending, the generated code is quite simple.
    /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





    /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"




    /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"




    /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, extra characters are already removed, and the length is unlimited. The script that processes the launch according to this method after working out should copy an empty RSC file inside the existing Mikrotik file.
    I did not like this method, since in principle there is no protection against glitches and uncontrolled unsent messages are possible.

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

    Also popular now: