MikroTik Scripts: Notification when the external IP address of the router changes

Notification when the external IP address of the MikroTik router changes. Sending a message to email or Telegram.

The script creates a global variable CurrentIP to store the current external IP address of the MikroTik router.

The script uses the API of the ipify.org service to get an external IP address.

To send an email message, use the function: MikroTik Scripts: Sending Notifications and Files to Email (function).

To send a Telegram message, the function is used: MikroTik Scripts: Sending a message to Telegram (function)

Example of an email notification of a change in the external IP address
Example of an email notification of a change in the external IP address

Article in other languages:
🇪🇸 – MikroTik Scripts: notificación cuando cambia la dirección IP externa del enrutador
🇷🇺 – MikroTik Скрипты: Уведомление при изменении внешнего IP адреса роутера
🇫🇷 – Scripts MikroTik: Notification lorsque l’adresse IP externe du routeur change
🇩🇪 – MikroTik-Skripte: Benachrichtigung, wenn sich die externe IP-Adresse des Routers ändert
🇳🇱 – MikroTik-scripts: Melding wanneer het externe IP-adres van de router verandert

Create script

[System] -> [Scripts] -> [+] -> [Name: CheckChangeExternalAdress] -> [Policy: read, write, policy, test]

Script code

:global CurrentIP;
:local NewIP ([/tool fetch url=http://api.ipify.org/ as-value output=user] -> "data")

:if ($NewIP != $CurrentIP) do={
    # Variables
    :local Time [/system clock get time];
    :local Date [/system clock get date];
    :local DeviceName [/system identity get name];
    :local Text "New IP: $NewIP, Previous IP: $CurrentIP";
    :set CurrentIP $NewIP;

    # START Send Telegram Module
    :local MessageText "\F0\9F\9F\A2 <b>$DeviceName: External IP address has changed.</b> $Text";
    :local SendTelegramMessage [:parse [/system script  get MyTGBotSendMessage source]]; 
    $SendTelegramMessage MessageText=$MessageText;
    #END Send Telegram Module

    # START Send Email Module
    :local SendTo "test@mhelp.pro";
    :local Subject "\F0\9F\9F\A2 INFO: $DeviceName [$Date $Time] External IP address has changed.";
    :local MessageText $Text;
    :local FileName "";
    :local SendEmail [:parse [/system script get SendEmailFunction source]];
    $SendEmail SendTo=$SendTo TextMail=$MessageText Subject=$Subject FileName=$FileName;
    # END Send Email Module
};

Add script to MikroTik Scheduler

For the script to work, you must set the following permissions: read, write, policy, test.

[System] -> [Schedule] -> [+] -> [Name: CheckPowerOn script] - > [Interval: 00:05:00] -> [Policy: read, write, policy, test]

Script code:

/system script run CheckChangeExternalAdress;
MicroTik Script Notification when the external IP address of the router changes

🟢 Mikrotik script notification about changing the external IP address of the router was discussed in this article. I hope now the change of the external IP address of the MikroTik device will not be unexpected. However, if you run into any problems while setting up, feel free to write in the comments. I will try to help.

✅ The script is checked: hAP ac lite [RouterBOARD 952Ui-5ac2nD], RouterOS 6.47.8 (stable).

📒 When copying materials from the site, you must indicate the author (Yun Sergey) and a hyperlink to the source (MHelp.pro).

Leave a Reply

Your email address will not be published. Required fields are marked *